PowerShell Loops

A loop statement allows for the execution of a statement or group of statements multiple times. There are three types of loop statement in PowerShell.

  • ‘for’ loop – Useful when it is known in advance the number of times the statements within the loop need to be executed.
  • ‘while’ loop – Repeats a statement or group of statements while an expression evaluates to true. There is a condition to get into the loop, so if the condition evaluates to true at the beginning then the statements inside the loop will never execute.
  • ‘do’ loop – Allows for a condition to be tested at the end of the loop structure. Repeats a statement or group of statements while an expression evaluates to true or until it evaluates to true. The statements within the loop always execute once because the condition doesn’t get tested until the end of the first loop iteration.

As with ‘if’ statements, it is possible to nest one or more of any of these loops inside another.

‘for’ Loop

The ‘for’ loop is useful when the statements within the loop are required to run a specific number of times and this is known in advance. It takes the following form.

for (initialiser; condition; iterator)
{
    # Statement(s) to execute.
}

The initialiser is usually a local variable set up as a loop counter. The condition is an expression that is checked before each iteration of the loop, which must evaluate to true for the statements within the loop to be executed. Finally, the iterator is an expression that is executed at the end of each loop iteration, which usually increments the loop counter.

The following example displays the value of the loop counter for each iteration of the loop.

for ($i = 1; $i -le 10; $i++)
{
    Write-Host $i
}

Here the initialiser is set to one and the condition is checking for a value of less than or equal to ten. With the iterator adding one to the loop counter each time, it means that the statements within the loop will be executed ten times. The output from this loop is shown below.

1
2
3
4
5
6
7
8
9
10

‘while’ Loop

As mentioned above, the ‘while’ loop has a condition, which must evaluate to true before the statements within it can be executed.

while (condition)
{
   # Statement(s) to execute.
}

In the below example, a variable is declared and initialised to one, then the ‘while’ loop checks to see that its value is less than or equal to ten, before displaying it and incrementing the variable by one.

$i = 1
while ($i -le 10)
{
   Write-Host $i
   $i++
}

The output will be the same as in the ‘for’ loop example above. Note that if the variable ‘i’ had been declared and initialised to eleven or above, then the statements within the loop would not be executed at all.

‘do’ Loop

A ‘do’ loop comes in a couple of different forms. It can either be used to execute a statement or group of statements whilst a condition is true, or until a condition is true. In either case, the condition is checked at the end of the loop, so the statements within it will always execute at least once, because the condition doesn’t get tested for the first time until the end of the first iteration.

In this example, the statements within the loop execute while the condition is true, in this case, while the variable ‘i’ is less than or equal to ten. The output is exactly the same as in the above two examples. Note that if ‘i’ is initialised to eleven or more, even though it is above ten, because the condition isn’t tested until the end of the first iteration, one value would be displayed.

$i = 1
do {
    Write-Host $i
    $i += 1
} while ($i -le 10)

The next example, which produces the same results as the other three, executes the statements within the loop until the condition is true, in this case, until ‘i’ is greater than ten. As with the above example, if ‘i’ were initialised to eleven or more, then one value would be displayed before the condition is checked.

$i = 1
do
{
    Write-Host $i
    $i += 1
} until ($i -gt 10)

The ‘break’ and ‘continue’ Statements

The ‘break’ and ‘continue’ statements can be used to alter how the statements within any of the above examples are executed. If the ‘break’ statement is encountered, the program exits the loop and continues on the first line after the end of the loop. The ‘continue’ statement differs from the ‘break’ statement in that it only exits the current iteration of the loop and not the whole loop.

Here the ‘continue’ statement is executed when the variable ‘i’ equals five, which has the effect of the variable value not being displayed. The values one through four and six to ten are displayed.

for ($i = 1; $i -le 10; $i++) 
{
    if ($i -eq 5)
    {
        continue
    }
    Write-Host $i 
}