In all honesty, I did hear about Windows PowerShell quite a while ago but never really bothered to take a closer look. Mainly, because I was thinking that this would be little more than a modern Command Line Prompt and secondly, because I did not really see a lot of use to my daily business in there… how wrong I have been!
Just recently, a colleague of mine started digging into Windows PowerShell and soon enough he was (jokingly, I am hoping) raving about “what else can be scripted”. While he was doing that on purpose and with a clear idea of what he wanted to achieve, he got me curious.
So here is From Zero to PowerShell 2.0 – or what are the first steps of someone who is trying to find out what PowerShell is all about…
What do I need to install PowerShell? And what is it?
The first question is answered easy enough. If you are running Windows 7 like me you do not need to install anything – PowerShell has been installed with the Operating System. You can find it under Start -> All Programs -> Accessories -> Windows PowerShell.
The What is it part is a bit more difficult to answer: putting it into one sentence, I am currently tempted so say “It is a modern, .NET-based scripting platform for administrative purposes” – although that might cut it a bit too short after all. But for the time being, I’d like to settle for that statement.
PowerShell Testing – Hello World!
Of course, curiosity always wins: you can read a thick book about Windows PowerShell or you can search the Internet for long examples – there is nothing better than diving into it right away to get an initial idea – remember the “Hello World!” Programs of the old days? It came back in any programming language there ever was – and to many of us it was the first line of code we ever wrote in a new language.
Go to Start -> All Programs -> Accessories -> Windows PowerShell and launch the interactive console, Windows PowerShell (not to be confides with Windows PowerShell ISE – we will see that one later).
So back to “Hello World!” – which would be a rather boring example so let’s beef it up a bit: we want the system to write “Hello World! – It is <the current date> and this is the first PowerShell Experience I’ve ever made…”.
Before I worry in how to deal with the text, I want to show you how to actually get the current date from the system through PowerShell: type Get-Date and press the Return key:
Windows PowerShell Copyright (C) 2009 Microsoft Corporation. All rights reserved. PS C:\Users\azapf> Get-Date Sonntag, 27. Februar 2011 14:25:00 PS C:\Users\azapf>
As you can see Get-Date produced the current system date. In plain words, I would call Get-Date a command but PowerShell has introduced the term Commandlet for this type of statement – often abbreviated as Cmdlet.
So a Commandlet is a PowerShell command – consisting of different parts itself: first, there is what PowerShell calls the verb: Get. Then there is the Noun – Date. Sometimes, there are additional Parameters which can be mandatory or optional.
So let’s get done with our Hello World! Example – in the PS Console, try this:
"Hello World! It is $(Get-Date) and this is the first PowerShell Experience I've ever made...".
This is going to be a very short detour – the entire topic deserves its own post later. Nonetheless, this is important. In PowerShell, you have the ability to define variables and store data in them.
The long form of the sample above could have been
$today = Get-Date "Hello World! It is $today and this is the first PowerShell Experience I've ever made...".
In this case, $today would have been our Variable and it would have stored the result of the Get-Date command. In the second step, we would have merely told the PowerShell Command Interpreter to insert whatever is stored in $today into the string and display it with the rest:
In the original sample, I used a construct “[…] is $(Get-Date) […]” instead of defining a variable first. This is actually an ad-hoc usage, telling the system to take whatever Get-Date returns and immediately use it in this place – the only important element to note here is that Get-Date has to be put into () – otherwise, it will not be interpreted as Commandlet!
But let’s put the Variables on the side for the moment – and take a second look at the Commandlets.
The Commandlet Reference
We already discussed that a Commandlet consists of the Verb, the Noun and potentially some Parameters.
So far, we only know the Get-Date Commandlet. How about other Cmdlets, especially those that get us something else? Funny enough, there is a Commandlet named Get-Command.
The help available for that Commandlet reads
“The Get-Command cmdlet gets basic information about cmdlets and other elements of Windows PowerShell commands in the session, such as aliases, functions, filters, scripts, and applications.”
Woohoo – exactly what we want! That’s actually pretty cool! In the PowerShell Console, type Get-Command Get-* to list all Commandlets that begin with Get-. And almost en passant, we have also used the first Parameter to a Commandlet.
“Displays information about Windows PowerSHell commands and concepts”
So try Get-Help Get-Date to see the following:
In other words: you can use Get-Command to explore what commands are available in Windows PowerShell 2.0 and you can use Get-Help on the command of your choice to get a more detailed information on what the command is all about and what parameters it takes.