{"id":1457,"date":"2014-05-08T00:01:00","date_gmt":"2014-05-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/08\/powershell-looping-advanced-break\/"},"modified":"2014-05-08T00:01:00","modified_gmt":"2014-05-08T00:01:00","slug":"powershell-looping-advanced-break","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-advanced-break\/","title":{"rendered":"PowerShell Looping: Advanced Break"},"content":{"rendered":"<p><b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about additional uses for the <b>Break<\/b> statement in looping.<\/p>\n<p>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 <a href=\"mailto:scripter@microsoft.com\">scripter@microsoft.com<\/a> email account.<\/p>\n<p>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&nbsp;2&nbsp;RT with 4G. If you are at TechEd, swing by the Scripting Guys booth: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-find-the-scripting-guys-booth-at-teched-2014\/\">Find the Scripting Guys Booth at TechEd 2014<\/a>. I am certain the Scripting Wife will be glad to show you her new Surface&nbsp;2&nbsp;RT.<\/p>\n<h2>Give me a break&hellip;<\/h2>\n<p>I should state at the outset that, in general, I avoid using the <b>Break<\/b> 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 <b>Break<\/b> 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, <a href=\"http:\/\/en.wikipedia.org\/wiki\/Ball_peen_hammer\" target=\"_blank\">ball-peen hammer<\/a>.<\/p>\n<p style=\"margin-left:30px\"><strong>Note<\/strong>&nbsp; This is a continuation of yesterday&#039;s post about using the <strong>Break<\/strong> statement: <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-basics-of-the-break\/\" target=\"_blank\">PowerShell Looping: Basics of the Break<\/a>. &nbsp;You might also like to review these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/looping\/\" target=\"_blank\">Hey, Scripting Guy! Blog posts about Windows PowerShell looping<\/a>.<\/p>\n<p>A very powerful feature of the <b>Break<\/b> 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 <b>Do<\/b>, <b>Foreach<\/b>, <b>For<\/b>, or <b>While<\/b>. 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:<\/p>\n<p style=\"margin-left:30px\">:AnotherLoop while ($x -lt $a.count)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;In another loop&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$a[$x]<\/p>\n<p style=\"margin-left:30px\">&nbsp;$x++<\/p>\n<p>The label is named <b>AnotherLoop<\/b>, and you can see that I have placed it first on the line. The label precedes the <b>While<\/b> statement. When I call this label in my script, I also must use the colon.<\/p>\n<p style=\"margin-left:30px\"><b>Note&nbsp;<\/b> The colon is left out of the Help examples that illustrate breaking to a label. Please remember that the colon is required.<\/p>\n<p>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 <b>While<\/b> statement to walk through the array of numbers. I use the <b>If<\/b> statement to see if the value of the element in the array is greater than 4. If it is, I use <b>Break<\/b> to go to a label named <b>:AnotherLoop<\/b>. While I am there, I continue walking through the array until I get to the end of the array.<\/p>\n<p style=\"margin-left:30px\">$a = 2..6<\/p>\n<p style=\"margin-left:30px\">$x = 0<\/p>\n<p style=\"margin-left:30px\">While ($x -lt $a.count)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;in main loop&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$a[$x]<\/p>\n<p style=\"margin-left:30px\">&nbsp;if($a[$x] -gt 4) {Break :AnotherLoop}<\/p>\n<p style=\"margin-left:30px\">&nbsp;$x++<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p style=\"margin-left:30px\">&quot;Out of the main loop&quot;<\/p>\n<p style=\"margin-left:30px\">:AnotherLoop while ($x -lt $a.count)<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;In another loop&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$a[$x]<\/p>\n<p style=\"margin-left:30px\">&nbsp;$x++<\/p>\n<p style=\"margin-left:30px\">}<\/p>\n<p>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 <b>AnotherLoop<\/b>. The script and output are shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-8-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-8-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using the <b>Break<\/b> statement. Windows PowerShell Fundamentals Week will continue tomorrow when I will talk about using the <b>Continue<\/b> statement.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, Script it, Cmdlet style.<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,95,3,4,45],"class_list":["post-1457","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-looping","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>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. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1457","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=1457"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1457\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=1457"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1457"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1457"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}