Prof. Powershell

Fun with PowerShell's Out-GridView

PowerShell 3 stretches out the features of older cmdlets. Out-Gridview is one example, which I show off in this week's tip.

Even though Windows PowerShell is primarily a console-based management engine, there are a few GUI bits you might want to take advantage of. One of them, which we've had since PowerShell 2 is Out-Gridview. As the name implies, this is a cmdlet that you pipe objects "out" to:

PS C:\> get-process | out-gridview

What you get is a graphical display of the default view (see Fig. 1).

Default grid view.

Figure 1. Default grid view; in PowerShell 2, it's all you get. (Click image to view larger version.)

You can sort on column headings or do additional filtering. In PowerShell 2 that was about it. However, the version that ships with PowerShell 3 has some nice enhancements you might want to take advantage of.

The first is the ability to pass an object back to the pipeline. I can run a command like this:

PS C:\> dir c:\work\*.ps1 | out-gridview -Title "Work Scripts" -passthru

which gives me Figure 2.

PowerShell takes the gridview a bit farther.

Figure 2. PowerShell 3 takes the gridview a bit farther. (Click image to view larger version.)

Notice I now have OK and Cancel buttons. If I select an item and click OK, the underlying object is passed back to the pipeline:

PS C:\> dir c:\work\*.ps1 | out-gridview -Title "Work Scripts" -passthru

   Directory: C:\work

Mode       LastWriteTime    Length  Name
----       -------------    ------  ----
-a---   3/7/2012 2:32 PM      3152  Add-VMtoInventory.ps1

What this means is that I could run a command like this:

PS C:\> dir c:\work\*.ps1 | out-gridview -Title "Work Scripts" -passthru | foreach { ise $_.fullname}

Now, the script I select will be opened in the PowerShell ISE.

What if you want to pass multiple items back? Easy, just select while holding the Ctrl key:

PS C:\> get-eventlog -list | out-gridview -title "Select one or more event logs"

Select more by holding down Ctrl and the items you require.

Figure 3. Select more by holding down Ctrl and the items you require. (Click image to view larger version.)

PS C:\> get-eventlog -list | out-gridview -title "Select one or more event logs" -PassThru

Max(K)  Retain     OverflowAction  Entries  Log
------  ------     --------------  -------  ---
20,480       0  OverwriteAsNeeded   19,661  Application
16,384       0  OverwriteAsNeeded    9,193  System

If you want to only allow a single selection, then use a command like this:

PS C:\> get-eventlog -list | out-gridview -title "Select one or more event logs" -OutputMode Single

The value of "Multiple" is the same thing as using -Passthru. Let's put this all together. Here are the commands:

PS C:\> $logs = get-eventlog -list | select -expand log | out-gridview -Title "Select on or more event logs" -PassThru
PS C:\> $count = 5,15,20,25 | out-gridview -title "How many entries?" -OutputMode Single
PS C:\> $logs | foreach { $log = $_ ; Get-Eventlog $log -Newest $count | select @{Name="Log";Expression={$log}},TimeGenerated,EntryType,Source,EventID,Message } | Out-GridView -Title "Event Log Report"

I'll let you run them to see the results in Out-GridView and to discover for yourself just how much fun you can have with PowerShell 3.

About the Author

Jeffery Hicks is a multi-year Microsoft MVP in Windows PowerShell, Microsoft Certified Professional and an IT veteran with almost 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff is a regular contributor to a variety on online sites, as well as frequent speaker at technology conferences and user groups. Keep up with Jeff and his projects at http://jdhitsolutions.com/blog.

comments powered by Disqus

MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.