Out-GridView Grows Up

by Aug 21, 2012

In PowerShell v2, Out-GridView was a useful way of displaying information in a separate window but its functionality was limited because you could not return any data (note that Out-GridView is available on servers only when the PowerShell ISE script editor feature is installed).

Get-Service | Out-GridView

In PowerShell v3, three new parameters have been added. The parameter -Wait will show the gridview window modal, so the script pauses until the window is closed.

The parameter -PassThru will allow the user to send back his selection to the script. So now, Out-GridView can work as a selector. Try this:

Get-Service | Out-GridView -PassThru

A gridview with all services opens. In the filter text box at its top, enter "network" (without the quotes). Only services with the keyword "network" are shown. Select them all, and then click OK in the bottom right corner. Your script receives the selected services.

By default, multi selection is enabled. You can use -OutputMode to further control this:

Get-Service | Out-GridView -OutputMode Single

This allows the user to select only one service, for example the one you want to restart:

Get-Service | Out-GridView -Title 'Select Service to Restart!' -OutputMode Single | Restart-Service -WhatIf

Basically, the parameter -PassThru is just a shortcut for -OutputMode Multiple.

Twitter This Tip! ReTweet this Tip!