EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell Break
Secondary Sidebar
PowerShell Tutorial
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell Break

By Chirag NagarekarChirag Nagarekar

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

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,241 ratings)

: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 into 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 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 to 5, it executes the break statement and the loop is terminated so the output here doesn’t exceed than 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

Same above example 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 the 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 to the “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: When the two arguments are in the Switch statement and when the first argument meets the condition with the Break statement, the script exits the switch loop. 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

Break command is helpful to terminate the loop when the loop executes infinitely. There is a significant difference between Break and Continue that, 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 Works in PowerShell?
  3. How For loop Works in PowerShell?
  4. Overview of PowerShell Tools
  5. Different Commands of PowerShell Scheduled Task
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more