PowerShell Looping: Using While

PowerShell Looping: Using While

  • Comments 2
  • Likes

Summary: Microsoft Scripting Guy, Ed Wilson, talks about Windows PowerShell looping and looks at using the While statement.

Microsoft Scripting Guy, Ed Wilson, is here. One of the things that I see with people coming to Windows PowerShell from VBScript or some other language, is that they seem to like to use the While statement. While there is nothing wrong with While or Do While Loop, or even Do, these are not the easiest things to use in Windows PowerShell.

     Note  This is Part 3 of a four-part series about looping.

Today I will talk about using While to loop through a collection.

While what?

When looping through a collection with the While statement, a control must be used to govern how long or how far the looping will take place. The basic structure of the While statement is shown here:

While (condition for controlling the looping)

{

  The script block that does what you want to do, and perhaps a counter variable

}

One way that I like to use the While statement is to cycle through the collection until I reach the end of the collection. The easy way to find the end of the collection is to use the count property. In general, I want to do something while my counter is less than the count of my collection. This is due to the placement of my counter variable at the bottom of my script block. So my While statement with the condition is shown here:

while ($x -lt $a.Count)

It is important when using this technique to initialize the value of the counter variable ($x in this example). Otherwise, the value of the counter may change on subsequent runs, and it will lead to unpredictable results.

In this example, I create an array with five items in it. The numbers 1 through 5 are stored in the $a variable. I then initialize the $x counter variable by setting the value to 0. These two lines of script are:

$a = 1,2,3,4,5

$x=0

After I have done that, I use the While statement and specify my condition. I then open the script block. Inside the script block, I use the $x counter variable to also identify which element in the array I want to work with. The interesting thing is the array is zero-based; therefore, the first element is element 0. Don’t get confused that it contains the value of 1. I then add five to the value, and display it to the output window. This line of script is shown here:

$a[$x] + 5

Next I increment the value of $x. I do this by using the ++ operator. Therefore $x++ adds one to the present value. This is shown here:

$x++

One advantage of using this technique is that I can choose any element from the array. I can also use different operators. For example, I can add 3 to the present value of $x as shown here:

$x+=3

A more likely scenario would be to add 2. This would permit me to access either odd or even elements. This is shown here:

$x+=2

Then just for fun, I print out the value of $x as it currently stands. This is to help understand how the counter variable is working. This line of script is shown here:

"`$x is now $x"

The complete script is:

$a = 1,2,3,4,5

$x=0

while ($x -lt $a.Count)

{

   $a[$x] + 5

   $x++

   #$x+=2

   "`$x is now $x"
}

The script and the output from the script are shown in the following image.

Image of command output

That is all there is to using the While statement. Looping Week will continue tomorrow when I will talk about automatic expansion.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment