Utilizing PowerShell Out-GridView as a GUI Alternative

In my previous post, I showed how you can build a simple menu driven PowerShell GUI; check it out here. To further improve upon that simple GUI, we will go over how to use Out-GridView. Out-GridView is a built-in powershell cmdlet that sends the output from a given command to an interactive window. This post will discuss several different use cases and how you can incorporate them into your code.

Why use Out-GridView?
  • Simplicity
  • Built-in to PowerShell
  • Customizable
  • Powerful

Parameters

Let’s first start off by going over the parameters that will be covered in this post and the values that can be used.

  • OutputMode
    • Single: This limits your selection to one object; use this if the next command can only take one value as input.
    • Multiple: This allows you to select multiple objects at a time; use this if the next command can handle multiple values as input.
    • None: Nothing will be able to be selected, use this if you just want a report. This is the default value.
  • Title
    • This is where you put your descriptive name for the Out-GridView window. Any additional instructions should be placed here as well.

Code Examples

GitHub

There are several ways of getting data into Out-GridView, as well as multiple ways of outputting data from Out-GridView. We will cover the methods that I utilize most frequently.

Method 1: Input directly from command, display results only

The most basic function of Out-GridView is to just simply display data to the user. In the below example, we do a Get-Service command to get all the services on a given system, then we pipe those results to Out-GridView to be viewed. We have set a title for the Out-GridView window and set the outputmode to None which means that there is no “OK” or “Cancel” button and no data will be passed along to the next command.

Get-Service | Out-GridView -Title "List of all the services on this system" -OutputMode None
Method 2: Input from variable, single output

A slightly more advanced option will use a variable to hold all of the output from the Get-Service command and then we will pipe that variable to Out-GridView. This gives us more flexibility and the ability to reuse the variable in other parts of the script if needed. We set the title for the Out-GridView window again, but this time we set the outputmode to Single which means that there will be an “OK” and “Cancel” button while only allowing the user to select one value. That one value selected will be stored in the variable specified ($single_output). In this example we utilize the Stop-Service command to stop the selected service.

$services = Get-Service ### Getting all services on the system and sending them to the services variable
$single_output = $services | Out-GridView -Title "Select the service that you want to stop" -OutputMode Single ### Sending the services variable to Out-GridView, the single service selected will be sent to single_output variable

Write-Host "Stopping service" $single_output.Name -BackgroundColor Cyan -ForegroundColor Black
Stop-Service $single_output.Name ### Finally the service selected will be stopped

Selecting a single service and then clicking the “OK” button will cause the selected service to be stopped.

Method 3: Input from custom array, multiple output

An even slightly more advanced option will use a custom array to hold all of the data from the services variable. The services variable will be populated from the Get-Service command and then piped to Where-Object which will only include services with a “Running” status. The array will be populated with all of the Running services from the Foreach loop. The custom array will then be piped to Out-GridView. The custom array is not entirely necessary for this simple example of stopping services but it could be useful with more complex scripts where you need to combine data from multiple sources. We set the title for the Out-GridView window again, but this time we set the outputmode to Multiple which means that there will be an “OK” and “Cancel” button while allowing the user to select multiple values. Multiple values can be selected by holding CTRL while clicking on the desired values. All the values can be selected by hitting CTRL + A. The values selected will be stored in the variable specified ($multiple_output). In this example, we utilize the Stop-Service command inside of a Foreach loop in order to stop the multiple selected services.

$custom_array = @() ### Creating an empty array to populate data in
$services = Get-Service | Select Name, Status, DisplayName, StartType | Where-Object {$_.Status -eq "Running"} ### Getting Running services on the system and sending them to the services variable

### Looping through all of the running services and adding them to the custom array
Foreach ($service in $services){ 

    ### Setting up custom array utilizing a PSObject
    $custom_array += New-Object PSObject -Property @{
    Service_Name = $service.Name
    Service_Status = $service.Status
    Service_DisplayName = $service.DisplayName
    Service_StartType = $service.StartType
    }
}

$multiple_output = $custom_array | Out-GridView -Title "This is using a custom array with multiple output values" -OutputMode Multiple

### Looping through all of the selected services and stopping the service
Foreach ($output in $multiple_output){

    Write-Host "Stopping service" $output.Service_Name -BackgroundColor Cyan -ForegroundColor Black
    Stop-Service $output.Service_Name ### Finally the service selected will be stopped

}

Selecting multiple services (Hold CTRL while clicking desired service) and then clicking the “OK” button will cause the selected services to be stopped. (CTRL + A to select all)

That’s it for now, thanks for reading!


Check out my other blog posts:

2 thoughts on “Utilizing PowerShell Out-GridView as a GUI Alternative

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s