Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Powershell Find application window state minimized or maximized

While working on one of the small freelance project there was a requirement to find whether launched application/process is minimized or maximized (For example when I open Microsoft excel what is the state whether it is minimized or maximized). This value of window state you can easily get with UIAutomationClinet .net class object. You can more read about the object on https://docs.microsoft.com/en-us/dotnet/api/system.windows.automation.automationelement?view=netcore-3.1. Below is the screenshot, In the result it shows 3 states non, minimized or maximized.

Microsoft powershell get-processstate  get-process automatioelement mainwindowtitle maximized minimized.png

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#Add-Type -AssemblyName UIAutomationClient
function Get-ProcessState 
{
    <#
        Written by: http://vcloud-lab.com
        Last Edit: 06-July-2020
    #>
    [CmdletBinding()]
    param (
        [Parameter(Mandatory=$true)]
        [string]$ProcessName = 'Notepad'
    )
    Begin 
    {
        Add-Type -AssemblyName UIAutomationClient
    }
    process
    {
        $processList = Get-Process -Name $ProcessName
        foreach ($process in $processList) 
        {
            $automationElement = [System.Windows.Automation.AutomationElement]::FromHandle($process.MainWindowHandle)
            $processPattern = $automationElement.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)
            [PSCustomObject]@{
                Process = $process.MainWindowTitle
                ProcessState = $processPattern.Current.WindowVisualState
            }
        }
    }
}

Download script Get-ProcessWindowStatus.ps1 here or you can download it from github.com/kunaludapi.

Useful Articles
Installing, importing and using any module in powershell
Microsoft PowerShell: Check Windows license activation status
Find next available free drive letter using PowerShell
Copy Files with PowerShell Remoting WINRM Protocol
How to Install and Use Microsoft PowerShell on Linux
Configure PowerShell remoting between Windows and Linux
Get-PSRepository WARNING Unable to find module repositories
Invoke-WebRequest : The underlying connection was closed: An unexpected error occurred on a send
Creating an internal PowerShell module repository
How to sign PowerShell ps1 scripts

Go Back

Comment

Blog Search

Page Views

12162282

Follow me on Blogarama