imageWith PowerShell 2.0 there are some nice GUI features to help leverage PowerShell in some new and interesting ways.  In yesterday’s post Sarah showed you the ISE works.  In this post I want to take a quick look at another GUI feature of PowerShell, the cmdlet, Out-GridView.  This cmdlet allows you to take the output from your PowerShell commands to a explorer style window.    This is handy cmdlet if your looking to just view and quickly manipulate data from a PowerShell command.

Before I get started with taking a look at the cmdlet, I need mention a special character you may have noticed in a few posts and examples.  The  pipe operator “|”, enables functionality called pipelining.  Pipelining essentially takes the output from one command and passes it along to the next command where it becomes input for the next command in the string.  The pipe operator “|” allows to string together multiple commands, and it routes information to the correct parameters – with very little effort.  A simple example of a pipeline command would be as follow

Get-Service | Where-Object {$_.Status -eq "Stopped"}

The purpose of the above command should be fairly obvious.  What happens under the covers as the command executes is it gets a service checks the status and moves on to the next service until it ends.  It is that simple.  You can even chain multiple commands together like this example, which sorts, then formats the table to only include the parameters I am interested in:

Get-Service | Sort-Object -property status | Format-Table displayname, status, requiredservices

You will see the “|” throughout the next several posts and it is key to using Out-GridView.   One of the primarily uses of the pipe operator “|” is to filter, sort and/or control the output of data, like the example above.   With Out-GridView you have a great way to work with the output of data.  Traditionally if PowerShell was used to output data to a file in a temporary fashion, to get a daily report.  You would read the report, then usually delete or archive it.  You may also want to dig into the report to get even more information.  Out-GridView makes working with data very easy.  Before you can use Out-GridView you have to have the .NET Framework 3.0 installed otherwise the cmdlet will not work.  So how does it work? Very simple:

imageGet-Service | Out-GridView

As you can see in the screenshot you get a GUI window with the output from Get-Service.  You can click on any of the column headings to very easily sort the data.  Will also notice you have a filtering line, which will filter your output as you type.  You can even fine tune your filtering by clicking the Add Criteria button and specify a property in your output and can filter with contains, equals, is empty…etc.  As you can see the Out-GridView presents the data in a very useful fashion. 

The last factoid I want to share in regards to Out-GridView is how to include and control the properties in the Grid View window.  When you get results from the commands you are generally only seeing a subset of the properties available for the cmdlet.  As you saw in the earlier example you can use a cmdlet like Format-Table (or even Format-List) to control what properties you wish to see in your output, which is extremely useful.  However, Format-Table and Format-List are not supported by Out-GridView, so the following example would produce an error (along the lines of Data format not supported in Out-GridView):

Get-Service | Format-Table displayname, status, requiredservices |Out-GridView

If you wanted to be selective the properties displayed in Out-GridView you would need to use Select-Object in place of Format-Table.  To make the example work your command would look something like this:

Get-Service | Select-Object displayname, status, requiredservices |Out-GridView

BTW, if you wanted to get all of the properties you could use the * and the command would look something like this:

Get-Service | Select-Object * | Out-GridView

Thanks for reading and if you missed any of the previous posts you can find a master list of series postings located here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell or on Sarah’s blog here: PowerShell Not Your Father's Command Line: 31 Days of PowerShell. Lastly Sarah and I want to hear from you email either of us with your comments or suggestions for future postings let us know, we look forward to hearing from you. Have a great day!