Which type of PowerShell loop should you be using?

Loops are very commonly used in PowerShell scripts, and the vast majority of the scripts that I have written over the years have included at least one loop. Even so, looping isn’t quite as straightforward as it might at first seem. While there is nothing overly complex about creating a loop, PowerShell supports several different types of loops. As such, I wanted to take the opportunity to talk about the various types of loops that exist and when it is appropriate to use each one.

powershell loop

The ForEach PowerShell loop

I wanted to start out by talking about ForEach loops because this is the loop type that I find myself using most often.

A ForEach loop is useful when you need to take action on each item in a collection of items. For example, suppose that you wanted to create a list of all of the processes running on a computer and then take some sort of action for each process. The code would look something like this:

$Processes = Get-Process
ForEach($Process in $Processes)
{
Do-Something
}

The first line creates a variable named $Processes and maps it to the list of processes. The ForEach loop then performs an action (in this case, it calls a function called Do-Something) for each process in the list of processes.

The For loop

PowerShell also allows you to create a For loop. This is different from a ForEach loop. A For loop is usually based on a number that determines how many times the loop will run. Here is an example:

For ($A = 10; $A -GE 1; $A--)
{
Do-Something
}
In this case, the for loop is setting a variable named $A to a value of 10. If the contents of this variable are greater than or equal to 1 (-GE 1), then the loop runs. Each time the loop processes, the value of $A is decreased by 1 ($A--) until the value eventually reaches 1, which causes the loop to terminate.

The While loop

A While loop functions in a way that is really similar to a For loop. Like a For loop, a While loop only progresses until a certain condition is true. The block of code shown below does essentially the same thing as the For loop that I showed you in the previous section.

$A =10
While ($A -GT 0)
{
Do-Something
$A--
}

Even though this loop does exactly the same thing as the For loop that I showed you a moment ago, there are two key differences. The first difference is in the terminating value that I used for the loop.

The terminating value used in the for loop works kind of like an if statement. It is essentially saying that the loop will be allowed to run if the value is greater than or equal to 1. This means that the loop will terminate when the value of $A reaches 0.

The While loop does the same thing, but rather than indicating that the value of $A has to be greater than or equal to 1, this loop will function so long as $A is greater than zero. It’s a subtle difference, but it’s an important one. A For loop processes until a specific condition is met, whereas a While loop progresses so long as a condition is not met. Oftentimes, these two types of loops can be used interchangeably. But you may find it is more convenient to use one type of loop over the other, depending on the situation.

The other big difference between the way that these two loops is in the way that the loop is declared. The For loop includes the starting value, the terminating condition, and the code to decrement the loop counter within the loop declaration (For ($A = 10; $A -GE 1; $A–)). On the other hand, the While loop gets its starting value from outside of the loop, which means that this type of loop is going to be the better choice if you want to base the number of loops on an existing variable. Also, the code used to decrement the loop counter is found in the loop body rather than in the loop declaration.

The Do loop

The last type of loop that I want to show you is the Do loop. The Do loop is very similar to the While loop and the For loop, but there is one very important difference. Here is what the script that I have been using so far would look like if rewritten to use a Do loop:

$A=10
Do
{
Do-Something
$A--
}
Until ($A -LE 0)

Once again, we are starting by setting $A to 10, and then we are looping the code, decrementing the $A variable during each loop. The loop progresses until $A reaches a value of 0.

You have no doubt noticed that even though the basic looping structure is similar to what I have already shown you with the other loop types, the general syntax is a bit different. This goes back to the important difference that I mentioned earlier.

Imagine for a moment that I were to modify the first line of code to $A = 0. Even though the value of $A is already set to zero, the loop would still process one time because the condition is not evaluated until the end of the loop. The very last line of code tells the loop to stop if $A is less than or equal to 0. In this case, we started with a value of zero and there is a line in the code that decrements $A by one ($A–). As such, the value of $A would be -1 at the end of the loop. That’s why we have to check to see if $A is less than or equal to zero rather than just checking to see if $A = 0.

The bottom line is that a Do loop should only be used if you want the loop’s code to run at least once, no matter what.

Every PowerShell loop serves a purpose

PowerShell offers a number of different loop types, and each has its unique use case. ForEach loops are best-suited to working with arrays and collections of objects, but the other three loop types are more general-purpose in nature and can be used in a variety of situations.

Featured image: Shutterstock

About The Author

Leave a Comment

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top