Use PowerShell Out-GridView as a GUI alternative

Out-GridView is a built-in powershell cmdlet that can send the output of a given command to an interactive window. This article will discuss several different use cases and how to incorporate them into the code.

Why use Out-GridView?

  • simple
  • Built in PowerShell
  • Customizable
  • powerful

Parameter

First, let's start discussing the parameters and the values ​​that can be used in this article.

  • output method
    • Single: This will limit your selection to one object; if the next command can only accept one value as input, use this option.
    • Multiple: This allows you to select multiple objects at once. If the next command can handle multiple values ​​as input, use this option.
    • None: Nothing can be selected. If you only need to report, please use this option. It's the default value.
  • title
    • This is where you put a descriptive name in the Out-GridView window. Any other instructions should also be placed here.

Code example

GitHub

There are several ways to import data into Out-GridView, and many ways to output data from Out-GridView. We will introduce the method I use most often.

Method 1: Input directly from the command and only display the result

The most basic function of Out-GridView is to only display data to the user. In the example below, we execute the Get-Service command to get all the services on a given system, and then pipe these results to Out-GridView for viewing. We have set a title for the Out-GridView window and set the outputmode to None , which means there is no "OK" or "Cancel" button, and no data will be passed 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 save all the output of the Get-Service command, and then we will pipe this variable to Out-GridView. This gives us more flexibility and can reuse the variable in other parts of the script when needed. We set the title for the Out-GridView window again, but this time we set the outputmode to Single , which means there will be an "OK" and "Cancel" button, and only allow the user to select a value. The selected value will be stored in the specified variable ( $ single_output ). In this example, we use 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 clicking the "OK" button will cause the selected service to be stopped.

 

Method 3: Input from a custom array, multiple outputs

A slightly more advanced option will use a custom array to hold all the data from the services variable. The service variable will be populated from the Get-Service command and then piped to the Where-Object, which only contains the services whose status is "running". The array will be filled with all the Running services in the Foreach loop. Then pass the custom array to Out-GridView through the pipeline. For this simple example of stopping the service, a custom array is not entirely necessary, but for more complex scripts (you need to combine data from multiple sources), it can be useful. 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. When clicking the desired value, hold down CTRL to select multiple values. All values ​​can be selected by pressing CTRL + A. The selected value will be stored in the specified variable ( $ multiple_output ). In this example, we use the Stop-Service command in the Foreach loop to stop 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 (press and hold CTRL when clicking the desired service), and then clicking the "OK" button will cause the selected service to stop. (CTRL + A to select all)

 

That's it, thank you for reading!

Guess you like

Origin blog.csdn.net/allway2/article/details/109556096