Geeks With Blogs
Caffeinated Coder A Grande, Triple Shot, Non-Fat Core Dump by Russell Ball

I've been spelunking Powershell lately with Bruce Payette's excellent book, Powershell in Action, and thought I would take a step back from some of the lower level nuances, such as dynamic type conversion and parameter binding rules, and organize my thoughts at a higher level.

What is Powershell? - It is the new command line/scripting environment from Microsoft that replaces cmd.exe and WSH. You can install it as a small, free OS update on Windows XP, Windows 2003, and Vista as long as you have the 2.0 framework installed.

Why is it Significant? - Microsoft has traditionally been focused much more on the GUI portion of the OS, which made creating administrative tasks much more difficult in Windows than it typically is from non-MS shells like Bash. This new version of the shell not only addresses these shortcomings in functionality, but significantly raises the bar for other shells by being object-based rather than string based. This eliminates most of the text parsing craziness that is usually required in order to consume command line output. Powershell also achieves an enormous amount of functional coverage by exposing all of the major MS API's, including XML, .NET,COM, ADO, WMI, and ADSI.

Some Basic Concepts

  • Cmdlets - Commands that follow the verb-noun pattern, such as Get-Process or Get-Date. These are actually .net classes that inherit from the base Cmdlet class and can take traditional switches or parameters with arguments. The environment comes with a number of great built in commands, but you can supplement these by installing the Powershell Community Extensions from Codeplex or creating your own that will get automatically loaded into the environment when the shell opens. You can view the cmdlets available on your system by calling Get-Command and then using Get-Help to find out more information about a particular cmdlet.
  • Pipeline - The pipe operator ( | ) allows you to chain together commands in a very concise way by taking the output from the command on the left side of the pipe operator and feeding it as input to the command on the right-side of the operator. This is where the object based paradigm really shines because it allows you to access specific information in the pipeline through properties instead of regular expressions and hard-coded column indexes.
  • Expansive Syntax- Powershell makes extensive use of aliases and pattern matching to ease the transition from cmd.exe and to allow for the terse, write-only experience from the command line that maximizes productivity when producing throw away code. For example, you can use the familiar dir instead of Get-Children when navigating the file system or the shortened alias gps instead of Get-Process when retrieving processes. You can get a list of built-in aliases using Get-Alias and even create your own using Set-Alias.
  • Provider Stores - These expose various data stores such as the registry, certs, and environmental variables as drives, thus allowing you to navigate a registry hive by simply typing cd hklm:. Open source projects have been created to expose VSS, SQL, and Exchange as providers. Use Get-PSDrive to enumerate the available drives.

A Quick Sample Script (Query top 5 CPU consuming process on a machine)

get-process | sort-object -desc CPU | select-object -first 5 processName, cpu | format-table -auto

Now a little more concisely with aliases

gps | sort -desc CPU | select -first 5 processName, cpu | ft -auto

If you want a good 20 minute introduction to the topic, I recommend listening to this old HanselMinutes podcast. It's over a year old, so the podcast uses the old code-name Monad, but it provides a great high level overview. If you enjoy it, check out later episodes where he interviews Bruce Payette, the dev lead on the Powershell team, and Jeffrey Snover, the architect.

Posted on Tuesday, July 3, 2007 1:23 PM Technical Overviews | Back to top

Related Posts on Geeks With Blogs Matching Categories

Comments on this post: Powershell from 50,000 feet

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Russell Ball | Powered by: GeeksWithBlogs.net