Run a command block based on the results of a conditional test.
Syntax
[:Loop_label] Do
{
command_block
} while (condition)
or
[:Loop_label] Do
{
command_block
} until (condition)
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.
:Loop_label An optional label than can be used to break or continue.
A Do..While loop continues as long as the condition is TRUE.
A Do..Until loop continues as long as the condition is FALSE. Until is an antonym of While.
As long as the condition is met, PowerShell will continue to rerun the {command_block} section unless interrupted by a break or continue statement.
Examples
Count to 10:
PS> Do { $val++ ; Write-Host $val } while($val -ne 10)
You can use carriage returns instead of semi-colons:
PS> Do { $val++
Write-Host $val
} while($val -ne 10)
“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.