{"id":53863,"date":"2009-04-29T15:17:00","date_gmt":"2009-04-29T15:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/04\/29\/hey-scripting-guy-how-can-i-use-the-dowhileloop-in-windows-powershell\/"},"modified":"2009-04-29T15:17:00","modified_gmt":"2009-04-29T15:17:00","slug":"hey-scripting-guy-how-can-i-use-the-dowhileloop-in-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-the-dowhileloop-in-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use the Do\u2026While\u2026Loop in Windows PowerShell?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>Hey, Scripting Guy! I frequently work with arrays in my scripts. The way I write scripts, I end up using <B>Do\u2026While\u2026Loop<\/B> frequently. I know that you like using the <B>For\u2026Each\u2026Next<\/B> for arrays, but to me <B>Do\u2026While\u2026Loop<\/B> seemed easier and more flexible. I looked and cannot find an example of how to use <B>Do\u2026While\u2026Loop<\/B> in Windows PowerShell. I can find the <B>ForEach<\/B> statement, but not the <B>Do\u2026While\u2026Loop<\/B>. It will be a bear if I have to switch over and completely change the way I write scripts. <BR><BR>&#8211; AB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi AB,<\/P>\n<P>Ed here. If you have been following us on Twitter, I took the weekend off, and the Scripting Wife and I went to <A href=\"http:\/\/en.wikipedia.org\/wiki\/Columbia,_South_Carolina\" target=\"_blank\">Columbia, South Carolina<\/A>, in the United States. While we were there we went to the <A href=\"http:\/\/en.wikipedia.org\/wiki\/Riverbanks_Zoo\" target=\"_blank\">Riverbanks Zoo<\/A>. So I know exactly what you mean when you say changing the way that you write scripts will be a bear. Perhaps even a bear such as this one I photographed Sunday:<\/P><IMG border=\"0\" alt=\"Image of a scary bear\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/april\/hey0429\/hsg-04-29-09-01.jpg\" width=\"500\" height=\"375\"> \n<P>&nbsp;<\/P>\n<P>However, you are right. We Scripting Guys seem to favor using the <B>For\u2026Each\u2026Next<\/B> when working with VBScript. We also like it in Windows PowerShell. But as we mentioned on Monday, it is all about choices. There are reasons why the different constructions were created, and each has advantages and disadvantages.<\/P>\n<TABLE id=\"E2D\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">This week we are looking at scripting with Windows PowerShell. Windows PowerShell is installed by default on Windows Server 2008 R2 and Windows 7. It is an optional installation on Windows Server 2008, and a download for <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=c6ef4735-c7de-46a2-997a-ea58fdfcba63\" target=\"_blank\">Windows Vista<\/A>, <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=6ccb7e0d-8f1d-4b97-a397-47bcc8ba3806\" target=\"_blank\">Windows XP<\/A>, and <A href=\"http:\/\/www.microsoft.com\/downloads\/details.aspx?displaylang=en&amp;FamilyID=10ee29af-7c3a-4057-8367-c9c1dab6e2bf\" target=\"_blank\">Windows Server 2003<\/A>. The <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\" target=\"_blank\">Windows PowerShell Scripting Hub<\/A> is a good place to start using Windows PowerShell. An excellent book for learning Windows PowerShell is the Microsoft Press book, <A href=\"http:\/\/www.microsoft.com\/MSPress\/books\/authors\/auth10329.aspx\" target=\"_blank\">Microsoft Windows PowerShell Step by Step<\/A>. This book has many hands-on labs and uses real-world examples to show the use of Windows PowerShell.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>We thought we would practice writing a bit of VBScript, so we created this <B>DemoDoWhile.vbs<\/B> script just for you AB. The first thing we do is assign the value of 0 to the variable <B>i<\/B>. We then create an array. To do this, we use the <B>Array<\/B> function, and assign the numbers 1 through 5 to the variable <B>ary<\/B>. We then use the <B>Do\u2026While\u2026Loop<\/B> construction to walk through the array of numbers. As long as the value of the variable <B>i<\/B> is less than the number 5, we display the value of the variable <B>i<\/B>. We then increment the value of the variable <B>i<\/B> and loop back around. The <B>DemoDoWhile.vbs<\/B> script is seen here:<\/P><PRE class=\"codeSample\">i = 0\nary = Array(1,2,3,4,5)\nDo While i &lt; 5\n WScript.Echo ary(i)\n i = i + 1\nLoop\n<\/PRE>\n<P>When we run the <B>DemoDoWhile.vbs<\/B> script in Cscript at the command prompt, we see the numbers 1 through 5 displayed at the command prompt. This is seen here:<\/P><IMG border=\"0\" alt=\"Image of the numbers 1 through 5 being displayed by the DemoDoWhile.vbs script when it is run in Cscript\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/april\/hey0429\/hsg-04-29-09-02.jpg\" width=\"500\" height=\"140\"> \n<P>&nbsp;<\/P>\n<P>We can do exactly the same thing by using Windows PowerShell. The <B>DemoDoWhile.ps1<\/B> script and the <B>DemoDoWhile.vbs<\/B> script are the same. The first thing we do is assign the value of 1 to the variable <B>$i<\/B>. We then create an array of the numbers 1 through 5 and store that array in the <B>$ary<\/B> variable. We use a shortcut in Windows PowerShell to make this a bit easier. Actually, arrays in Windows PowerShell are fairly easy anyway. If you want to create an array you just have to assign multiple pieces of data to the variable. To do this, we would separate each piece of data by a comma. This is seen here:<\/P><PRE class=\"codeSample\">$ary = 1,2,3,4,5\n<\/PRE>\n<P>If we needed to create an array with 32,000 numbers in it, it would be impractical to type each number and separate it with a comma. In VBScript we would have to use a <B>For\u2026Next<\/B><B>\u2026L<\/B><B>oop<\/B> to add the numbers to the array. In Windows PowerShell, we can use the range operator. To do this, we use a variable to hold the array of numbers that are created, and type the beginning and the ending number separated with two periods. This is seen here:<\/P><PRE class=\"codeSample\">$ary = 1..5\n<\/PRE>\n<P>Unfortunately the range operator does not work for letters. But there is nothing to prevent you from creating a range of numbers that represent the ASCII value of each letter, and then casting it to a string later. In fact we will do that in just a couple of minutes. It is cool! You may even find it useful some day. <\/P>\n<P>Where were we? Oh, yeah. We are now ready for the <B>Do\u2026While<\/B><B>\u2026Loop<\/B> in Windows PowerShell. We use the <B>Do<\/B> statement and open a set of braces (curly brackets). Inside these curly brackets, we have what is called a script block. The first thing we do is index into the array. On our first pass through the array, the value of <B>$i<\/B> is equal to 0. We therefore display the first element in the <B>$ary<\/B> array. We next increment the value of the <B>$i<\/B> variable by one. We are now done with the script block, so we look at the <B>While<\/B> statement. The condition we are examining is the value of the <B>$i<\/B> variable. As long as it is less than 5, we will continue to loop around. As soon as the value of <B>$i<\/B> is no longer less than the number 5, we stop looping. This is seen here:<\/P>\n<P><B>DemoDoWhile.ps1<\/B><\/P><PRE class=\"codeSample\">$i = 0\n$ary = 1..5\ndo \n{\n $ary[$i]\n $i++\n} while ($i -lt 5)\n<\/PRE>\n<P>When we run the <B>DemoDoWhile.ps1<\/B> script, we receive the results shown here:<\/P><IMG border=\"0\" alt=\"Image of a number displayed when the value of $i is less than 5\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/april\/hey0429\/hsg-04-29-09-03.jpg\" width=\"500\" height=\"122\"> \n<P>&nbsp;<\/P>\n<P>One thing to be aware of is that we are evaluating the value of <B>$i<\/B>. We initialized <B>$i<\/B> at 0. The first number in our array was 1. But the first element number in the array is always 0 in Windows PowerShell (unlike VBScript, which can start arrays with 0 or 1). Our <B>While<\/B> statement examines the value of <B>$i<\/B> and not the value that is contained in the array, which is why you see the number 5 displayed. <\/P>\n<P>We can change our <B>DemoDoWhile.ps1<\/B> script and do something rather cool by displaying the uppercase letters from A\u2013Z. To do this, we first initialize the <B>$i<\/B> variable and set it to 0. We then create a range of numbers from 65 through 91. These are the ASCII values for the capital letter <B>A <\/B>through the capital letter <B>Z<\/B>. Now we begin the <B>Do<\/B> statement and open our script block. To this point, the script is identical to the previous one. The trick we will use to separate letters from numbers is to cast the integer to a <B>char<\/B>. To do this, we use the <B>char<\/B> data type and put it in square brackets. We then use this to convert an integer to uppercase letter. To display the uppercase letter B from the ASCII value of 66, the code would resemble the following:<\/P><PRE class=\"codeSample\">PS C:\\&gt; [char]66\nB\n<\/PRE>\n<P>Because we know that the <B>$caps<\/B> variable contains an array of numbers that range from 65 through 91, and that the variable <B>$i<\/B> will hold numbers from 0 through 26, we index into the <B>$caps<\/B> array and cast the integer to a <B>char<\/B> and display the results. That\u2019s what this line of code does:<\/P><PRE class=\"codeSample\">[char]$caps[$i]\n<\/PRE>\n<P>We then increment the value of <B>$i<\/B> by one, close the script block, and enter the <B>While<\/B> statement where we check the value of <B>$i<\/B> to make sure it is less than 26. As long as <B>$i<\/B> is less than 26, we continue to loop around. The complete <B>DisplayCapitalLetters.ps1<\/B> script is seen here:<\/P><PRE class=\"codeSample\">$i = 0\n$caps = 65..91\ndo\n{\n [char]$caps[$i]\n $i++\n} while ($i -lt 26)\n<\/PRE>\n<P>AB, we hope that you will explore the <B>Do\u2026While<\/B><B>\u2026Loop<\/B> construction with Windows PowerShell, and you will be able to use it to meet your scripting needs. You know, while I was at the Riverbanks Zoo this weekend, I saw another bear as well. If you encounter a bear while scripting, I hope that it is this guy and not the first bear we saw:<\/P><IMG border=\"0\" alt=\"Image of a cuddly bear\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/april\/hey0429\/hsg-04-29-09-04.jpg\" width=\"500\" height=\"667\"> \n<P>&nbsp;<\/P>\n<P>Keep on scripting, and we will see you tomorrow as we continue talking about looping. Until then, watch out for the bears.<\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I frequently work with arrays in my scripts. The way I write scripts, I end up using Do\u2026While\u2026Loop frequently. I know that you like using the For\u2026Each\u2026Next for arrays, but to me Do\u2026While\u2026Loop seemed easier and more flexible. I looked and cannot find an example of how to use Do\u2026While\u2026Loop in Windows [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[51,3,4,45],"class_list":["post-53863","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I frequently work with arrays in my scripts. The way I write scripts, I end up using Do\u2026While\u2026Loop frequently. I know that you like using the For\u2026Each\u2026Next for arrays, but to me Do\u2026While\u2026Loop seemed easier and more flexible. I looked and cannot find an example of how to use Do\u2026While\u2026Loop in Windows [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53863","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=53863"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/53863\/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=53863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=53863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=53863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}