Use Out-GridView as Output Window

by Jul 1, 2019

Typically, Out-GridView opens a window and displays whatever you pipe into the cmdlet:

 
PS C:\> Get-Service | Out-GridView 
 

However, with a little trick Out-GridView becomes even more versatile. You can pipe information to the same output window whenever you want to.

First, get yourself an instance of Out-GridView that thinks it is running in a pipeline:

$pipeline = { Out-GridView }.GetSteppablePipeline()
$pipeline.Begin($true)

Now, you can output whatever you want by calling “Process”. Each call to Process() is like piping an element to the cmdlet:

$pipeline.Process('Hello this is awesome!')
Start-Sleep -Seconds 4
$pipeline.Process('You can output any time...')

When you are done, call End() to end the pipeline:

$pipeline.End()

This way, you could log information to a grid view or use it as a generic output window that shows results to the user.


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!