PowerShell 3.0 and later
By default, Out-GridView is a one-way road: you can pipe data into it and display it in a grid view window, but you cannot pass the data on.
Once you add the –PassThru switch parameter, this changes, and Out-GridView displays two new buttons in its lower right hand corner: “OK” and “Cancel”. It turns itself into a universal dialog.
Try this:
PS> Get-Service | Where-Object CanStop | Out-GridView -Title 'Stoppable Services' -PassThru
This opens a grid view windows with the title “Stoppable Services”, and lists all services that can be stopped (you still may need Administrator privileges to actually stop them).
You can now select one or many of them (hold CTRL to select many), then click “OK” in the lower right hand corner of the grid view window.
As you discover, the selected objects are returned.
To actually turn this one-liner into a useful tool, you can add cmdlets that pick up what Out-GridView outputs, and actually perform the needed action. This line will indeed try and stop all services that you have selected:
PS> Get-Service | Where-Object CanStop | Out-GridView -Title 'Stoppable Services' -PassThru | Stop-Service -WhatIf
Note that for safety, we added the –WhatIf parameter to Stop-Service, so the cmdlet is just simulating. Once you remove this parameter, the line will no longer pretend, but actually stop services.
Just sit down, have a coffee, and think about it: Out-GridView accepts any kind of data, so you can use it to create just about any tool with it. For example, use the Active Directory cmdlet Get-ADUser to find users that are currently disabled, and then have PowerShell enable all selected users for you.
Or display a list of processes that have a main window (top applications), and kill any selected process.
If you do this, you may want Out-GridView to disallow multiple selections. To allow just a single selection, try this:
PS> 1..10 | Out-GridView -Title 'Pick favorite number' -OutputMode Single