Sunday, July 15, 2012

Powershell: Scratching the Surface

Powershell: Scratching the Surface


I started using powershell a few years back to automate the creation of Exchange emailboxes in our Exchange 2007 system.  I used some borrowed code and modified it to fit our environment.  At the time I was able to figure it out just good enough to get it working.  I've since made a lot of progess with learning the scripting language and have not found many things I can't do with it.  If you are familiar with writing cmd batch files, this should come fairly easy to you.

Powershell to me seemed a bit intimidating at first because of the complexity the cmdlets could reach.  I've since learned that it is very easy to use once you understand some of the base concepts.  About a year ago, I was determined to learn more about it and have progressed greatly in my ability to write scripts.  I've found a lot of help out there from numerous online resources and would like to pull what I know into this blog to help others get started.  I'll start with some basics here and work my way into some more advanced scripts.  

Example 1: Simple text output


For starters, lets do some "Hello World" examples.  This first example is how you can simply output the text "Hello World" in the command shell window.

write-host "Hello World"

This is as simple as it gets.  Notice that the command write-host is a verb-noun combination that produces a result.  Most powershell cmdlets revolve around this concept, which to me makes it much easier to remember commands.  A lot of powershell commands have a shorter equivelent command.  For example:

Echo "Hello World"

Echo, which you remember from DOS and cmd prompt produces the same result as write-host.  Another example:

get-childitem

dir

Some cmdlets also have short acronyms you can use instead of typing the full command such as:

gci

Gci will give you the same directory listing output as get-childitem or dir. 

Anyway, back to hello world.  So say you want to output a string of text to a file to store for later use or review.  You can accomplish this in this way:

echo "Hello World" | out-file c:\scripts\helloworld.txt

You can append text to that same file this way:

echo "Hello World" | out-file c:\scripts\helloworld.txt -append

In some cases you may run into a program that isn't able to properly decode the default encoding scheme.  Let's set the encoding to ascii by doing this:

echo "Hello World" | out-file -encoding ascii c:\scripts\helloworld.txt -append

Now, lets say you want to read the text from the helloworld.txt file you just created.  You can accomplish this by doing the following:

get-content c:\scripts\helloworld.txt

This will display the contents of the helloworld.txt file in the powershell console.

Example 2: Simple Variable Usage

Learning the ins and outs of variable usage in powershell is very important because you'll use them in almost every script you write.  All powershell variables are preceeded by a $ and are connected to a value using a  = .  For example:

$value1 = 5
$value2 = 7

Powershell is also equiped with mathematical functionality.  After creating the variables above, try this:

$value1 + $value2

You should see an output of "12" on your screen as powershell performed an addition function in this last command.

Here's another way you can use variables to store a value.  Lets pull the data from the helloworld.txt we created in the last example.

$hello = get-content "c:\scripts\helloworld.txt"

Now that we've stored the contents of the text file in the variable, lets run it by typing the following:

$hello

This displays the contents of the text file in the console.

Lets combine this with some other text.  Type the following:

write-host "$hello, how are you?"

Now you have combined the contents of the text file with the additional provided text to create a single output in the console.

This concludes my first post.  Hope it was helpful.  For questions or comments, please email ps1scripting@gmail.com

No comments:

Post a Comment