Windows PowerShell in Action: Learning Windows PowerShell

This excerpt from Chapter 1 of the book "Windows PowerShell in Action," explains how to install PowerShell and how to start the interpreter once it's installed. You'll also review the basic formats of Window's PowerShell's commands, how command-line editing works and how to use PowerShell's Tab-Completion feature.

This chapter excerpt from Windows PowerShell in Action, by Bruce Payette, is printed with permission from Manning Publications, Copyright 2008.

Click here to purchase the entire book.

1.4.1 Installing and starting Windows PowerShell

More on Windows PowerShell

These Scripting School columns explain how to use Windows PowerShell.

Visit our topical resource center for news, tips and expert advice on scripting with Windows PowerShell.
First things first—you'll almost certainly have to download and install the PowerShell package on your computer. Go to the PowerShell page on the Microsoft website.

This page should contain a link that will take you to the latest installer and any documentation packages or other materials available. Alternatively, you can go to Microsoft Update and search for the installer there. Once you've located the installer, follow the instructions to install the package. After you have it installed, to start an interactive PowerShell session go to:

Start -> Programs -> Windows PowerShell

When it's started, you'll see a screen like that shown in figure 1.3:

Figure 1.3 When you start an interactive Windows PowerShell session, the first thing you see is the PowerShell logo and then the prompt. As soon as you see the prompt, you can begin entering commands.

Now type the first command most people type: "dir". This produces a listing of the files on your system, as shown in figure 1.4.

Figure 1.4 At the prompt, type "dir" and press the Enter key. Windows PowerShell will then execute the dir command and display a list of files in the current directory.

As you would expect, the dir command prints a listing of the current directory to standard output.

NOTE Let's stop for a second and talk about the conventions we're going to use in examples. Since PowerShell is an interactive environment, we'll show a lot of example commands as the user would type them, followed by the responses the system generates. Before the command text, there will be a prompt string that looks like "PS (2) >". Following the prompt, the actual command will be displayed in bold font. PowerShell's responses will follow on the next few lines. Since Windows PowerShell doesn't display anything in front of the output lines, you can distinguish output from commands by looking for the prompt string. These conventions are illustrated in figure 1.5.

Figure 1.5 This diagram illustrates the conventions we're using for showing examples in this book. The text that the user types is shown in bold. Prompts and other output from the interpreter are shown in normal weight text.

On to the examples. Instead of simply displaying the directory listing, let's save it into a file using output redirection. In the following example, we redirect the output into the file c:foo.txt and then use the type command to display what was saved:

PS (2) > dir c:config.sys > c:foo.txt
PS (3) > type c:foo.txt
Directory: Microsoft.PowerShell.CoreFileSystem::C:
Mode LastWriteTime Length Name
---- ------------- ------ ----
-a--- 11/17/2004 3:32 AM 0 config.sys
PS (4) >

As you can see, commands work more or less as you'd expect if you've used other shells. Let's go over some other things that should be familiar to you.

1.4.2 Command editing

Command-line editing works the same way for Windows PowerShell as it does for cmd.exe. The available editing features and keystrokes are listed in Table 1.1:

Keyboard sequence Editing operation
Left/Right Arrows Move the editing cursor left and right through the current command line
Ctrl-Left Arrow,
Ctrl-Right Arrow
Holding the control (CTRL) key down while pressing the left and right arrow keys will move the editing cursor through the current comman line one word at at time, instead of one character at a time.
Home Move the editing cursor to the beginning of the current command line.
End Moves the editing cursor to the beginning of the current command line.
Up/Down Arrows Moves up and down through the command history
Insert Key Toggles between character insert and character overwrite modes.
Delete Key Deletes the character under the cursor.
Backspace Key Deletes the character behind the cursor
F7 Pops up command history in a window on the console. Use the up and down arrows to select a command, then Enter to execute that command
Tab Does command line completion. (See the next section for details.)

These key sequences let you create and edit commands effectively at the command line. In fact, they aren't really part of Windows PowerShell at all. These command-line editing features are part of the Windows console subsystem, so they are the same across all console applications. There is one editing feature, however, that is significantly different for PowerShell. This is command completion, also call tab-completion. While cmd.exe does have tab-completion, PowerShell's implementation is significantly more powerful. We'll describe this feature next.

1.4.3 Command completion

An important feature at the command line is tab-completion. This allows you to partially enter a command, then hit the Tab key and have Windows PowerShell try to fill in the rest of the command. By default, PowerShell will do tab completion against the file system, so if you type a partial file name and then hit Tab, the system matches what you've typed against the files in the current directory and returns the first matching file name. Hitting Tab again takes you to the next match, and so on. Windows PowerShell also supplies the powerful capability of tab-completion on wild cards (see chapter 4 for information on PowerShell wild cards). This means that you can type:

PS (1) > cd c:pro*files

and the command is expanded to:

PS (2) > cd 'C:Program Files'

PowerShell will also do tab-completion on partial cmdlet names. If you enter a cmdlet name up to the dash and then hit the Tab key, the system will step through the matching cmdlet names.

So far, this isn't much more interesting than what cmd.exe provide. What is significantly different is that PowerShell also does completion on parameter names. If you enter a command followed by a partial parameter name and hit Tab, the system will step through all of the possible parameters for that command.

PowerShell also does tab-completion on variables. If you type a partial variable name and then hit the Tab key, PowerShell will complete the name of the variable.

And finally, PowerShell does completion on properties in variables. If you've used the Microsoft Visual Studio development environment, you've probably seen the Intellisense feature. Property completion is kind of a limited Intellisense capability at the command line. If you type something like:

PS (1) > $a="abcde"
PS (2) > $a.len

The system expands the property name to:

PS (2) > $a.Length

Again, the first Tab returns the first matching property or method. If the match is a method, an open parenthesis is displayed:

PS (3) > $a.sub

which produces:

PS (3) > $a.Substring(

Note that the system corrects the capitalization for the method or property name to match how it was actually defined. This doesn't really impact how things work. PowerShell is case-insensitive by default whenever it has to match against something. (There are operators that allow you to do case-sensitive matching, which are discussed in chapter 3).

AUTHOR'S NOTE: The PowerShell tab completion mechanism is user extendable. While the path completion mechanism is built into the executable, features such as parameter and property completion are implemented through a shell function that users can examine and modify. The name of this function is TabExpansion. Chapter 7 describes how to write and manipulate PowerShell functions.

This was first published in August 2007

Dig Deeper



Find more PRO+ content and other member only offers, here.



Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: