Quantcast
Channel: Hey, Scripting Guy! Blog
Viewing all articles
Browse latest Browse all 3333

PowerShell Looping: Advanced Break

$
0
0

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 


Viewing all articles
Browse latest Browse all 3333

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>