Beginning Windows Powershell Scripting

Powershell is a replacement shell for the Microsoft Windows operating system that brings advanced scripting to Windows. Previously, Windows Powershell has been packaged as a separate add-on to Windows, marketed mainly to server administrators. Starting with Windows 7, Powershell is now a built-in part of the operating system, giving this capability to all Windows users.

Scripting with Powershell is far more flexible than batch-file scripting, and for those of you who have worked with Perl, you'll find a number of similarities present in Powershell.

I've designed this tutorial series to provide a very quick introduction to scripting with Powershell, and will not be going deeply into the details of the environment. My goal is to get you creating practical scripts right away, and assume that with such an introduction you'll be interested enough to delve deeper into the subject matter on your own.

Starting out: Hello, Powershell!

Since the late 1970s, Hello, World has been the traditional way to introduce new programmers to a language, and this tradition is equally useful for Powershell. To begin, bring up the Windows 7 command-prompt and type powershell. This transforms your basic Windows command-line environment into a super-charged Powershell environment:

C:\bin> powershell
Windows PowerShell
Copyright (C) 2009 Microsoft Corporation. All rights reserved.
PS C:\bin>

You're now ready to enter Powershell commands directly, or you can combine a number of commands into a script that can be run from the powershell command-line. Since this is a Powershell scripting tutorial, we'll be making scripts. Let's get to it!

Open your favorite text-editor (mine is Vim, though many Windows script programmers simply use Notepad which works well enough) and begin scripting! We'll name our script hello.ps1:

PS C:\bin> notepad hello.ps1


Powershell expects that its scripts have a ".ps1" file-name extension.

This will bring up your text editor window so you can begin scripting.

Writing output

Type the following in your text editor:

"Hello, World!"

That's the entire program! Save the file and run it from the Powershell command-line.

PS C:\bin> .\hello.ps1
Hello, World!

> Note: Unless the current directory is in your PATH, Powershell will need to know where your script is. If it's in the current directory, the path is '.'

Powershell will print any bare string or variable directly to the command-line. There's no need for a print command, but you can use one if you like. This script will produce exactly the same output as the one before:

write-host "Hello, World!"

Using variables

Programming and scripting languages frequently feature variables. In Powershell, variables are declared by simply writing the variable name, prefaced with a $ character, and assigning a value to it. To show this, we'll modify hello.ps1 to use a variable that stores the string to output.

$hello = "Hello, World!"

As before, simply typing the variable's name on a line by itself will print its value to the console.

Strings can be concatenated by using the '+' operator. Suppose we wanted to greet a number of different people. We'd likely want to keep the greeting separate from the name(s) of who we'll be greeting, so we can combine the parts of the greeting together. The string could be built as follows:

$greeting = "Hello"
$name = "World"
$hello = $greeting + ", " + $name + "!"

As in Perl, variables that occur inside a set of double-quotes will be expanded to their values. The above program could thus be written as:

$greeting = "Hello"
$name = "World"
$hello = "$greeting, $name!"

Since we already know that bare strings are output directly, the program could be made even more simple by eliminating the final variable:

$greeting = "Hello"
$name = "World"
"$greeting, $name!"

All of these variants produce exactly the same output. Try it yourself!

Getting user input

Remember the write-host command used in the earlier example? There's also a read-host command that's useful for getting user input. The syntax is simple:

$greeting = "Hello"
$name = read-host "What is your name?"
"$greeting, $name!"

Running this code will prompt you to enter your name, as follows:

PS C:\bin> .\hello.ps1
What is your name?: Powershell
Hello, Powershell!

That's it for this lesson! The next lesson will talk about Powershell data types.

For more information

While you're experimenting and learning Powershell, you may find these sources of more information to be of use:

Get help!

Typing help at the Powershell prompt provides a paged view of Powershell's built-in help system. You can also get help on specific subjects: try typing help about_variables for a page that provides much more information about working with variables.

Visit the Powershell blog

There's a lot of useful information given on Microsoft's official Powershell blog. It's a prime source of tips and esoterica for Powershell use and scripting.