Run a command block based on the results of a conditional test.

      [:Loop_label] Do
      } while (condition)


      [:Loop_label] Do
      } until (condition)


   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.

As long as the condition is met, PowerShell will continue to rerun the {command_block} section unless interrupted by a break or continue statement.


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

While - Loop as long as the condition is TRUE.
Until - Loop as long as the condition is FALSE. (Until is an antonym of While.)
Break statement
Continue statement
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.

Copyright © 1999-2023
Some rights reserved