Take Control of Windows With PowerShell

The traditional Windows command line is really not that different from the DOS prompt that predates Windows by years. The command prompt commands and syntax date back to the 1980s and while a few commands have been added, the options to automate using the traditional command line aren’t much different than the last version of traditional MS-DOS.

While few end users need to access the command line on a regular basis, for admins and power users there is no faster and easier way to accomplish complex or repetitive tasks. Simple tasks could be accomplished, but more complicated and advanced work was difficult or impossible without using third party software. Is PowerShell the answer?

Microsoft realized this deficiency and sought to address it by creating a new scripting language called PowerShell. PowerShell sought to overcome the limitations of the traditional command line and provide a more modern scripting language supporting features such as functions, pipelining of commands (using the output of one command as the input to another), and direct usage of .NET based objects which are integral to modern Windows systems.

The goal was to provide a scripting based language that could do the same actions as more traditional programming languages such as C# and Visual Basic. How well did they succeed?

Getting and Running PowerShell

The PowerShell Prompt

The PowerShell Prompt

You probably already have the current version of PowerShell on your computer. Powershell 2.0 is included in Windows 7 and Windows Server 2008 R2. It is also available as part of the Windows Management Framework for Windows XP, Windows Vista, Windows Server 2003, and Windows Server 2008.

PowerShell dir Command

The traditional command line commands still work under PowerShell

You will find Powershell under the Start Menu at Start -> Accessories -> Windows Powershell -> Windows Powershell. When you click to start PowerShell you will see the PowerShell command prompt appear after a few seconds. It looks similar to the traditional Windows command line.

While there are many changes, Microsoft either supports or simulates the traditional command line commands you’re already familiar with. If you type dir at the prompt you will get a directory listing similar to the one you’d see under the Command Prompt.

The Power of PowerShell

To use the full potential of PowerShell you must learn its new syntax.

To use the full potential of PowerShell you must learn its new syntax. Much of the power of a traditional shell comes from the ability to pass strings from program to program. PowerShell passes actual .NET objects. In both cases you link commands together so the output of one command becomes input to the the next.

With PowerShell you are not just limited to text, but can handle more complex objects. It also means you have the full power of the built in .NET libraries available. This lets you perform tasks with access to the same libraries you would writing in C#. It also means that any library written for use by .NET programs can be accessed from PowerShell.

PowerShell uses four types of commands: cmdlets, functions, scripts, and native programs. A cmdlet is a program specifically written to interact with PowerShell and return an object that another program can process. Scripts are similar to a batch file under the traditional command prompt and consist of a list of PowerShell code that can be executed.

Functions are named blocks of code that can then be called from elsewhere multiple times and are often used within a script. Native programs are traditional Windows programs written without knowing about PowerShell.

The dir command used earlier actually doesn’t exist in PowerShell. It’s an alias, a shortcut to another command. In fact the dir command is an alias, a command redefined to another command. When you type dir, the alias defines the command get-childitem that is actually run. This example is simple, but a command of any complexity can be used as an alias.

What PowerShell Can Do

Let’s look at an example of what PowerShell can do. Here’s a command that displays the titles and and links to recent articles published at Windows.AppStorm.
([xml](new-object net.webclient).DownloadString("http://feeds2.feedburner.com/WindowsAppStorm")).rss.channel.item | format-list title,link

Complex PowerShell Command

PowerShell is capable of advanced tasks such as parsing data.

Briefly this command creates a web connection using a .NET library (new-object net.webclient) and downloads the RSS feed containing the recent articles published to Windows.AppStorm. The returns is a file that contains information about the most recent articles published to the site. We then tell PowerShell to parse the file to get the individual articles (indicated by rss.channel.item).

The output of this is a list of articles we then passe to a program that then displays the title and link attributes of the article information. All that is done in a single line of PowerShell.

Should You Learn PowerShell?

You may wonder why you should care about the command line. Almost everything in Windows can be done with a graphical interface. However there are many things that need to be done regularly or many times. The traditional graphical interface doesn’t scale. A task that takes one minute isn’t a big deal, but if you need to do that task fifty times, then it will always take you fifty minutes to do that.

If you need to do a task repeatedly, every time you log into a corporate network, then taking that minute down to a few seconds will add up to a significant time savings. The command line interface allows you to work faster.

PowerShell is a complex language. Compared to the traditional command line the syntax is quite different. Writing PowerShell scripts is closer to programming in a traditional language than simply stringing commands together. With this complexity comes a lot of power and flexibility.

You can do things in PowerShell that before required writing full programs. If you find yourself often wishing that you could automate a task or need to manage multiple Windows machines, look into PowerShell to ease your work.