Like most procedural programming languages, PowerShell supports a variety of loops. A specialty of Microsoft’s scripting language is that it not only offers a foreach command but also a Foreach-Object cmdlet that allows you to iterate through the elements of an array.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication

If you have been working with other programming languages, you will quickly feel at home with PowerShell’s various loop types. Their functionality and syntax is based on loops in C and derived languages such as JavaScript. Thus, the loop condition is usually enclosed in parentheses and the loop body in braces. However, the conditional operators that you require for the loop condition need getting used to. Apart from that, PowerShell’s keywords are case-insensitive, so you can use for, For, or FOR.

Loops for different purposes ^

The different loop types cover all thinkable application scenarios. For instance, PowerShell supports pretest and posttest loops that enable you to evaluate the boolean condition either at the beginning or at the end of the loop body. If you need a loop counter that you want to initialize and increment in the loop condition, you can work with a for loop. However, if you just have to check a certain condition, you can make use of a while loop.

For with integrated loop counter ^

PowerShell’s for loop doesn’t bring any great surprises and behaves as in other programming languages.

This simple example initializes the loop counter $i with the value 1. It then increments the counter by one with each iteration until it reaches 10, which is the condition that quits the loop.

While loop ^

The while loop works just as you might know it from other languages. It is called a pretest loop because the instructions in the loop body are not executed, even once, if the loop condition doesn’t match. In contrast to the for loop, the condition can only contain a boolean expression. If you want to use a loop counter, you have to initialize it before the while statement and increment or decrement it in the loop body. The following snippet demonstrates how you could use while for a simple menu system. With each iteration, the user is asked to press a key to select a menu item. Entering “Q” terminates the loop.

while loop example

while loop example

The while loop tests the condition before the first iteration. As you can see in the example, you can also call cmdlets and assign values in the loop condition.

In addition, PowerShell supports the posttest loops do-while and do-until. In both cases, the instructions in the loop body are executed at least once because the condition is at the end of the loop.

From a syntactical point of view, do-while and do-until are identical. The difference is logical in nature. do-while continues to run as long as the condition is true and terminates when the condition is no longer fulfilled; do-until works the other way around: it quits when the condition takes the value TRUE. The general structure looks like this:

Basically, do-until and do-while can replace each other if you negate the condition.

Loop control with break and continue ^

PowerShell offers two additional ways to control loops. The commands break and continue are known from other languages and are rarely required in well-structured scripts. Before you work with these language elements, you should try to avoid them by using another algorithm. Code that uses break and continue is hard to read and can have unwanted side effects.

The break command can be used not only in all loop types but also in switch blocks. The command always has the same effect—that is, the block will be terminated and the control is transferred to the parent block. The break command is comparable to exit for and exit do in VBScript.

Using break in loops quits the iteration, and the following code outside the loop will be executed. For nested loops, this would be the higher order for-, while-, foreach-, or do block. The only situation where the usage of break likely makes sense is within an if statement because otherwise the loop wouldn’t be iterated a second time.

Break with goto ^

A specialty of PowerShell’s break command is that you can combine it with a label. This works similar to the notorious GoTo in Basic. The only exception is that the targets have to be in the same line with a for-, while-, foreach-, or do- command.

In this construct, the program would jump to the do loop when the if condition within the while block applies.

Continue command ^

As with the break command, the continue command terminates the execution of the loop body. In contrast to break, continue doesn’t direct the program flow to the code after the loop; instead, it resumes the execution of the loop in the next iteration. Here, again, use continue sparingly!

Even though PowerShell comes with the complete loop weaponry of compiled languages, you will rarely exploit its entire potential because scripts usually have a relatively simple structure.

Clearly, the most frequent use of loops, especially in interactive mode, is the iteration through the elements of an array because many cmdlets return this data type.

Implicit iteration through array elements

Because of this, Microsoft simplified such operations and allows admins to choose between different methods. Starting with PowerShell 3.0, the simplest and most elegant one is the implicit foreach that, in some cases, allows you to avoid this keyword. The example below demonstrates how you can retrieve a certain property of all array elements without using a loop or Select-Object.

This call of Get-ADUser returns all Active Directory user objects as an array. The above command displays the last name of each element.

Foreach and Foreach-Object ^

If you want to do more than just display a property of the array elements (for instance, access and process their values), you’ll need an explicit loop. For these cases, PowerShell offers the foreach command and the Foreach-Object cmdlet. This may create confusion because the latter has the predefined alias foreach (in addition to “%”).

The differences between the PowerShell and the VBScript version of foreach are only syntactical in nature. In PowerShell, the keyword is not for each but foreach, and, instead of closing the loop body with a next command, you use braces.

These lines fulfill the same purpose as the previous example with the implicit loop. However, the result of Get-ADUser is assigned to the variable $user. Afterward, you need an additional variable ($u), which always contains the current element of the array.

If you want to solve the above problem with Foreach-Object, you have to pipe the output of Get-ADUser to Foreach-Object and use the automatic variable $_ to refer to the current element, like this:

The command in this example doesn’t just retrieve the last names of the AD users but also uses the ToTitleCase method to convert the initial letters into capital letters if the property surname is unequal to NULL. Notice that I used the alias “%” for Foreach-Object. The parentheses around the Get-Culture cmdlet (which gets information such as current language settings) ensure that the TextInfo property is applied to the result (an object) returned by the cmdlet.

Since commands with the Foreach-Object and the implicit foreach are shorter and require less typing (not just because of the alias “%”), no reason appears to exist to work with the foreach command itself. However, when you have a large number of loop passes with relatively complex calculations in the loop body, the conventional foreach loop is significantly faster.

Win the monthly 4sysops member prize for IT pros


Related Posts

  1. Emmanuel 2 years ago

    Thank you for the info.

    A small correction is needed in this text:
    "do-while continues to run as long as the condition is true and terminates when the condition is no longer fulfilled; do-while works the other way around:"

    The second "do-while" should be "do-until"


  2. Wolfgang Sommergut 2 years ago

    @Emmanuel: You are right. Thank you!


  3. Michael Pietroforte 2 years ago

    I corrected the sentence now. Thanks!


  4. robk 2 years ago

    can you have multiple statements inside while?
    While ($a=1 -or $a=2 -or $a=3)?


Leave a reply

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



Please ask IT administration questions in the forum. Any other messages are welcome.

© 4sysops 2006 - 2017

Log in with your credentials


Forgot your details?

Create Account