This article is excerpted from the book Windows 7 and Vista Guide to Scripting, Automation, and Command Line Tools, copyright Pearson Education, all rights reserved. Reprinted with permission.
In 2006, Windows Script Host (WSH) and the Command Prompt shell got a new sibling when Microsoft released a completely new environment called Windows PowerShell. PowerShell has some similarities to the older tools: Like the Command Prompt shell, it lets you type commands interactively as well as put them into script/batch files, and you can easily use pipes and redirection to string files and programs together. Like WSH, it provides a powerful object-oriented programming language for complex scripting.
But PowerShell is a very different, strange animal, and it has powerful features that go way beyond what WSH and the Command Prompt offer. I show you what I mean in the following sections.
An Object-Oriented Command Shell
Here’s one way that that PowerShell is different and strange. Recall that if you type dir
into a regular Command Prompt window, the dir
command spits out a bunch of text onto the screen, giving the names, sizes, and timestamps of the files in a directory. You can direct that text into a file using the >
redirection operator, or you can “pipe” the text to another program using the |
operator. For example, the command
dir /s /b | sort
lists the files in the current directory and its subdirectories as a stream of text, and the text is sent to the sort program that sorts them alphabetically. If you’ve used a command-line environment in nearly any computer operating system in the last, say, 30 years, this should be familiar.
Windows PowerShell command lines appear to work in the same way, but its commands deal with objects rather than just text -- objects that represent files, folders, Windows device drivers, network services, documents any of the hundreds of objects defined in the .NET Framework library). What flows from one command to the next, via that pipe symbol (|
), is a stream of objects. The various PowerShell commands let you generate, manipulate, invoke methods on, change properties of, and extract information from these objects.
As an example, here’s what happens when you type dir
in a PowerShell window. The PowerShell dir
command is actually an alias (shorthand) for the Get-ChildItem
cmdlet. You can type either name and get the same result.
Wait, cmd-what? Cmdlet, pronounced “command-let,” as in “cute tiny little command.” PowerShell’s built-in commands are called cmdlets. Maybe they were out of vowels up in Redmond that year? No, the reason for the odd name is that they’re not completely built in to PowerShell, like cmd.exe’s commands, nor are they really completely independent of it, like exe files. They’re implemented in a new way, so they needed a new word. In any case. . .