Get UI Information for Processes

by Feb 1, 2016

PowerShell can use UIAutomation calls to find out useful UI information about any process. You can find out whether a process accepts keyboard input, whether it is currently visible (and what its window dimensions are), and whether it is a native Win32 application or uses WPF–among other things.

Here is an example that examines two processes: the PowerShell window, and a minimized Notepad:

#requires -Version 2
function Get-ProcessInfoUI($Process)
{
  foreach ($proc in $Process)
  {
    Add-Type -AssemblyName UIAutomationClient
    [System.Windows.Automation.AutomationElement]::FromHandle($proc.MainWindowHandle).Current |
    Select-Object -Property Name, HasKeyboardFocus, IsOffscreen, BoundingRectangle, NativeWindowHandle, ProcessId, Orientation, FrameworkId
  }
}

# get current PowerShell process:
$ise = Get-Process -Id $pid
# start new Notepad and wait until loaded:
$notepad = Start-Process notepad -WindowStyle Minimized -PassThru
$null = $notepad.WaitForInputIdle()

# get UI information for these processes
Get-ProcessInfoUI $ise, $notepad | Out-GridView -Title 'UI Information'

The output is shown in a grid view window but can also be output to your console:

  
Name               : Windows PowerShell ISE-Mainwindow
HasKeyboardFocus   : False
IsOffscreen        : False
BoundingRectangle  : -9;-9;1618;868
NativeWindowHandle : 131848
ProcessId          : 4428
Orientation        : None
FrameworkId        : WPF

Name               : Untitled - Editor
HasKeyboardFocus   : False
IsOffscreen        : False
BoundingRectangle  : Empty
NativeWindowHandle : 593514
ProcessId          : 2924
Orientation        : None
FrameworkId        : Win32 

 

Throughout this month, we’d like to point you to three awesome community-driven global PowerShell events taking place this year:

Europe: April 20-22: 3-day PowerShell Conference EU in Hannover, Germany, with more than 30+ speakers including Jeffrey Snover and Bruce Payette, and 60+ sessions: www.psconf.eu.

Asia: October 21-22: 2-day PowerShell Conference Asia in Singapore. Watch latest announcements at www.psconf.asia

North America: April 4-6: 3-day PowerShell and DevOps Global Summit in Bellevue, WA, USA with 20+ speakers including many PowerShell Team members: https://eventloom.com/event/home/PSNA16

All events have limited seats available so you may want to register early.

Twitter This Tip! ReTweet this Tip!