While
suggest changeA while loop tests a condition and, when the condition is true, runs the body. It repeats until the condition is false.
while(condition){
code_block
}
The following example counts down from 10 through 0:
$i = 10
while($i -ge 0){
$i
$i--
}
Unlike the do loop, while tests the condition before the first run. The body does not run when the condition starts out false.
PowerShell treats a command that writes output as true. This example starts Notepad and sleeps while a notepad process exists. If another Notepad is already open, the loop continues until every notepad process has exited.
Start-Process notepad.exe
while(Get-Process notepad -ErrorAction SilentlyContinue){
Start-Sleep -Milliseconds 500
}
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents