What is PowerShell Break statement?


The break statement in PowerShell is useful for terminating the loop. When the break statement is executed inside the inner loop, it terminates that particular loop execution and when it is placed in the outer loop, it terminates the entire loop including child loop(s).

Break statement is used with Foreach, For, While, Do-While and the Switch statement. When the break statement is used with the Label, PowerShell executes the label loop instead of exiting from the loop.

Break Statement example

$i = 1
While($i -ne 10){
   Write-Output $i
   if($i -eq 5){break}
   $i++
}

Output

1
2
3
4
5

In the above example, the loop terminates when the value reaches 5 because the Break statement is executed.

Updated on: 12-Mar-2020

120 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements