Minimize Process from powershell/python using Process ID (PID)

I’m writing a python script for an app lock. For this, I’m executing the Get-Process -Name "notepad++" PowerShell command using python subprocess to get the process id.
Screenshoot of Process

Now, using psutil I’m able to kill the process. But my objective is to minimize the windows in a while loop either using powershell/python. So, the program becomes unusable until the user enters the password.

>Solution :

With Powershell, you could do it with the use of the UIAutomationClient methods without having to rely on native calls.

Here is a small example to demonstrate how to check the window state and minimize the window if not.

Add-Type -AssemblyName UIAutomationClient

$MyProcess = Get-Process -Name "notepad++"

$ae = [System.Windows.Automation.AutomationElement]::FromHandle($MyProcess.MainWindowHandle)
$wp = $ae.GetCurrentPattern([System.Windows.Automation.WindowPatternIdentifiers]::Pattern)


# Your loop to make sure the window stay minimized would be here
# While...
$IsMinimized = $wp.Current.WindowVisualState -eq 'Minimized'
if (! $IsMinimized) { $wp.SetWindowVisualState('Minimized') } 
# End While

Reference

How to switch minimized application to normal state

Leave a Reply