Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2012
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

October 28, 2021 PowerShellWindows 10Windows Server 2019

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
5
Facebook Twitter Google + Pinterest
previous post
Prevent Users from Changing Proxy Settings in Windows
next post
Fix: Windows Won’t Boot (Start) After Installing Updates

Related Reading

Using Previous Command History in PowerShell Console

January 31, 2023

How to Install the PowerShell Active Directory Module...

January 31, 2023

Enable Internet Explorer (IE) Compatibility Mode in Microsoft...

January 27, 2023

Finding Duplicate E-mail (SMTP) Addresses in Exchange

January 27, 2023

How to Disable or Uninstall Internet Explorer (IE)...

January 26, 2023

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

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMWare
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • Using Previous Command History in PowerShell Console

    January 31, 2023
  • How to Install the PowerShell Active Directory Module and Manage AD?

    January 31, 2023
  • Finding Duplicate E-mail (SMTP) Addresses in Exchange

    January 27, 2023
  • How to Delete Old User Profiles in Windows?

    January 25, 2023
  • How to Install Free VMware Hypervisor (ESXi)?

    January 24, 2023
  • How to Enable TLS 1.2 on Windows?

    January 18, 2023
  • Allow or Prevent Non-Admin Users from Reboot/Shutdown Windows

    January 17, 2023
  • Fix: Can’t Extend Volume in Windows

    January 12, 2023
  • Wi-Fi (Internet) Disconnects After Sleep or Hibernation on Windows 10/11

    January 11, 2023
  • Adding Trusted Root Certificates on Linux

    January 9, 2023

Follow us

woshub.com
  • Facebook
  • Twitter
  • RSS
Popular Posts
  • Configuring Port Forwarding in Windows
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Installing RSAT Administration Tools on Windows 10 and 11
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • Get-ADUser: Find Active Directory User Info with PowerShell
  • How to Hide Installed Programs in Windows 10 and 11?
  • Configuring SFTP (SSH FTP) Server on Windows
Footer Logo

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


Back To Top