{"id":1470,"date":"2014-05-06T00:01:00","date_gmt":"2014-05-06T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/06\/powershell-looping-understanding-and-using-dountil\/"},"modified":"2022-06-20T13:18:28","modified_gmt":"2022-06-20T20:18:28","slug":"powershell-looping-understanding-and-using-dountil","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-understanding-and-using-dountil\/","title":{"rendered":"PowerShell Looping: Understanding and Using Do\u2026Until"},"content":{"rendered":"<p align=\"left\">\n  <b>Summary<\/b>: Microsoft Scripting Guy, Ed Wilson, talks about understanding and using the Do\u2026Until loop.\n<\/p>\n<p align=\"left\">\n  Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are busy getting ready for TechEd 2014 North America, which will be in Houston, Texas this year. It kicks off this Sunday with registration and preconference sessions. This year, TechEd sold out several weeks prior to the event, so there is lots of excitement built up around this event. The Scripting Wife posted her <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/scripting-wife-creates-ideal-teched-2014-schedule\/\" target=\"_blank\" rel=\"noopener\">Ideal TechEd Schedule<\/a>, the <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/scripting-guys-booth-guest-meet-and-greet-schedule-for-teched-2014\/\" target=\"_blank\" rel=\"noopener\">Meet and Greet Schedule<\/a> for the Scripting Guys booth, and a map so you can <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\u00a02014<\/a>.\n<\/p>\n<p align=\"left\">\n  A major point of confusion amongst beginning scripters, regardless of the language, is the difference between a Do\u2026Until loop and a Do\u2026While loop.\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  <b>Note<\/b>\u00a0 Also see yesterday\u2019s post, <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-understanding-and-using-do-while\/\" target=\"_blank\" rel=\"noopener\">PowerShell Looping: Understanding and Using Do&#8230;While<\/a>.\n<\/p>\n<p align=\"left\">\n  In one respect, Do \u2026 Until and Do \u2026 While are opposites. This is due to the position of the condition to be evaluated. Whereas, a Do\u2026While loop continues processing WHILE a condition evaluates as True, a Do\u2026Until loop processes UNTIL a condition becomes True. This is a subtle difference that can lead to seriously difficult errors to detect in a complicated Windows PowerShell script.\n<\/p>\n<p align=\"left\">\n  In yesterday\u2019s post, I talked about using the Do&#8230;While loop. In one of the scripts from that post, I evaluate the value of a variable. If the value of <b>$a<\/b> is less than or equal to the number 5, the script block continues to process. But if the initial value of <b>$a<\/b> is greater than the 5, the script block still runs one time. This is because it is bottom evaluated. Here is a screenshot of that process:\n<\/p>\n<p align=\"left\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p align=\"left\">\n  If I change this from a Do\u2026While to a Do\u2026Until loop, the script looks like this:\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  $a = 0 #if greater than 5 runs forever\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 #if less than 5 runs only once\n<\/p>\n<p align=\"left\">\n  \u00a0\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  DO\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  {\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  \u00a0&#8220;Starting Loop $a&#8221;\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  \u00a0$a\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  \u00a0$a++\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  \u00a0&#8220;Now `$a is $a&#8221;\n<\/p>\n<p align=\"left\" style=\"margin-left:30px\">\n  } Until ($a -le 5)\n<\/p>\n<p align=\"left\">\n  The script block runs one time because it runs until the value of <b>$a<\/b> is less than or equal to 5. The output is shown here:\n<\/p>\n<p align=\"left\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p align=\"left\">\n  If I increase the value of <b>$a<\/b> to 9, notice that the script goes into an infinite loop. This is because the script runs the script block, and then it evaluates the condition. Because the condition will never become True, the script block continues to run, and run, and run. This is shown here:\n<\/p>\n<p align=\"left\">\n  <a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-6-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a>\n<\/p>\n<p align=\"left\">\n  In summary, both Do\u2026While and Do\u2026Until are bottom evaluated. Therefore, the script block runs at least one time. Do\u2026While runs as long as a condition is True. When that condition becomes False, it ceases to run. However, if the condition is always False, it will still run one loop.\n<\/p>\n<p align=\"left\">\n  Do\u2026Until runs until a condition becomes True. That is, it runs as long as a condition is False. It also evaluates at the bottom of the loop; and therefore, it will also run at least one time. If a condition never becomes True, the script goes into an endless loop.\n<\/p>\n<p align=\"left\">\n  In my example, 9 is never less than or equal to 5; and therefore, the condition will never become True. The script block went into an endless loop, and I had to stop the script by pressing the red square in the ISE. In the Windows PowerShell console, I would press <b>Ctrl<\/b>+<b>C<\/b>.\n<\/p>\n<p align=\"left\">\n  There are three similar tools: While, Do\u2026While, and Do\u2026Until. Each has its own usage and purpose in a script.\n<\/p>\n<p align=\"left\">\n  That is all there is to using Do\u2026Until. Join me tomorrow when I will talk about Windows PowerShell control statements in looping.\n<\/p>\n<p align=\"left\">\n  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.\n<\/p>\n<p align=\"left\">\n  <b>Ed Wilson, Microsoft Scripting Guy<\/b>\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about understanding and using the Do\u2026Until loop. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are busy getting ready for TechEd 2014 North America, which will be in Houston, Texas this year. It kicks off this Sunday with registration and preconference sessions. This year, [&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-1470","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 understanding and using the Do\u2026Until loop. Microsoft Scripting Guy, Ed Wilson, is here. The Scripting Wife and I are busy getting ready for TechEd 2014 North America, which will be in Houston, Texas this year. It kicks off this Sunday with registration and preconference sessions. This year, [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1470","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=1470"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1470\/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=1470"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1470"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1470"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}