Maybe you have already come across applications that require you to rearrange things on your desktop for optimal visibility or which you only use in combination with other programs or items - an automated startup or rearrangement would come handy in those situation.

The following little PowerShell script allows just this - automatical actions on process start and/or end.

$target = "firefox"
$process = Get-Process | Where-Object {$_.ProcessName -eq $target}
while ($true)
{
while (!($process))
{
$process = Get-Process | Where-Object {$_.ProcessName -eq $target}
start-sleep -s 5
}
if ($process)
{
"Place action on process start here"
$process.WaitForExit()
start-sleep -s 2
$process = Get-Process | Where-Object {$_.ProcessName -eq $target}
"Place action on process exit here"
}
}

The script above runs continuously until it is terminated or the current session is ended. With a wait time of 5 seconds to give the CPU a break it checks if the process is running - if not, it continues to check, if yes, it spills out some text you can replace with the action to perform on process start and waits until the process is ended. Afterwards, it returns some text to replace and continues to wait for process start again. Currently, the process that is monitored is firefox and is specified in the $target variable at the top of the code.

To run the script, copy and paste it into a notepad, save it as .ps1 file and schedule it on startup with the Windows Scheduled Tasks service if you like. To run the script completely without pop-ups, have a look here.

Monitor Processes With Windows PowerShell
Tagged on:                         

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Ad #native_company# — #native_desc# #native_cta#