Thursday, December 30, 2010

Hello World!

Hello World! The famous first words that fledging programmers get to watch appear on the screen as they write the first bit of code. My first Hello World program in QBASIC over 15 years ago. After an unsuccessful attempt to learn C in many of it variants, I have moved to Windows scripting as my preferred choice in Windows programming. I guess you can so: "For those that can't code, script". I have been learning VBScript for the past year until I came across this new wonderful language called PowerShell. PowerShell allows you to do everything VBScript does and more. Like access to Windows .Net framework and such. Now, I have no .Net experience, it's almost Latin to me, but as I continue on this journey of learning to write PowerShell scripts, maybe I'll figure it out. Most everything I know is self taught, pulled from existing scripts I have access to. Dissecting them to find out how they work. And PowerShell is a great language for dissecting code. Which brings us to our first topic today: Get-Help

Get-Help
Whenever you come across a command, called cmdlet, in PowerShell you don't understand, you can always use the Get-Help cmdlet.

Get-Help Example (Click for larger image)
 Which brings us to our next command: Write-Host

Write-Host
Write-Host does exactly that, it writes customized output to the host, aka the console windows. It can't be used, to the best of my knowledge, for anything else. For example, it wont pipe into an Out-File cmdlet, but more on Out-File and piping later, for now, let's write our first bit of code:

Write-Host "Hello World"
Hello World!
Pretty simple right? But what if you didn't want to type this <sarcasm>long</sarcasm> command every single time? Open your favorite text editor, or my personal favorite the PowerShell ISE and put this command in it and save it as a .ps1 file. In this example HelloWorld.ps1. Now you can run it from the PowerShell console just like this.

.\HelloWorld.ps1
HelloWorld.ps1
But wait, that's not all! No true code is complete without some kind of loop in it. PowerShell supports a myriad of loop constructs. The first one I will cover is the ForEach loop.

ForEach
ForEach is a loop construct that allows you to move through an object, such as an array, assigning each element to a variable to be worked with. Let's make our little script from before slightly more complicated.

$HelloWorld = "H","e","l","l","o"," ","W","o","r","l","d","!"
ForEach ($letter in $HelloWorld)
{
    Write-Host $letter
-nonewline

}
Write-Host

Let us examine each line here. The first line establishes an array called $HelloWorld. There are a couple of different ways to establish an array, but I will use this one for now. It's the next line we want to focus on, ForEach. This is the beginning of the loop construct and, in my opinion, one of the easiest to master. What happens here is: The script loops through the $HelloWorld array and assigns each element to the variable $letter. Then using our friend Write-Host with the switch -nonewline, which prevents carriage returns between the letters, we display our message. The last Write-Host after the loop gives us a new line after the message is written.

A quick note about how PowerShell works. You can take the above code, and save it as a .ps1 file, nothing wrong with that, but PowerShell has the ability to do a line by line execution straight from the console window. Now, to me, that's amazing. Just look at the above code in this example:
Line by Line Execution (Click for a larger image)
Another thing to remember about PowerShell is, it is not case sensitive. Write-Host, write-host, wRITE-hOST, and any other combinations of capitalization that you can think of, all do the same thing. I will do my best to stay uniform with capitalization to ensure readability.

In conclusion, I haven't even begun to scratch the surface of what is possible with PowerShell. The is pretty much no way anything I have put down today could be of practical use, but this is just the start. Once I get this up to speed with what I know, I'll begin to add other elements as I learn them

Coming Next
Next week, I will try to cover: Read-Host, String Manipulation, Aliases, and Piping. All very useful, and a great next step for the beginner. Until then...