More power with PowerShell

For quite some time now I've wanted to be able to create scripts a bit more complicated than plain batch files. I mainly use Windows in my daily work so switching to Linux just for the sake of making cool scripts wasn't a desire of mine and as I discovered not even needed. I accidentally discovered Windows PowerShell (PS) while playing around with Windows 7 beta looking for new ways to set up a programming enviroment for Qt programming. While PS will be implemented in Windows 7 by default, it is to my supprice, and good fortune, also available for both Windows XP and Windows Vista.

So enough of this Windows commercial - I'm not getting paid.(Really!) In a small series of blogg entries I will have a look at several functions PS can provide, and try to set up some scenarios on how to use them. I do this as much for my own learning as for the attempt to educate the masses:)

Lets start with some basic stuff to kick off. Playing around with text and files is both usefull and needed in many different settings. Should you want to generate files, or just report content and analyze it on screen the .net support in PS enables you to a whole lot of options. PS allows you to create different kinds of variables and even objects.

Below I will show some ways of gathering and manipulating information, as well as using the info to create meaningfull(?) output. We will start off with some simple string commands and assigning values to some variables. After that we try the same with file content. Last I will demonstrate a few ways of handling the content.

Lets assign a simple text string to the variable $foo and do a
simple echo. The value gets assigned and echoed as a string.

PS C:\> $foo = "Hello Nerd"
PS C:\> echo $foo
Hello Nerd

Now lets assign the value of $foo to $bar and split it. The $bar variable becomes an array of strings, split by white spaces (both space and tab counts as white spaces). We can echo one or more indexes of the array and combine them with values to create new strings.

PS C:\> $bar = $foo
PS C:\> $bar = $bar.split()
PS C:\> echo $bar[0]
Hello
PS C:\> echo $bar[1]
Nerd

PS C:\> $foo = $bar[0]
PS C:\> echo $foo
Hello
PS C:\> $foo = $foo+"World"
PS C:\> echo $foo
HelloWorld

It is worth noting that the 'splitting' character will disappear in the split process and in that case breaking the string following it. So handle with care.

PS C:\> $foo = $foo.split('W')
PS C:\> echo $foo
Hello
orld

As the value of $foo now is "broken" we reassign a value to it by combining the variable $bar, a character sutable for splitting and a string.

PS C:\> $foo = $bar[0]+';'+'World'
PS C:\> echo $foo
Hello;World
PS C:\> $foo = $foo.split(';')

Now lets echo the values again
PS C:\> echo $foo[0] $foo[1]
Hello
World

PS C:\> $foo = $foo[0] + ' ' + $foo[1]
PS C:\> echo $foo
Hello World

Now we know how to create an array and navigate it in a basic way. We could also use some information about the array, for instance how long it is. The next example shows us the use of the length funtion on the array. Finding the wat of a string is done in a similar way.

PS C:\> $foo = "Hello there world"
PS C:\> $bar = $foo.split().length
PS C:\> echo $bar
3

PS C:\> $foo = "Hello there world"
PS C:\> $foo.length
17

Lets combine the strings we use with files. We can extract the content of the file foo.txt by using the Get-Content(alias: gc) cmdlet.

PS C:\Temp> $foo = gc C:\Temp\foo.txt
PS C:\Temp> echo $foo
Hello World - this is a test file. The content is assigned to $foo.
second line
third line
PS C:\Temp> $foo.length
3

We now have an array containing the file content.
Let us join the lines into one string.

PS C:\Temp> $foo = [string]::join(" ",$foo)
PS C:\Temp> $foo.length
91

PS C:\Temp> echo $foo
Hello World - this is a test file. The content is
assigned to $foo. second line  third line

This entry has already grown a bit so I think I will stop here and continue in the next entry. There I will turn the focus more on web tech and how I can use PS to generate HTML/XML from a selection of options. I think that in the end I will have a custom cmdlet that can generate a file or a set of HTML files ready to take some content and published. As usual I would love to have your comments and questions.

So until next time - happy coding.

Comments

Popular posts from this blog

Designing and programming - Part 2

Filtering Dropdown choices in a Power Pages form using Dataverse Relations

Exploring the Power of Variables in Liquid and Power Pages