{"id":1464,"date":"2014-05-07T00:01:00","date_gmt":"2014-05-07T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/07\/powershell-looping-basics-of-the-break\/"},"modified":"2022-06-20T13:24:49","modified_gmt":"2022-06-20T20:24:49","slug":"powershell-looping-basics-of-the-break","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-basics-of-the-break\/","title":{"rendered":"PowerShell Looping: Basics of the Break"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about the basics of using the <strong>Break<\/strong> statement in a Windows PowerShell loop.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Hmmm, it seems that the Scripting Wife and I just returned from the Windows PowerShell Summit, and yet in four days, we are leaving for TechEd in Houston. Oh, wait. We DID just return from the Windows PowerShell summit. Dude, it rocked, too.<\/p>\n<p>We are so looking forward to TechEd. Make sure you stop by the Scripting Guys booth and say &#8220;hi.&#8221; Hey, come hang out at the Scripting Guys booth all week. We have a number of way cool guests who will be stopping by. Check out these posts for more information:<\/p>\n<ul>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/weekend-scripter-find-the-scripting-guys-booth-at-teched-2014\/\" target=\"_blank\" rel=\"noopener\">Find the Scripting Guys Booth at TechEd 2014<\/a><\/li>\n<li><a href=\"https:\/\/devblogs.microsoft.com\/scripting\/scripting-guys-booth-guest-meet-and-greet-schedule-for-teched-2014\/\" target=\"_blank\" rel=\"noopener\">Scripting Guys Booth Guest Meet and Greet Schedule for TechEd 2014<\/a><\/li>\n<\/ul>\n<p>In addition, we will be adding guests all week, so book mark the page, follow @ScriptingGuys on Twitter, and @ScriptingWife on Twitter. We will be sending tweets during TechEd about things as they develop.<\/p>\n<p>We have a couple of pretty cool things to give away. In addition, the folks from <a href=\"http:\/\/powershell.org\/wp\/\" target=\"_blank\" rel=\"noopener\">PowerShell.org<\/a> are sharing our booth again this year. It only makes sense, because the community is what makes Windows PowerShell so strong and such a vital piece of technology.<\/p>\n<h2>Understanding the Break statement<\/h2>\n<p>The <strong>Break<\/strong> statement is used to exit a looping statement such as a <strong>Foreach<\/strong>, <strong>For<\/strong>, <strong>While<\/strong>, or <strong>Do<\/strong> loop. When present, the <strong>Break<\/strong> statement causes Windows PowerShell to exit the loop. The <strong>Break<\/strong> statement can also be used in a <strong>Switch<\/strong> statement.<\/p>\n<p style=\"margin-left:30px\">\n  <b>Note\u00a0<\/b> For more information about looping in Windows PowerShell script, take a look at these <a href=\"\/b\/heyscriptingguy\/archive\/tags\/looping\/\" target=\"_blank\" rel=\"noopener\">Hey, Scripting Guy! Blog posts<\/a>.\n<\/p>\n<p>In the following script, a Do\u2026While loop runs while the value of the <strong>$a<\/strong> variable is less than or equal to 5. (This script is discussed in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-understanding-and-using-do-while\/\" target=\"_blank\" rel=\"noopener\">PowerShell Looping: Understanding and Using Do\u2026While<\/a>.)<\/p>\n<p style=\"margin-left:30px\">\n  $a = 2 #if greater than 5 still runs once\n<\/p>\n<p style=\"margin-left:30px\">\n  DO\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Starting Loop $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a++\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Now `$a is $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  } While ($a -le 5)\n<\/p>\n<p>If the value of <strong>$a<\/strong> is less than or equal to 5, the script loops. But if the value is greater than 5, initially, the script runs in an infinite loop. At any rate, the next value displays. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>\u00a0Using the Break statement<\/h2>\n<p>There are two problems with the previous script. The first is that it always displays an extra number, and second is it goes into an infinite loop if the initial value is too great. It would be possible to add structured error handling to fix this problem, but an easier solution is to use the <strong>Break<\/strong> statement.<\/p>\n<p>The most serious problem with the script is the infinite loop. To check this problem, I use an <strong>If<\/strong> statement, and if the value of <strong>$a<\/strong> is greater than my maximum value, I simply use the <strong>Break<\/strong> statement inside a script block as shown here:<\/p>\n<p style=\"margin-left:30px\">\n  If($a -gt $max) {break}\n<\/p>\n<p>The other change I made to the script is to create a value for the maximum value instead of having to hard code it everywhere. And I added a line of script indicating that I was outside of the loop. When the <strong>Break<\/strong> statement hits, it exits the loop and goes to the script outside of the loop. It will therefore exit to the \u201cNow do something else outside the loop\u201d statement. Here is the script:<\/p>\n<p style=\"margin-left:30px\">\n  $a = 9 #if greater than 5 if statement catches it\n<\/p>\n<p style=\"margin-left:30px\">\n  $max = 5\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Enter the loop&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  DO\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0If($a -gt $max) {break}\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Starting Loop $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a++\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Now `$a is $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  } While ($a -le $max)\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Now do something else outside the loop&#8221;\n<\/p>\n<p>When I run the script, I see that it catches the condition of a too great value for <strong>$a<\/strong>, and therefore avoids the infinite loop. This is shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>There is only one problem left, and it is that there is always an extra line printed. It is due to the way the script is written. I display the value of <strong>$a<\/strong> after I increment to the new value, but it looks silly when I hit the value of <strong>$a = 5<\/strong> and then it says that <strong>$a<\/strong> is equal to 6. This is shown here:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Probably the best thing to do would be to revise the script. But in this example, I don\u2019t want to do that. So instead, I copy my if \/ break line and place it ahead of the \u201cNow $a is $a\u201d line. The revised script is shown here:<\/p>\n<p style=\"margin-left:30px\">\n  $a = 2 #if greater than 5 if statement catches it\n<\/p>\n<p style=\"margin-left:30px\">\n  $max = 5\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Enter the loop&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  DO\n<\/p>\n<p style=\"margin-left:30px\">\n  {\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0If($a -gt $max) {break}\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Starting Loop $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0$a++\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0If($a -gt $max) {break}\n<\/p>\n<p style=\"margin-left:30px\">\n  \u00a0&#8220;Now `$a is $a&#8221;\n<\/p>\n<p style=\"margin-left:30px\">\n  } While ($a -le $max)\n<\/p>\n<p style=\"margin-left:30px\">\n  &#8220;Now do something else outside the loop&#8221;\n<\/p>\n<p>When I run the script now, I no longer get the extra line of text displaying in the output:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-7-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>That is all there is to using the <strong>Break<\/strong> statement within a loop. Windows PowerShell Fundamentals Week will continue tomorrow when I will look at more advanced usage of the <strong>Break<\/strong> statement.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\" rel=\"noopener\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\" target=\"_blank\" rel=\"noopener\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\" rel=\"noopener\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\" rel=\"noopener\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<p><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about the basics of using the Break statement in a Windows PowerShell loop. Microsoft Scripting Guy, Ed Wilson, is here. Hmmm, it seems that the Scripting Wife and I just returned from the Windows PowerShell Summit, and yet in four days, we are leaving for TechEd in Houston. [&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-1464","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 the basics of using the Break statement in a Windows PowerShell loop. Microsoft Scripting Guy, Ed Wilson, is here. Hmmm, it seems that the Scripting Wife and I just returned from the Windows PowerShell Summit, and yet in four days, we are leaving for TechEd in Houston. [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1464","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=1464"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1464\/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=1464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}