PSMonday #35: December 26, 2016

Topic: Foreach

Notice: This post is a part of the PowerShell Monday series — a group of quick and easy to read mini lessons that briefly cover beginning and intermediate PowerShell topics. As a PowerShell enthusiast, this seemed like a beneficial way to ensure those around me at work were consistently learning new things about Windows PowerShell. At some point, I decided I would share these posts here, as well. Here’s the PowerShell Monday Table of Contents.

A day late, but here we go.

Like the If statement, the foreach statement is an often used language construct. In fact, I wouldn’t be surprised to find out that it was the second most used, right after the If statement. The foreach differs, however, in that it is a looping construct. This is to say that we use it to loop through a collection, or a group, of items.

As we’ve done in the past, let’s start with the conceptual format of the foreach construct. There’s always a few ways to write the same thing. The first part of this example, shows how it’s displayed in the PowerShell help, and the second part, is how I write, my foreach statements.

foreach ($<item> in $<collection>){<statement list>}

Foreach ($<item> in $<collection>) {
    <statement list>
}

The most common collection we’ll loop through using a foreach is an array. An array is a data structure, designed to store a collection of items. Before we get there, consider that many variables are only ever assigned a single value, such as in the below example.

$Computer = 'Server01'
$Computer

Server01

In the previous example, the Computer variable only held a single, string value of “Server01.”

An array lets us get away with a single variable holding multiple values. Let’s start with the multiple values of ‘red’, ‘blue’, ‘green’, and ‘gray’. Each of these is an item, but together, they are a collection; they’re an array. Before we actually work with this array, we’ll assign it to the Colors variable. The decision to use a plural variable, is intentional.

$Colors = 'red','blue','green','gray'
$Colors

red
blue
green
gray

With this variable set, we can loop through each item in the collection — each value in the variable — using foreach. The first time we enter the foreach, the Color variable (singular), will be ‘red.’ The second time we loop through, the Color variable is ‘blue.’ The third iteration into the foreach, it’s ‘green,’ and on the final pass, the Color variable is ‘gray.’

Foreach ($Color in $Colors) {
    Write-Output -InputObject "The `$Color variable is: $Color"
}

The $Color variable is: red
The $Color variable is: blue
The $Color variable is: green
The $Color variable is: gray

This item variable, could’ve been any word; however, we chose to use Color, because it made the most sense. The decision to use a single variable was intentional, too. Here’s the same example as above, however, we’ve changed Color to Hamburger. It makes no sense, but it still works.

Foreach ($Hamburger in $Colors) {
    Write-Output -InputObject "The `$Hamburger variable is: $Hamburger"
}

The $Hamburger variable is: red
The $Hamburger variable is: blue
The $Hamburger variable is: green
The $Hamburger variable is: gray

We’ll likely wrap up the foreach language construct next week, just in time to work with the ForEach-Object cmdlet. They’re different.

Leave a Reply

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