Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / PowerShell / Using Out-GridView to View and Select Table Data in PowerShell

March 16, 2024

Using Out-GridView to View and Select Table Data in PowerShell

The Out-GridView cmdlet allows displaying data as an interactive graphical table that can be filtered or sorted based on different criteria. You can use the Out-Gridview cmdlet in scripts where you want to provide a user with the simplest GUI to select objects.

In fact, Out-GridView is a wrapper to run .NET DataGridView, a standard graphical form from a Windows Form Control.

Using Out-GridView Tables

Let’s look at the simplest example of using the Out-GridView cmdlet to display a list of Windows services and some of their properties:

Get-Service | Select DisplayName,Status,ServiceName,Can* | Out-GridView

Using Out-GridView tables in PowerShell

As you can see, a graphic table form with the list of Windows service properties appeared. The cmdlet sets column names automatically based on the object properties or data type, and extends PSObject properties if data format cannot be defined.

You can search the form using the Filter box.

search in out-gridview

You can access data in Excel tables directly from PowerShell.

You can also use the Add criteria button to search the table. In the screenshot below, I have created the simplest filter with a list of running services having VMW in their names. The filter is created based on the values of object properties directly.

add a criteria in out-gridview in order to filter results

Or let’s display a list of TOP 10 processes with the highest CPU utilization (I have changed the name of my Out-GridView window using the –Title option):

Get-Process | Sort-Object CPU -Descending | Select -First 10 | Out-GridView -Title "Top 10 CPU processes"

You can quickly sort the table contents in ascending/descending order by clicking the column header.

Powershell Out-GridView sorting

Out-GridView cmdlet with PassThru Switch

However, the most powerful Out-Gridview feature is the –PassThru option, which provides a new level of user-friendly GUI for your PowerShell scripts.

The option is available in PowerShell 3.0 or higher, allows a user to select one or more objects in the table, and passes them using the standard pipe to the next cmdlets in your PowerShell script.

For example, the following PowerShell script displays a list of running Windows services. A user selects a service in the list and clicks OK.

Get-Service | Where-Object {$_.status -eq 'running'}| Out-GridView -Title "Select service to restart" –PassThru -OutputMode Multiple | Restart-service –verbose

The script will restart only the service selected by the user.

PowerShell Out-GridView cmdlet with PassThru Switch

You can save the objects selected by the user into a variable:

$Svcs = Get-Service | Where-Object {$_.status -eq 'running'}| Out-GridView -Title "Select services" –PassThru

Or you can save only the values of a property. To do it, add the following pipe to the previous command:

| Select -ExpandProperty Name

processing Out-GridView output

You can allow a user to select only one item or multiple items in the table using the following options:

-OutputMode Single and -OutputMode Multiple

Press and hold Ctrl to select multiple rows in the table.

How to Use Out-Gridview as a GUI in PowerShell Script?

Here are some more interesting examples of using Out-GridView.

To display a list of previous commands from the PowerShell history and run the selected commands again:

Get-History | Out-GridView -PassThru | Invoke-Expression

To display a list of additional Windows components and install selected (for example, RSAT Active Directory management tools and the SSH client):

Get-WindowsCapability -Online | Where-Object {$_.State –eq “NotPresent”}| Out-GridView -PassThru |Add-WindowsCapability –Online

Select-Object with Out-GridView

To get a list of RDP sessions from your RDS farm’s Connection Broker and connect to the user’s selected desktop using an RDP shadow connection:

import-module remotedesktop
$cbserver = "munrdsbroker1.woshub.com"
$id = get-rdusersession -ConnectionBroker $cbserver | Out-GridView -title "RD Connection" -PassThru | select hostserver, unifiedsessionid
$id2 = $id | select -ExpandProperty unifiedsessionid
$srv = $id | select -ExpandProperty hostserver
mstsc /v:"$srv" /shadow:"$id2" /control /noconsentprompt

You can use the Get-ADUser cmdlet from the AD PowerShell module to display a list of enabled users in a specific OU and reset the user’s domain password:

Import-Module ActiveDirectory
$NewPasswd=Read-Host "Enter a new user password" –AsSecureString
Get-ADUser -filter {Enabled -eq "true"} -properties Name, displayname,EmailAddress,pwdLastSet -SearchBase ‘OU=Berlin,OU=DE,DC=woshub,DC=com’| Out-GridView -PassThru –title “Select a user to reset a password”| Set-ADAccountPassword -NewPassword $NewPasswd -Reset

Using Out-GridView to manage AD users and objects

Using the Invoke-Command, you can get data from remote computers and show them in a table:

Invoke-Command -ComputerName be-dc01, mun-dc01, mun-dc02 -ScriptBlock {Get-Culture} | Select-Object PSComputerName,DisplayName| Out-GridView

Unfortunately, the Out-GridView cmdlet cannot be used in Windows Server Core. If you run it, the following error occurs:

out-gridview : To use the Out-GridView, install Windows PowerShell ISE by using Server Manager, and then restart this application. (Could not load file or assembly 'Microsoft.PowerShell.GraphicalHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=xxxxx' or one of its dependencies. The system cannot find the file specified.)

However, you can use the –ComputerName option many cmdlets have to access Server Core. For example:

Get-Service -ComputerName lon-dc02 | Where-Object {$_.status -eq 'running'}| Out-GridView –Title "Select service to restart" -OutputMode Single|Restart-Service -Verbose

For some reason, Microsoft removed the Out-GridView cmdlet from PowerShell Core 6.x, but returned it in version 7.0. If you are using PowerShell 6.x, update it to the latest version using this command:

iex "& { $(irm https://aka.ms/install-powershell.ps1) } -UseMSI"

As you can see, Out-GridView allows adding a nice graphical interface to your PowerShell scripts.

1 comment
10
Facebook Twitter Google + Pinterest
PowerShellWindows 10Windows Server 2019
previous post
Prevent Users from Changing Proxy Settings in Windows
next post
How to Rename an Active Directory Domain

Related Reading

PowerShell: Get Folder Size on Windows

April 2, 2024

How to Download Offline Installer (APPX/MSIX) for Microsoft...

March 12, 2024

Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)

March 17, 2024

How to Refresh (Update) Group Policy Settings on...

August 13, 2024

How to Backup and Restore Websites and IIS...

June 8, 2023

Start Menu or Taskbar Search Not Working in...

April 22, 2025

How to Run a Scheduled Task After Another...

June 8, 2023

Slow Access to Shared Folders and Network Drives...

March 11, 2024

1 comment

Daniel Liuzzi December 30, 2021 - 12:33 am

This cmdlet is incredibly useful! BTW, its alias “ogv” comes in handy for quickly viewing query results in the CLI, i.e. `ls | ogv`.

Reply

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Install and Manage Windows Updates with PowerShell (PSWindowsUpdate)
  • How to Download Offline Installer (APPX/MSIX) for Microsoft Store App
  • Configuring Port Forwarding in Windows
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • Adding Drivers into VMWare ESXi Installation Image
  • Tracking and Analyzing Remote Desktop Connection Logs in Windows
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top