PowerShell is Microsoft's administrative scripting tool incorporated into Windows Server 2008. Windows administrators can learn PowerShell to script common management tasks. PowerShell is a download that is available for Windows XP, Server 2003, and Vista; it is included within Windows Server 2008 as a feature you add to the base OS. Under development for several years, the first public view of the product was at the Professional Developer's conference in September 2003. The first release (PowerShell on Windows XP and Windows 2003 Server) occurred in November 2006, with versions for Vista and Longhorn server coming during 2007.
PowerShell is a command shell, similar to Unix shells like C-shell and Bourne shell or Microsoft's CMD.EXE shell, focused on the administrator (as opposed to a programmer). With PowerShell, an administrator can enter interactive commands as well and run more detailed scripts. Scripts can take parameters and produce output in a variety of forms.
PowerShell has the composability of the best versions of Unix/Linux shells, with the programming power of Ruby or Perl. Finally, it's built on top of the .NET framework. This means you can access just about any .NET feature directly from PowerShell, as well as accessing COM and WMI objects.
In this chapter, we will first look at the background to PowerShell and why Microsoft developed it. Next, we'll examine how to install it, and use it both at the command line and as a scripting tool. Finally, we'll look at how you can extend it for your own use.
Before delving into PowerShell itself, it's important to understand the reasons it was developed as well as the approaches the PowerShell development team took to the product. Ever since Microsoft got into the software business, administrators have been managing client and server systems using a huge array of semi ad-hoc tools. These include console tools like CMD.EXE and a wealth of command-line tools. In addition, there were GUI tools such as the Microsoft Management Console, Regedit.exe, Adsiedit, and LDP.
The problem was that those tools did not do "everything"—thus there was occasionally a need to use VBScript or even native APIs and C#/C++. Nor did these tools integrate well—there was no way to use the output of, say, LDP as input to Regedit. Since there was no single tool that did everything, administrators ended up needing to use a variety of disparate tools to solve administrative issues.
These individual tools were totally adequate for their original purpose. However, the scope and capability of each tool was not consistent. Each tool did only part of the task and provided little integration with other tools. As many admins discovered, they would start down one path, such as writing a batch script using OS provided tools, only to find that the tools don't quite do what was needed. This meant moving to a different tool (e.g., using VBScript and COM automation) and throwing away some or all of their earlier work.
With the more recent releases of Windows (e.g., Windows XP, Windows Server 2003, and now Windows Server 2008), Microsoft has made great strides, particularly with the command-line tools. While the coverage is greatly improved, more is needed. Microsoft's answer to these concerns is PowerShell.
There are three key aspects of PowerShell that are of interest to an admin:
It is focused on the administrator.
It is broad in scope and completeness.
It is highly extensible.
You can directly call into .NET, WMI, and COM to work with existing code. You can access just about any sort of data, and this includes PowerShell's native support for XML. And if that's not enough, you can write your own extensions—for example, to make PowerShell work with a custom-developed line of business applications.