Do

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

Syntax
      [:Loop_label] Do
      {
         command_block
      } while (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.

As long as the condition remains true, PowerShell reruns the {command_block} section.
'until' may be used in place of 'while' (a synonym)

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:

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
While - Loop while a condition is True



Back to the Top

© Copyright SS64.com 1999-2010
Some rights reserved