PowerShell Looping: Advanced Break

PowerShell Looping: Advanced Break

  • Comments 10
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about additional uses for the Break statement in looping.

Microsoft Scripting Guy, Ed Wilson, is here. This morning, it is cool and the sun is barely up. I am sipping a cup of English Breakfast tea with a spoonful of spearmint and peppermint leaves, and a cinnamon stick. I have my new Surface Power Cover keyboard with my new Surface 2 Pro, and I am sitting on the lanai checking my scripter@microsoft.com email account.

One thing that is funny about the Surface Power Cover is that it is selling at a premium price from various outlets on Amazon. The Scripting Wife went to the Microsoft Store online, and she ordered the Power Cover for me when she ordered herself a new Surface 2 RT with 4G. If you are at TechEd, swing by the Scripting Guys booth: Find the Scripting Guys Booth at TechEd 2014. I am certain the Scripting Wife will be glad to show you her new Surface 2 RT.

Give me a break…

I should state at the outset that, in general, I avoid using the Break statement. I prefer to use simple constructions that have a single way in and a single way out. It makes the script easier to read and easier to understand. Therefore, it is easier to maintain and troubleshoot. However, there are times when using the Break statement will simplify the script, and it is the right tool to use. Not every task calls for a sledge hammer. But dude, when a sledge hammer is called for, I do not want to tackle the task with an 8-ounce, ball-peen hammer.

Note  This is a continuation of yesterday's post about using the Break statement: PowerShell Looping: Basics of the Break.  You might also like to review these Hey, Scripting Guy! Blog posts about Windows PowerShell looping.

A very powerful feature of the Break statement, and one that is not used very much, is the ability to break to a specific label. That is right. I can include labels in my Windows PowerShell script. The limitation is that the label can only precede a looping type of keyword, such as Do, Foreach, For, or While. When using the label, the label must be the first thing on the line, and it begins with a colon. For example, the following script illustrates creating a label:

:AnotherLoop while ($x -lt $a.count)

{

 "In another loop"

 $a[$x]

 $x++

The label is named AnotherLoop, and you can see that I have placed it first on the line. The label precedes the While statement. When I call this label in my script, I also must use the colon.

Note  The colon is left out of the Help examples that illustrate breaking to a label. Please remember that the colon is required.

In the following script, I first create a range of numbers from 2 through 6. I then set my counter variable equal to 0 (zero). Now I use the While statement to walk through the array of numbers. I use the If statement to see if the value of the element in the array is greater than 4. If it is, I use Break to go to a label named :AnotherLoop. While I am there, I continue walking through the array until I get to the end of the array.

$a = 2..6

$x = 0

While ($x -lt $a.count)

{

 "in main loop"

 $a[$x]

 if($a[$x] -gt 4) {Break :AnotherLoop}

 $x++

}

"Out of the main loop"

:AnotherLoop while ($x -lt $a.count)

{

 "In another loop"

 $a[$x]

 $x++

}

When I run this script, I see that I am in the main loop until I get to the number 5, then I move to the loop named AnotherLoop. The script and output are shown here:

Image of command output

That is all there is to using the Break statement. Windows PowerShell Fundamentals Week will continue tomorrow when I will talk about using the Continue statement.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, Script it, Cmdlet style.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi ScriptingGuy,

    it does´n't seem, that this looping to a label works as described...
    Your script clearly states, that the line "out of main loop" is executed, even it is placed before the loop label.
    I played around a bit, and it seems, that break normally quits the loop and the script executes everything after the closing "}" regardless of giving any label to it.

  • I have to agree with Martin here. The label doesn't do anything. Even if you put the labeled while on top it doesn't jump back up.

    However dispite that it doesn't do anything, isn't the feature you're explaining here just mimicing the horror that is goto? And do we really want to tell people that something like goto exists in PowerShell?

  • Here is a version that may be a bit more illustrative of what is happening: http://1drv.ms/1nrcyvx

  • Martin is right, this is not how labels are used in powershell looping.

    See http://technet.microsoft.com/en-us/library/hh847873.aspx

    Labels are for nested loops when you want to choose which loop to break out of.

  • Steve, as always, the TechNet documentation nails it down :)

  • thanks

  • Thanks commenters! Thanks to you too Ed, I didn't know about this one but I have to agree the TechNet article is pretty clear. I don't have a ton of loops within loops but I learned something new today. :)

  • I need more work in this area