Updated June 22, 2023
What is PowerShell Break Statement?
The break statement in PowerShell is used to terminate the loop. When the break statement is executed inside the inner loop, it terminates that loop execution, and when it is placed in the outer loop, it terminates the entire loop, including child loop(s).
The break statement is used with Foreach, For, While, Do-While, and the Switch statement. When the break statement is used with the Label, PowerShell exits the label loop instead of exiting the current loop.
Syntax:
To use the break statement, just write a BREAK in the loop. This is case insensitive. You can also use a BREAK with the label.
:myLabel while (<condition>) { <statement list>}
Flowchart of PowerShell Break Statement
Below is the flowchart of PowerShell Break:
1. Break Statement in Inner Loop
Explanation: In the above diagram, when the program starts execution and checks the condition in the outer loop, and if it satisfies, it enters the inner loop. If the break statement is executed, it terminates the inner loop and jumps directly to the outer loop, as mentioned in the above diagram.
2. Break Statement in Outer Loop
Explanation: As shown in the above diagram, when the Outer loop executes, it enters into the inner loop, and also the Break condition is mentioned in the outer loop; once it is executed, it terminates both inner and outer loops.
Examples of PowerShell Break
Below are the examples of PowerShell Break Statement:
Example #1 – Break with While Loop
Code:
$i=1
While($i -lt 10){
Write-Output "i = $i"
if($i -eq 5){
Write-Output "Break Statement executed"
break
}
$i++
}
Output:
In the above example, when the value of $i reaches 5, it executes the break statement, and the loop is terminated so the output here doesn’t exceed 5.
Example #2 – Break with For Loop
Code:
Break command can also be used with the For loop.
for($i=1;$i -lt 10 ; $i++){
Write-Output "i = $i"
if($i -eq 5){
Write-Output "Break Statement executed"
Break
}
}
Output:
Example #3 – Break with the Do-While Loop
The same above example is with the Do-While loop.
Code:
$i = 1
do {
Write-Output "i = $i"
if($i -eq 5){
Write-Output "Break statement executed"
Break
}
$i++
} while ($i -lt 10)
Output:
Example #4 – Break Statement in Inner Loop
Code:
$i = 1
while($i -lt 3){
Write-Output "i = $i"
$j = 1
while ($j -lt 5 ) {
Write-Output "j = $j"
if($j -eq 2){
Write-Output "Break Command Executed"
Break
}
$j++
}
$i++
Write-Output "`n------------------------`n"
}
Output:
Explanation: In the above example, whenever the value of $j reaches 3, it terminates the current loop, and execution moves to the parent loop. So, here Break command is executed twice as per the condition.
Example #5 – Break Statement in Outer Loop
Code:
$i = 1
while($i -lt 5){
Write-Output "i = $i"
$j = 1
while ($j -lt 3 ) {
Write-Output "j = $j"
$j++
}
if($i -eq 2){
Write-Output "Break statement executed"
Break
}
$i++
Write-Output "`n------------------------`n"
}
Output:
Explanation: In the above example, the Break statement is in the main outer loop, so when the value of $i reaches to 2, the script terminates both loops.
Example #6 – Break Command with the Foreach Loop
Code:
foreach($item in (Get-ChildItem D:\Temp -Recurse)){
$item | Select Name, Length
if($item.Name -eq "Style.css"){
Write-Output "Break command exectuted"
Break
}
}
Output:
In the above example, the script grabs the content inside the directory, and when the value name equals “Style.css,” then it exits the loop.
Example #7 – Break Statement with Switch
Code:
Switch (3,5){
1 {"This is One"}
2 {"This is Two"}
3 {"This is Three"; Break}
4 {"This is Four"}
5 {"This is Five"; Break}
}
Output:
Explanation: The script exits the switch loop when the two arguments are in the Switch statement and when the first argument meets the condition with the Break statement. So only one output is displayed
Example #8 – Break with Label
Code:
When the break statement is mentioned with the Label, PowerShell exits to the label instead of exiting the current loop.
$i = 1
while ($i -lt 10) {
Write-Output "i = $i"
if($i -eq 5){
Write-Output "Break statement executed"
Break :mylable
}
$i++
}
Write-Output "Entering to another loop"
$j = 1
:mylable while($j -lt 3){
Write-Output "j = $j"
$j++
}
Output:
Explanation: In the above example, when the break command is executed with the label, it skips the current loops and moves to the labeled loop.
Conclusion
The break command helps terminate the loop when the loop executes infinitely. There is a significant difference between Break and Continue; Break exits the loop while the Continue statement skips the current iteration.
Recommended Articles
This is a guide to PowerShell Break. Here we discuss What is PowerShell Break Statement with Flowchart along with examples and code implementation. You can also go through our other suggested articles to learn more –