'How to achieve a 'Wait' mode in Powershell Get-Process
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?
Solution 1:[1]
add it to the infinite loop
as following:
do{
get-process
start-sleep 5}
while(1)
Solution 2:[2]
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mohamed saeed |
| Solution 2 |
