Basics of PowerShell Looping: Foreach

Summary: Microsoft Scripting Guy, Ed Wilson, talks about using the Windows PowerShell Foreach statement to loop through a collection.

Microsoft Scripting Guy, Ed Wilson, is here. When the Scripting Wife and I were in Amsterdam, Windows PowerShell MVP, Jeff Wouters, told me that a lot of people he ran across had problems looping through collections with Windows PowerShell.  Here is a picture of Jeff and me.

Photo of Ed and Jeff

Basics of looping

Looping is a fundamental Windows PowerShell concept. Actually, I take that back. It is a fundamental concept of any programing language, even batch languages. So what is the problem?

Most people coming to Windows PowerShell for the first time understand about variables. For example, if I store a value in a variable, and if I want to get at that value, it is no problem. I address the variable as shown here:

PS C:\> $a = 5

PS C:\> $b = 6

PS C:\> $c = 7

PS C:\> $a

5

PS C:\> $b

6

PS C:\> $c

7

One thing that makes Windows PowerShell easy to use, is that it automatically unravels arrays. An array is when I add more than one thing to a variable. For example, earlier I assigned three values to three variables. Now, I want to add those three variables to a single variable. So I will use $d to hold an array comprised of $a, $b, and $c. In Windows PowerShell, it is easy to see the values. I just call the variable as shown here:

PS C:\> $d = $a,$b,$c

PS C:\> $d

5

6

7

I can also access the values of the variables by position in the array. The first position is [0], and the last position in our array is [2]. So I can access specific elements from the array by using the position numbers. This is shown here:

PS C:\> $d[0]

5

PS C:\> $d[1]

6

PS C:\> $d[2]

7

PS C:\>

Walking through the array

Suppose I want to add the number five to each of the three values I have in $a, $b, and $c. If I work with them individually, it is easy. I just do the following:

PS C:\> $a + 5

10

PS C:\> $b + 5

11

PS C:\> $c + 5

12

PS C:\>

The problem comes with the values I have in my array that is in the $d variable. In Windows PowerShell, if I add 5 to my $d variable, I end up actually adding the value as another element in the array. This is shown here:

PS C:\> $d + 5

5

6

7

5

To add the number five to each of the elements in the array, I need to walk through the array by using the Foreach command. To use the foreach command, I need to do three things:

  1. I call the Foreach command.
  2. I use a pair of parentheses, I use a variable for my place holder (enumerator), and I use the variable that is holding the collection.
  3. I use a pair of curly braces (script block) that includes the script that does what I want to do.

The placeholder variable I use represents the current item from the collection that I will be working with. The variable only gets a value inside the script block, and it will always be a different item each time I loop through the collection. The Foreach command is shown here:

Foreach (placeholder variable IN collection)

{

  What I want to do to the placeholder variable

}

In my example, the Foreach command is shown here:

Foreach ($i in $d)

{

 $i + 5

}

Here's the entire script:

$a = 5

$b = 6

$c = 7

$d = $a,$b,$c

Foreach ($i in $d)

{

 $i + 5

}

The command and the output from the command are shown in the following image:

Image of command output

That is all there is to using Foreach to loop through a collection. Looping Week will continue tomorrow when I will talk about using Foreach-Object in the pipeline.

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