Break

Exit a program loop immediately.

In a For, ForEach, While, Do loop or in a Switch statement you can add a break statement to exit the code block.

Choose the conditions under which you call the continue statement carefully, as it is easy to create an infinite loop. Most commonly an if statement can be used to define the break conditions.

Examples

Count to 10 but stop as soon as you reach number 5:

 PS> $i =0
     while ($i -lt 10)
         {
           $i +=1 
           if ($i -eq 5) {break}
           Write-Host $i
         }

“Our greatest glory is not in never failing, but in rising up every time we fail” -Ralph Waldo Emerson

Related:

Continue statement
ForEach-Object - (foreach) Loop for each object in the pipeline
ForEach - Loop through values in the pipeline
For - Loop through items that match a condition
Switch - Multiple if statements
While Loop while a condition is True


© Copyright SS64.com 1999-2014
Some rights reserved