Using a Grid View Window as a Selection Dialog (Part 1)

by Jan 17, 2018

How can a grid view window be used as a simple selection dialog?

When you pipe objects to a grid view window, all object properties are shown. Often this works really well, and then just a line like this is needed:

Get-Service | Out-GridView -Title "Select Service" -OutputMode Single

Sometimes, especially when an object has tons of properties, it can be an overkill and confuse the user:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration | 
    Out-GridView -Title "Select Network Card" -OutputMode Single

To simplify the dialog, you can use an approach we used earlier with user profile management, and use a hash table. Simply pick one property that you’d like to use as a key. This property needs to be unique. Next, try this:

Get-WmiObject -Class Win32_NetworkAdapterConfiguration | 
    Out-GridView -Title "Select Network Card" -OutputMode Single

$hashTable = Get-WmiObject -Class Win32_NetworkAdapterConfiguration |
    Group-Object -Property Description -AsHashTable -AsString

$hashTable.Keys |
    Sort-Object |
    Out-GridView -Title "Select Network Card" -OutputMode Single |
    ForEach-Object {
        $hashTable[$_]
    }

As you see, only the selected property is displayed in your grid view window, yet when the user selects an item, the full object is retrieved. This works very similar for the service list:

$hashTable = Get-Service |
    Group-Object -Property DisplayName -AsHashTable -AsString

$hashTable.Keys |
    Sort-Object |
    Out-GridView -Title "Select Service" -OutputMode Single |
    ForEach-Object {
        $hashTable[$_]
    }

Are you an experienced professional PowerShell user? Then learning from default course work isn’t your thing. Consider learning the tricks of the trade from one another! Meet the most creative and sophisticated fellow PowerShellers, along with Microsoft PowerShell team members and PowerShell inventor Jeffrey Snover. Attend this years’ PowerShell Conference EU, taking place April 17-20 in Hanover, Germany, for the leading edge. 35 international top speakers, 80 sessions, and security workshops are waiting for you, including two exciting evening events. The conference is limited to 300 delegates. More details at www.psconf.eu.

Twitter This Tip! ReTweet this Tip!