
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Out-GridView in PowerShell
Description
Out-GridView in a PowerShell is the output format in the GUI format. Generally, we get the output on the console using the Format-Table or Format-List command. Similarly, Out-GridView is also the output format but we can interact with it because of the GUI format.
Moreover, it provides us options to select single or multiple rows and we can store the selected output and can utilize them in the script.
Out-Gridview with Pipeline
You can pipeline output to the Gridview in a similar way as Format-Table or Format-List commands.
Example1 − With Pipeline output
Get-Service | where{$_.StartType -eq 'Disabled'} | Out-GridView
Output
The above command will get all the windows services that are disabled with the custom title “Disabled Services”.
Example2 − Filter Out-Gridview output
You can also add a filter to the output as shown below. You can apply multiple filters based on your requirement.
Example3 − Selecting output from the Out-Gridview
Sometimes in a script, you need to make a selection from the output and Out-GridView can provide that functionality using the OutputMode parameter.
Single Output Mode
You can select a single row from the output by providing a “Single” value to the OutputMode parameter.
Example
Get-Service | where{$_.StartType -eq 'Disabled'} | Out-GridView -Title "Disabled Services" -OutputMode Single
Output
Select the single line and click ok.
You can store the output in a variable and use in PowerShell.
$ser = Get-Service | where{$_.StartType -eq 'Disabled'} | Out-GridView -Title "Disabled Services" -OutputMode Single $ser.DisplayName
Multiple Output Mode.
When you use the Multiple instead of the single output mode, you can have multiple selections and you can store them into a variable.
$services = Get-Service | where{$_.StartType -eq 'Disabled'} | Out-GridView - Title "Disabled Services" -OutputMode Multiple Write-Output "Services" $services.Name
Using PassThru.
Like multiple output mode, -PassThru parameter allows you to interact with the single or multiple outputs.
Example
Get-Service | where{$_.StartType -eq 'Disabled'} | Out-GridView -Title "Disabled Services" -PassThru
Output
Here, we are selecting the first 3 rows and the output would be,
Example4 − Using Out-GridView as a function.
You can use the Out-GridView cmdlet as a function. For example, in the above case, we can make selected services in the manual state as shown below.
Get-Service | where{$_.StartType -eq 'Disabled'} |` Out-GridView -Title "Disabled Services" -PassThru | ` Set-Service -StartupType Manual -PassThru