Chris Umbel

PowerShell 2.0 Out-GridView, ISE and ScriptCmdlets

In my last post I discussed the background-job and remoting systems of PowerShell 2.0. While I find those features interesting personally there are three more that I'd like to discuss that have a broader appeal: the Out-GridView CmdLet, ScriptCmdlets and the PowerShell Integrated Scripting Environment (ISE).

Note that PowerShell 2.0 is in CTP 3 at the time of writing and everything is subject to change.

Out-GridView

The Out-GridView cmdlet gives you a lightwieight sortable/searchable grid that you can easily pipe collections into as demonstrated below.

ls | Out-GridView

ScriptCmdlets

Previously the only way to develop your own Cmdlets was to resort to one of the higher level .Net languages such as VB.Net and C#. This restriction has been removed with the introduction of ScriptCmdlets.

Consider the following ScriptCmdLet that retrieves a user's Twitter status:

Cmdlet Get-UsersStatus
{
    # definition of the Cmdlet's parameters
     param([string] $username)
    
    $url = ("http://twitter.com/users/{0}.xml" -f $username)
    
    $webClient =  New-Object Net.WebClient
    $responseDoc = New-Object Xml.XmlDocument
    
    $responseDoc.LoadXml([Text.Encoding]::ASCII.GetString(
      $webClient.DownloadData($url)))
      
    $responseDoc.SelectSingleNode("/user/status/text")   
}

Which can now be executed Cmdlet style:

Get-UsersStatus "chrisumbel"

With a minor change to our param definition we can now accept input from the pipeline:

param([ValueFromPipeline][string] $username)

So our Cmdlet can be executes as such:

"chrisumbel", "wimbledon" | Get-UsersStatus

which gets the status all the users piped into it, chrisumbel and wimbledon in this case.

An interesting thing to consider is that ScriptCmdlets can override other Cmdlets, including those that are built-in.

PowerShell ISE

A weakness of PowerShell up to this point is that you typically had to resort to a third-party solution to get a rich development expirience. While the PowerShell ISE is certainly not a replacement for some of the fancy commercial offerings it's far more helpful than notepad. It grants the scripter syntax highlighting, one-click script running and a tabbed environment for working with multiple scripts.

Check out the following screenshot which contains the code from our Twitter status example:

Conclusion

These features go a long way to making a powerful shell environment even more powerful, not to mention quite a bit more friendly. Here's looking forward to the official release!

Sun Jul 05 2009 17:07:21 GMT+0000 (UTC)

Follow Chris
RSS Feed
Twitter
Facebook
CodePlex
github
LinkedIn
Google