0

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.

user5828964
  • 302
  • 2
  • 10
  • 1
    Does this answer your question: https://stackoverflow.com/questions/2791489/how-do-i-take-out-the-focus-or-minimize-a-window-with-python ? The answer would by the same for PowerShell (call native API function `ShowWindow`), but it'd probably require more code written. – Mathias R. Jessen Jan 27 '22 at 18:07

1 Answers1

1

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

Sage Pourpre
  • 9,932
  • 3
  • 27
  • 39