Home > Blockchain >  How to achieve a 'Wait' mode in Powershell Get-Process
How to achieve a 'Wait' mode in Powershell Get-Process

Time:04-19

Get-Process cmdlet returns every process that is running and then control returns to powershell. For example, running this in powershell, i get:

PS C:\Users\me> Get-Process -Name thunderbird

Handles  NPM(K)    PM(K)      WS(K)     CPU(s)     Id  SI ProcessName
-------  ------    -----      -----     ------     --  -- -----------
   1791     139   193596     186964     551.86  13552   1 thunderbird
    723      45   194640     198084       4.81  16772   1 thunderbird
   1012      68    45068      41648       0.78  18328   1 thunderbird

 PS C:\Users\me>

Is there a way to keep it running and update when another thunderbird process starts?

CodePudding user response:

add it to the infinite loop

as following:

do{
get-process
start-sleep 5} 
while(1)

CodePudding user response:

Edit: Overcomplicated it for a sec there! The below also updates when instances are closed.

do{
cls
Get-Process -Name notepad -ErrorAction SilentlyContinue
Start-Sleep -Seconds 5
}while(1)

Old code

$proc = Get-Process -Name notepad -ErrorAction SilentlyContinue
$proc
do{
$update = Get-Process -Name notepad -ErrorAction SilentlyContinue
if ($update -ne $proc){
    $update | Where-Object -Property Id -NotIn $proc.Id
    $proc = $update
}
Start-Sleep -Seconds 5
}while(1)
  • Related