How a PowerShell For Loop Can Run a Command Multiple Times

The For loop is likely one of the most valuable tools in any programming language, and  Microsoft PowerShell is not any totally different. You can use different loops to repeat instructions, however the For loop is maybe essentially the most simple.

From iterating over arrays to finishing up a perform a predetermined variety of occasions, there are lots of issues you possibly can obtain with this software. Here is a tutorial on the best way to use For loops in PowerShell.

How a PowerShell For Loop Can Run a Command Multiple Times

What Is the Use of For Loops in PowerShell?

Unlike Command Prompt, PowerShell is a full scripting surroundings. This means you possibly can write reusable PowerShell scripts to mechanically perform duties relatively than getting into instructions manually. And For loops are the important thing to writing these modules.

A For assertion repeats a script block a particular variety of occasions, modifying a given variable to maintain monitor of development. This permits you to use the loop for some relatively attention-grabbing situations.

How a PowerShell For Loop Can Run a Command Multiple Times

You can generate sequences of numbers, decide a prime quantity, or show a countdown timer. More virtually, you possibly can iterate over an array of objects, performing some motion with every entry.

The Syntax of a For Loop in PowerShell

For loops work in Windows PowerShell the identical means they do in any programming language. Expressions initializing the monitoring variable, testing its worth, and modifying the variable are ensconced throughout the brackets following “For,” separated by semicolons. Then comes the assertion listing itself sure by curly braces.

For (Initialization; Condition; Update)

    Script Block

How to Use the For Loop in a PowerShell Script

Using the For loop is fairly easy. Whenever you encounter a state of affairs that requires repeating a barely various step, you need to put it into a For loop.

How a PowerShell For Loop Can Run a Command Multiple Times

Let’s say that you must write a code snippet that may discover the sum of all pure numbers till a quantity n, contained within the variable $n. Here is a primary For loop instance:

$n = 10

$sum = 0

For ($i = 1 ; $i -le $n ; $i++)

    $sum = $sum + $i

“The sum of $n natural numbers is $sum”

How a PowerShell For Loop Can Run a Command Multiple Times

Accessing Arrays Through a For Loop

Generating numeric sequences is hardly what most individuals use PowerShell for. A extra frequent utilization is to iterate over an array.

Say you have got an array $week with seven parts. You can output the listing of the times contained within the array utilizing a easy For loop, as demonstrated within the following instance.

For ($i = 0 ; $i -lt $week.Length ; $i++)


How a PowerShell For Loop Can Run a Command Multiple Times

Using the ForEvery Loop To Quickly Iterate Through an Array

Another type of the For loop is the ForEvery assertion. This model simplifies going by means of the contents of a PowerShell array and processing them individually. The earlier code snippet, for instance, may be rewritten like this:

Foreach ($day in $week)


How a PowerShell For Loop Can Run a Command Multiple Times

When coping with extra complicated objects, you may as well use the Foreach-Object cmdlet to carry out an motion on the contents of any PowerShell command.

How is the For Loop Different From Other Types of Loops?

The For loop isn’t the one sort of looping assertion out there to you. Like most programming languages, PowerShell has a number of forms of loops.


The easiest of those is the While loop. All you have got is a situation and a script block, and the loop runs so long as the check expression evaluates to true. Since the expression is evaluated first, there may be a likelihood for the code to not run in any respect or find yourself as an infinite loop. 

While (Condition)

    Script Block

How a PowerShell For Loop Can Run a Command Multiple Times


If you might be writing a script that should run no less than as soon as – even when the expression evaluates to false—then you should utilize the Do-While loop. The solely distinction from the While loop is that it locations the command block earlier than the situation, which implies that the code is executed earlier than the check expression is checked for the primary time.


    Script block

While (Condition)

How a PowerShell For Loop Can Run a Command Multiple Times


Another model of this loop is Do-Until. Basically, it runs the code block after which repeats it till the check expression is true – the reverse of a Do-While loop. Not a significantly helpful construction, as you possibly can obtain the identical factor by modifying the situation in a customary Do-While loop.


    Script Block

Until (Condition)

How a PowerShell For Loop Can Run a Command Multiple Times

Why the For Loop is Better

The downside with all these different looping constructions is that they don’t embody the preliminary worth or replace statements. You need to manually bear in mind to create a variable exterior the loop after which increment it (or decrement it) throughout each go of the loop.

As you would possibly count on, programmers usually overlook this step, resulting in a script that doesn’t work as supposed. This wastes treasured time in debugging.

The For loop avoids this concern by necessitating the initialization and increment expressions throughout the beginning brackets. This results in cleaner, extra sturdy scripts.

When Should You Use the For Loop in PowerShell?

PowerShell is all about creating scripts for automation. And most likely essentially the most useful gizmo for attaining that is the For loop. You can use it to interchange many copy-paste operations with a extra compact, elegant script.

The most typical perform of this loop is to iterate over an array, one entry at a time. For an much more streamlined script, you should utilize a ForEvery loop as effectively. You can even use the For loop to generate numerical sequences, though that’s hardly ever wanted.

The For loop scores over different looping algorithms by together with all important features of a loop into the parameters, stopping any errors as a result of forgetting these statements. This makes the For loop indispensable in any state of affairs calling for repeating a set of instructions.