{"id":1477,"date":"2014-05-05T00:01:00","date_gmt":"2014-05-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/05\/05\/powershell-looping-understanding-and-using-do-while\/"},"modified":"2014-05-05T00:01:00","modified_gmt":"2014-05-05T00:01:00","slug":"powershell-looping-understanding-and-using-do-while","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-understanding-and-using-do-while\/","title":{"rendered":"PowerShell Looping: Understanding and Using Do&#8230;While"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about using the Do&hellip;While statement in Windows PowerShell scripts.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Last week, the Scripting Wife and I were at the Windows PowerShell Summit in Bellevue, Washington. It was an awesome week. We got to reconnect with many old friends, and we also made new ones. I delivered three sessions on Wednesday. Needless to say, I was a bit tired. In fact, the entire week was tiring. I am talking about 16-hour days, and a three-hour time zone change kind of tired. But the energy was buzzing around; and therefore, it was awesome.<\/p>\n<p>Before one of my sessions, I decided to get a cup of coffee. Someone saw me and said, &ldquo;What are you doing with coffee? Don&rsquo;t you usually drink tea?&rdquo; Well, yes I do, but I thought I would go for the &quot;hard stuff&quot; to provide a little extra jolt before my session.<\/p>\n<p>After one of my sessions, someone came up to me and said they liked my blog posts last week, and wondered if I would write something about <b>Do<\/b>. A &quot;Scooby Do, where are you&quot; kind of do? Probably not. Here you go&#8230;<\/p>\n<p>The Do&hellip;While loop construction in Windows PowerShell is a bottom evaluated version of the <b>While<\/b> loop that I talked about in <a href=\"https:\/\/devblogs.microsoft.com\/scripting\/powershell-looping-using-while\/\" target=\"_blank\">PowerShell Looping: Using While<\/a>. The whole idea is that I am going to do something while a condition exists. As an example, here is a recent weather forecast for Charlotte, North Carolina, where the Scripting Wife and I live:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-5-5-14-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/Hsg-5-5-14-1.png\" alt=\"Image of weather report\" title=\"Image of weather report\" \/><\/a><\/p>\n<p>So I might go outside and ride my bike while the sun is shining. The strange thing is the way the evaluation goes. It might look like this:<\/p>\n<p>Go to the garage.<\/p>\n<p>Get the bike.<\/p>\n<p>Ride bike.<\/p>\n<p>Ride while it is sunny.<\/p>\n<p>The evaluation occurs at the end. In Windows PowerShell script, it would look like the following:<\/p>\n<p style=\"margin-left:30px\">Go to garage<\/p>\n<p style=\"margin-left:30px\">Get the bike<\/p>\n<p style=\"margin-left:30px\">Do<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp; Ride bike<\/p>\n<p style=\"margin-left:30px\">} (While (weather &ndash;eq &lsquo;sunny&rsquo;)<\/p>\n<h2>Using Do&hellip;While<\/h2>\n<p>Now I want to take a look at a real Windows PowerShell script that illustrates using the Do&hellip;While loop.<\/p>\n<p>In the script that follows, I set the value of the <b>$a<\/b> variable to equal 1. In the <b>Do<\/b> loop, I print a message that I am starting the loop. Next I display the value of <b>$a<\/b>, and then I increment <b>$a<\/b>. I print the current value of <b>$a<\/b>, and then close the script block. In the <b>While<\/b> evaluation block, I check to see if <b>$a<\/b> is less than or equal to 5. The script is shown here:<\/p>\n<p style=\"margin-left:30px\">$a = 1&nbsp;<\/p>\n<p style=\"margin-left:30px\">DO<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;Starting Loop $a&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$a<\/p>\n<p style=\"margin-left:30px\">&nbsp;$a++<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;Now `$a is $a&quot;<\/p>\n<p style=\"margin-left:30px\">} While ($a -le 5)<\/p>\n<p>The script in the ISE and the output from the script are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-02.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-02.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>So, I can see that there are four parts to a Do&hellip;While loop. First is the <b>Do<\/b> keyword, then the script block that I want to &ldquo;do.&rdquo; Then comes the <b>While<\/b> keyword, and the condition that is evaluated to determine if another loop will occur.<\/p>\n<p>In many respects, the <b>While<\/b> statement and the Do&hellip;While loop are similar. The difference is that with the <b>While<\/b> statement, the condition is evaluated to determine if the script block will take place. In the Do&hellip;While loop, because the condition occurs at the end of the loop, it will always loop at least once.<\/p>\n<p>This is illustrated in the image that follows. I use exactly the same script as I used previously, but instead of setting the value of <b>$a<\/b>, I assign a value of 9. Now, 9 is greater than 5, so the <b>($a &ndash;le 5)<\/b> condition is never true. Yet, as you can see in the image, the script block ran once.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-03.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-03.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<h2>A more practical example<\/h2>\n<p>I decided to write a little script that will monitor for the existence of a process by name. As long as the process runs, the script continues to loop around. To ensure that the process runs, I start it at the beginning of the script. I then use the <b>&ndash;contains<\/b> operator to look for the process by name in the control portion of the loop. Here is the script:<\/p>\n<p style=\"margin-left:30px\">Start-process notepad<\/p>\n<p style=\"margin-left:30px\">$p = &quot;notepad&quot;<\/p>\n<p style=\"margin-left:30px\">Do<\/p>\n<p style=\"margin-left:30px\">{<\/p>\n<p style=\"margin-left:30px\">&nbsp;&quot;$p found at $(get-date)&quot;<\/p>\n<p style=\"margin-left:30px\">&nbsp;$proc = Get-Process<\/p>\n<p style=\"margin-left:30px\">&nbsp;start-sleep 2<\/p>\n<p style=\"margin-left:30px\">} While ($proc.name -contains &#039;notepad&#039;)&nbsp;<\/p>\n<p>In the following image, I see that it took me a few seconds to close Notepad. When I manually close Notepad, the looping stops.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-04.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-5-5-14-04.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>Windows PowerShell Fundamentals Week will continue tomorrow when I will continue talking about looping.<\/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, peace.<\/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 using the Do&hellip;While statement in Windows PowerShell scripts. Microsoft Scripting Guy, Ed Wilson, is here. Last week, the Scripting Wife and I were at the Windows PowerShell Summit in Bellevue, Washington. It was an awesome week. We got to reconnect with many old friends, and we also [&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-1477","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 using the Do&hellip;While statement in Windows PowerShell scripts. Microsoft Scripting Guy, Ed Wilson, is here. Last week, the Scripting Wife and I were at the Windows PowerShell Summit in Bellevue, Washington. It was an awesome week. We got to reconnect with many old friends, and we also [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1477","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=1477"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/1477\/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=1477"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=1477"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=1477"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}