Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading