EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Break
 

PowerShell Break

Chirag Nagarekar
Article byChirag Nagarekar
EDUCBA
Reviewed byRavi Rathore

Updated June 22, 2023

powershell break

 

 

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).

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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

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

powershell break

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:

PowerShell Break Example 1

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:

PowerShell Break Example 2

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:

PowerShell Break Example 3

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:

PowerShell Break Example 4

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:

Outer Loop Example 5

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:

Foreach loop Example 6

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:

Switch Example 7

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:

Label Example 8

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 –

  1. Top 9 Versions of PowerShell
  2. How Does Switch Case Work in PowerShell?
  3. How does For loop Work in PowerShell?
  4. Overview of PowerShell Tools
  5. Different Commands of PowerShell Scheduled Task

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW