MCPMag.com

Sign up for our newsletter.

I agree to this site's Privacy Policy.

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 Microsoft MVP in Windows PowerShell, Microsoft Certified Trainer and an IT veteran with over 20 years of experience, much of it spent as an IT consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He works today as an independent author, trainer and consultant. Jeff writes the popular Prof. PowerShell column for MPCMag.com and is a regular contributor to the Petri IT Knowledgebase and 4SysOps. If he isn't writing, then he's most likely recording training videos for companies like TrainSignal or hanging out in the forums at PowerShell.org. Jeff's latest books are Learn PowerShell 3 in a Month of Lunches, Learn PowerShell Toolmaking in a Month of Lunches and PowerShell in Depth: An Administrators Guide. You can keep up with Jeff at his blog http://jdhitsolutions.com/blog, on Twitter at twitter.com/jeffhicks and on Google Plus (http:/gplus.to/JeffHicks)

comments powered by Disqus

Reader Comments:

Sat, Jan 25, 2014

http://w DOT ww.healthinsurancebible.com http://www.edmedscosts.com/ DOT http://gethookedmagazine.com/o DOT nline-casino.html http://y DOT ouroptionshere.com http://w DOT ww.autoprotectionoptions.com

Wed, Jan 22, 2014

http://www.mybestinsurancequotes.net/ DOT http://w DOT ww.allstatescarinsurance.com http://w DOT ww.findairflights.com http://w DOT ww.mylifeinsuranceplace.com http://w DOT ww.healthinsurancebible.com http://www.edmedscosts.com/ DOT http://w DOT ww.casinosspot.com

Sat, Jan 18, 2014

http://w DOT ww.getaffordablehealthinsurance.net http://w DOT ww.medicaforyou.net http://w DOT ww.getedtabletsonline.com http://www.buyfirstmedication.com/ DOT http://w DOT ww.medformen.com

Fri, Jan 17, 2014 Nash http://www.forgetyoured.net/ DOT http://w DOT ww.gettopedmeds.com http://www.mycheapinsuranceonline.com/ DOT http://w DOT ww.cheapautoinsurcover.com

http://www.forgetyoured.net/ DOT http://w DOT ww.gettopedmeds.com http://www.mycheapinsuranceonline.com/ DOT http://w DOT ww.cheapautoinsurcover.com http://w DOT ww.insureyourbusiness.net http://y DOT ouroptionshere.com

Sat, Nov 9, 2013 Tori autoisurance quotes

http://www.carinsurquote.net/ DOT

Mon, Sep 30, 2013 Marilu business insurance cashback

http://www.eben-uk.org/ DOT http://www.comparecashbackcards.net/ DOT

Thu, Sep 26, 2013 Charlee credit apr 0%

http://www.lowaprcard.net/ DOT

Mon, Sep 16, 2013 Jane low rate credit cards canada

http://www.lowaprcard.net/ DOT

Sat, Apr 27, 2013 Jenn cheap auto insurance >:OO cialis 063

http://www.compareinsuroffersonline.com/ DOT >:OO http://www.edmedsonline4you.com/ DOT 063

Sat, Apr 27, 2013 Janelle auto insurance quotes =-((( viagra 544

http://www.compareinsuroffersonline.com/ DOT =-((( http://www.viamedstore.com/ DOT 544

Wed, Jan 9, 2013 Rob Campbell

Related: http://gallery.technet.microsoft.com/scriptcenter/Select-FromGridView-521a56d8

Add Your Comment Now:

Your Name:(optional)
Your Email:(optional)
Your Location:(optional)
Comment:
Please type the letters/numbers you see above