Out-Gridview is in my opinion one of the most practical PowerShell cmdlets. As all PS cmdlets, it has the same verb-noun syntax: do this (verb) with that (noun). Out-GridView simply tells PS to send command output to a grid view table.
The power of Out-GridView cmdlet comes from its optional -PassThru switch, which lets user to select items from table, then passing them as parameters to another cmdlet. Out-GridView without -PassThru only lists command output in a table.
The syntax with -PassThru switch is as follows:
first cmdlet | Out-GridView -PassThru | another cmdlet
In this tutorial, I will show just a few practical examples about using Out-GridView with -PassThru, to let you see and understand how it works. See official Out-GridView documentation on Microsoft Docs for more information.
Some practical examples
1. Hyper-V: | Start and stop VMs using Out-GridView |
2. Files: | Copy, move & delete files using Out-GridView |
3. Windows Features: | Add or remove optional features using Out-GridView |
Start and stop VMs using Out-GridView
1.1 Cmdlets used in following examples:
- Get-VM Lists all Hyper-V virtual machines
- Start-VM Starts selected VM
- Stop-VM Stops selected VM
Get-VM | Out-GridView -PassThru | Start-VM
When command is executed (#1 in screenshot), grid view table shows all virtual machines. To select VMs I want to start, I hold down CTRL key and select them from the list (#2), then click OK to start selected VMs (#3):
(Click to enlarge.)
Hyper-V Manager will now show that both selected virtual machines are running. You can also see the status with Out-GridView with this simple command:
Get-VM | Out-GridView
(Click to enlarge.)
1.3 OK, now I want to stop one or more running virtual machines. The procedure is exactly the same than when starting them, I just need to change the last cmdlet from Start-VM to Stop-VM:
Get-VM | Out-GridView -PassThru | Stop-VM
Copy, move & delete files using Out-GridView
2.1 Cmdlets used in following examples:
- Get-ChildItem Lists all files and subfolders in selected folder
- Copy-Item Copies selected items to new location
- Move-Item Moves selected items to new location
- Remove-Item Delete a file or a folder
Get-ChildItem C:\Users\YourUsername\OneDrive\Documents | Out-GridView -PassThru | Copy-Item -Destination F:\OneDriveDocs
See step 1.2 for how to select items.
2.2 To move files, change the last cmdlet to Move-Item. Following command would move selected items in your OneDrive\Documents folder to folder F:\OneDriveDocs:
Get-ChildItem C:\Users\YourUsername\OneDrive\Documents | Out-GridView -PassThru | Move-Item -Destination F:\OneDriveDocs
2.3 To delete files, change the last cmdlet to Remove-Item. Following command would delete selected items from your OneDrive\Documents folder:
Get-ChildItem C:\Users\YourUsername\OneDrive\Documents | Out-GridView -PassThru | Remove-Item
2.3 Using -Recurse switch with Get-ChildItem lists all chosen items in given folder and all its subfoders. Following command would list all files not only in OneDrive\Documents folder, but also all files in any existing subfolder of Documents, let you select the files to be deleted, and then delete them:
Get-ChildItem C:\Users\YourUsername\OneDrive\Documents -Recurse | Out-GridView -PassThru | Remove-Item
2.4 With -Recurse switch, we can also use -Include switch to narrow down the list with wildcards. Following command would list all ISO images (extension .iso) on drive G: and all its subfolders, show them in table, let user to select ISO images to be deleted, then delete them:
Get-ChildItem G:\ -Recurse -Include *.iso | Out-GridView -PassThru | Remove-Item
2.5 To list all JPG and PNG images on folder D:\HolidayPics and all its subfolders, and copy selected items to folder X:\Backup\HolidayPics:
Get-ChildItem D:\HolidayPics -Recurse -Include *.jpg, *.png | Out-GridView -PassThru | Copy-Item -Destination X:\Backup\HolidayPics
Notice that as I wanted to list multiple file types, I separated them with a comma (-Include *.jpg, *.png).
Add or remove optional features using Out-GridView
3.1 Cmdlets used in following examples:
- Get-WindowsOptionalFeature Lists all available optional features
- Enable-WindowsOptionalFeature Enables selected optional features
- Disable-WindowsOptionalFeature Disables selected optional features
3.2 To list currently disabled Windows optional features, and select features to be enabled:
Get-WindowsOptionalFeature -OnLine | Where-Object {$_.State -eq "Disabled"} | Out-GridView -PassThru | Enable-WindowsOptionalFeature
3.3 To list currently enabled Windows optional features, and select features to be disabled:
Get-WindowsOptionalFeature -OnLine | Where-Object {$_.State -eq "Enabled"} | Out-GridView -PassThru | Disable-WindowsOptionalFeature
The few examples shown in this tutorial show you how to use Out-GridView. When used without switches, it lists selected command's output for viewing. When used with -PassThru switch, it passes selected output from first cmdlet as parameters to second cmdlet.
Easy, and practical!
Kari