Run a command block based on the results of a conditional test.
Syntax while (condition) {command_block} Key condition If this evaluates to TRUE the loop {command_block} runs. when the loop has run once the condition is evaluated again command_block Commands, separated by commas, to run each time the loop repeats.
As long as the condition remains true, PowerShell reruns the {command_block} section.
Examples
Count to 10:
PS> while($val -ne 10) { $val++ ; Write-Host $val }
You can use carriage returns instead of semi-colons:
PS> while($val -ne 10) { $val++ Write-Host $val }
“A quarrel is quickly settled when deserted by one party; there is no battle unless there be two” ~ Lucius Annaeus Seneca
Related PowerShell Cmdlets:
Break statement
Continue statement
Do - Run a command block based on the results of a conditional test.
ForEach-Object - Loop for each object in the pipeline (foreach).
ForEach - Loop through values in the pipeline.
IF - Conditionally perform a command.
For - Loop through items that match a condition.
Switch - Multiple if statements.