PowerShell: Windows Scripting Made Fun Again!

Want to help support this blog? Try out Oh Dear, the best all-in-one monitoring tool for your entire website, co-founded by me (the guy that wrote this blogpost). Start with a 10-day trial, no strings attached.

We offer uptime monitoring, SSL checks, broken links checking, performance & cronjob monitoring, branded status pages & so much more. Try us out today!

Profile image of Mattias Geniar

Mattias Geniar, February 27, 2009

Follow me on Twitter as @mattiasgeniar

Windows Scripting used to be horrible. You got a lousy language to write in – Visual Basic (especially coming from C-like languages) – and it just didn’t seem all that powerfull. Any action you wanted done, required quite a few lines of code, a healthy supply of patience and.

But now, there’s a new kid in town; Windows PowerShell.

And they choose its name right; PowerShell. It IS powerful.

One of the most intriguing features is its ability to pipe output from one command, to the other.

How there – what’s new in that? Linux has had this for many years now, and even the command prompt in windows allowed you to pipe output onto another program.

It’s they way it pipes that output. Normal shells take the output of a certain command (which is plain text), and passes that text on as the input for the next command. PowerShell does this better; it takes the output from the first command, which can be anything from plain text, to an array of objects, to a SQL record set – and intelligently feeds it to the second command, which interprets its datatype (String, Hashtable, RecordSet, …) and can immediately process it.

This allows for much more robust programs, that get things done quicker – and in fewer lines of code. Granted, it has a bit of a weird syntax to get used to – but once you grasp it, the only limits you will reach are those of your own imagination.

There’s a very interesting video on Channel9 featuring Erik Meijer and Jeffrey Snover on how PowerShell was built, and what it’s capabilities are. Since the video features the creator of PowerShell, it really drills down to the very details of it.

Installing PowerShell

It’s a built-in feature in Windows Server 2008, but runs just as well in earlier versions of Windows Server (as well as Windows XP/Vista). You can download it, and install it (without rebooting the desktop/server!).

Get it going

Nothing fancy about that. Start it from your Start menu (or add a shortcut to %systemroot%/system32/ directory to the PowerShell executable, to start it from the Run screen).

Start PowerShell

Start PowerShell

You’ll see a new “command prompt” like screen, with a blue background color.

Your first command

Nothing like losing your virginity on a new scripting language, so allow me to pick your cherry; type the following cmdlet: Get-Date. This will print the current date, in your current open shell, in a localized language.

Get-Date: Date & Time in Powershell

Get-Date: Date & Time in Powershell

One of the key features of PowerShell, is that almost everything is an object, from which you can invoke methods, of retrieve properties. If you want to retrieve a property from an object, that isn’t assigned to a variable, you need to enclose it in round brackets. Try the following command for instance:

(Get-Date).Year











Get-Date Object: (Get-Date).Year

(Get-Date).Year


This will parse the object “Get-Date”, and show the property “Year”. Obviously, there’s “Minute”, “Hour”, “Second”, “Month”, … A detailed overview at Microsoft’s Technet; Listing Date & Time Information: Get-Date.

Variables

Finally one of the few languages that has adopted the dollar-sign variable notation; $value = “string”.

You can try this immediately in the PowerShell: $variable = “Here’s my value.".  This will assign the value “Here’s my value” to the variable “$variable”. Print it, by typing $variable.

Variable Assigning

Variable Assigning

To complete the example above, the following is also possible.

$dteCurrentDate = Get-Date

$dteCurrentDate.Year

$dteCurrentDate.Month

$dteCurrentDate.ToString()

$dteCurrentDate.AddDays(10).ToString()

Get-Date Properties

Get-Date Properties

PowerShell’s awesomeness

Let’s get to the fun part. Piping output from one cmdlet to another, and parsing its objects/properties immediately. Issue the following command.

ls

Unix users will know this as the command to list files & folders in a particular directory (and without a parameter, the current directory). Similar to “dir” in command prompt. Output ought to be similar to this.

ls: Mode, LastWriteTime, Length, Name

ls: Mode, LastWriteTime, Length, Name

Now image you only want the columns Name and Length, in that order?

ls | Select Name, Length

This will parse the output of “ls” (which normally includes columns such as Mode, LastWriteTime, Length & Name) and filter it down to only the “Name” and “Length” column.

ls | Select Name, Length

ls | Select Name, Length

GUI: command completion, syntax highlighting & more

While writing in a plain old notepad/wordpad will work too, it’s always nice to have a GUI around for code completion & syntax highlighting. PowerGUI is powerfect (pun intended) for that.

PowerGUI Interface

PowerGUI Interface

Especially useful when you’re just starting to get into it, the code completion can help find that one cmdlet you just couldn’t remember. Definately worth the download.

Wrap it up

These are some of the basics of PowerShell, and don’t actually dive deep into it. I’ll be writing a few more segments on PowerShell, which go deeper into directory looping & filtering, sending mails from PowerShell, running MySQL queries, basic operations, …

I’m loving all the cool things you can accomplish through PowerShell, you should too! ;-)

In the meantime, here are some very interesting links to learn more about PowerShell.



Want to subscribe to the cron.weekly newsletter?

I write a weekly-ish newsletter on Linux, open source & webdevelopment called cron.weekly.

It features the latest news, guides & tutorials and new open source projects. You can sign up via email below.

No spam. Just some good, practical Linux & open source content.