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.
- By Jeffery Hicks
- 01/08/2013
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).
|
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.
|
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"
|
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 an IT veteran with over 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 is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.