Out-GridView

Exploring Out-GridView – An Interactive Table for PowerShell Output

Welcome back to Wahman’s PowerShell Blog! Today, we’re going to explore an incredibly useful cmdlet that many overlook when automating scripts with PowerShell: Out-GridView.

According to Microsoft, Out-GridView “sends output to an interactive table in a separate window.” In practical terms, it’s an excellent way to visually inspect data, offer GUI-based selection options, and stream-line user interactions without needing full forms or complex UI programming.

Available on Windows machines with the Windows PowerShell ISE or with the Windows Presentation Framework installed, Out-GridView opens up dynamic, filterable, scrollable, and selectable data grids. Let’s dive into four practical examples from beginner to advanced usage!

Example 1: Viewing Output as a Table (Beginner)

Let’s start with a simple example. Suppose you want to list all services running on your system. Rather than scrolling through endless text output, display it in a sortable, filterable UI:

Get-Service | Out-GridView

This opens a new window showing all services with columns like Name, DisplayName, and Status. You can easily sort or filter to find exactly what you need.

Example 2: Letting the User Select Items (Intermediate)

Out-GridView becomes interactive when you add the -PassThru parameter, allowing users to select one or more items and passing them to the next command:

Get-Process | Out-GridView -PassThru | Stop-Process

This presents a list of running processes. The user can select one or several processes, and PowerShell will stop them. Handy for managing rogue applications!

Example 3: Filtering a Custom Object Collection (Intermediate)

Create custom objects and use Out-GridView to inspect or filter them. Maybe you have a CSV with hardware inventory data:

$inventory = Import-Csv "C:\data\hardware.csv"
$inventory | Out-GridView

This gives a visual overview of the imported data, much like opening it in Excel, but without leaving PowerShell.

Example 4: Use as a Selection Menu in Scripts (Advanced)

You can integrate Out-GridView into scripts to provide GUI-based choices. For instance, picking which servers to run a command on:

$servers = @('Server1','Server2','Server3','Server4') | ForEach-Object {
    [PSCustomObject]@{ ServerName = $_ }
}

$selectedServers = $servers | Out-GridView -Title 'Select servers to update' -PassThru

foreach ($server in $selectedServers) {
    Invoke-Command -ComputerName $server.ServerName -ScriptBlock {
        Get-WindowsUpdate
    }
}

This snippet creates a simple grid where an admin picks which servers to update. Clean, efficient, and powerful!

Final Thoughts

Out-GridView is a brilliant way to add an interactive edge to your PowerShell scripts by combining the power of scripting with GUI functionality. Whether reviewing data, letting users select input, or extending a tool’s usability, it greatly enhances script dynamics.

Happy scripting, and I will see you in the next post!

Leave a Reply

Your email address will not be published. Required fields are marked *