PSMonday #39: January 23, 2017

Topic: For

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.

This week we’ll discuss the For loop. The first of the two below commands can be used to open the For help file. Inside the file you’ll find the command layout, as we’ve seen with the other language constructs, and cmdlets. The way I write the For language construct, has been included below.

Get-Help -Name about_For -ShowWindow

For (<init>; <condition>; <repeat>) {
    <statement list>
}

The For loop begins with the For keyword and a set of parenthesis. Inside the parenthesis is an initialization (listed as init above), followed by a semi-colon. Following the initialization, we include a condition. By now, you undoubtedly realize there will always be some sort of required condition within our language constructs. Foll­owing the second semi-colon, we have a way to increment our initialization variable, as an assurance we’ll loop through the For loop as many times as is indicated by our condition. This may make more sense, very shortly.

We’re only going to look at a single example for the For loop, but we’ll be sure to highlight all the key features, as well as fully walk through the example. Take a look at the below example, and then we’ll discuss it thoroughly.

For ($i = 1; $i -le 10; $i++) {
    "The value of the `$i variable is $i."
    Start-Sleep -Seconds 1
}

We begin our For statement by entering the For keyword, followed by a set of parenthesis. To initialize the For construct, we’ll set a variable, $i, to the numeric value of 1, as $i = 1. We could have used a different number, however, it would require some changes in the areas we’ll discuss next. We could’ve use a different variable, too, had we opted to do so.

Our condition, $i -le 10, indicates to continue to loop through this For construct, so long as $i is less than, or equal to, 10. The final value in the parenthesis, $i++, increments the value in $i by 1, for each loop iteration. It’s shorthand for $i = $i + 1. Inside the curly brackets is where we enter our statement(s); this is where we do our work. In our example, we’ll echo a string that includes the value of the $i variable, and then sleep, or pause, for one second. Here’s the results of our example.

The value of the $i variable is 1.
The value of the $i variable is 2.
The value of the $i variable is 3.
The value of the $i variable is 4.
The value of the $i variable is 5.
The value of the $i variable is 6.
The value of the $i variable is 7.
The value of the $i variable is 8.
The value of the $i variable is 9.
The value of the $i variable is 10.

There’s some confusion about when to use a foreach variation (foreach and ForEach-Object), and the For construct. I use For when I know how many times I need to loop, as this is a requirement. If you know this piece of information, then you might consider the For loop over the other two looping constructs we’ve discussed thus far. Think back to foreach and ForEach-Object: the number of times we were going to loop, was never a consideration before we began.

We’ll be back next Monday where we’ll cover another looping construct: the Do-While loop.

Leave a Reply

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