Out-GridView can serve as a universal selection dialog when you add the -PassThru parameter. The one-liner below stops all services you select in the grid view window (well, not really; you can play safely until you remove -WhatIf):

Get-Service | Out-GridView -Title 'Select Service' -PassThru | Stop-Service -WhatIf  

However, there is a long-standing bug in Out-GridView: while information is pumped into the grid view window, the buttons to select items are enabled but not returning anything. Here is a test-case: I am filling the grid view window with all files in the Windows folder that are larger than 10MB:

Get-ChildItem -Path C:\Windows -Recurse -File -ErrorAction SilentlyContinue |
    Where-Object Length -gt 10MB |
    Out-GridView -Title 'Select a file' -PassThru 

Enumerating the files can take some time, yet because of the beautiful real-time nature you see the files listed in the grid view window as they arrive, and you can select some and click the OK button the lower right corner to return them to the console.

Note: If you click the OK button before all files have been sent to the grid view window, the grid view window closes but returns nothing. For OK to work you must know when the grid view window output is complete.

Except, there is no way for you to know. You can wait a while and hope for the best but there is no clue telling you that the grid view window is completely filled.

One workaround is to store the data in a variable first, then send it rapidly to the grid view window:

$files = Get-ChildItem -Path C:\Windows -Recurse -File -ErrorAction SilentlyContinue |
    Where-Object Length -gt 10MB 

$files | Out-GridView -Title 'Select a file' -PassThru

You are effectively losing the real-time effect though and will likely have to wait for many seconds before the data is collected and the grid view window opens.

A smarter way makes use of PowerShell’s pipeline architecture and uses a pipeline-aware function. Its “end” block is called when all pipeline processing is done, so you can place code there to give a clue that all data is complete:

function Send-PipelineEndNotification
    begin {
        Write-Host "Collecting Data..." -NoNewline -ForegroundColor DarkYellow
    process { $_ }
    end {
        Write-Host "Completed." -ForegroundColor Green

Get-ChildItem -Path C:\Windows -Recurse -File -ErrorAction SilentlyContinue |
    Where-Object Length -gt 10MB |
    Send-PipelineEndNotification |
    Out-GridView -Title 'Select a file' -PassThru 

Simply call Send-PipelineEndNotification before Out-GridView. In the console, you now see a warning that information is still being collected, and a green notification text and beep when the grid view window is complete and ready to return selected items.

PowerShell Conference Europe (psconf.eu) opens June 2, 2020, in Hannover, Germany, and you can be part of it! 4 days, 3 tracks, 80 PowerShell sessions, and 40 renown speakers from around the world (including PowerShell inventor Jeffrey Snover, the PowerShell team with Steve Lee, the Amazon AWS team, and many more) are waiting for questions and discussions, providing authoritative firsthand information, tips and guidance for professional PowerShell scripters.

Find out more at http://powershell.one/psconfeu/psconf.eu-2020/about, download the mobile app with sessions and speakers at http://psconfeu.sessionize.com/, and secure your seat at https://psconf.eu/register.html.

Twitter This Tip! ReTweet this Tip!