X

Monitor Processes With Windows PowerShell

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

Monitor Processes with  PowerShell

The following little PowerShell script allows just this - automatic 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, and if yes, it spills out some text you can replace with the action to perform on the process start and waits until the process is ended. Afterward, it returns some text to replace and continues to wait for the process to 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.

Other popular PowerShell guides

Categories: Windows Windows 7
CSch:
X

Headline

You can control the ways in which we improve and personalize your experience. Please choose whether you wish to allow the following:

Privacy Settings