2. Control Statements

for loop in PowerShell

Once you jump into dipper coding you will face various kinds of situation, there may be situations when you required to execute certain expressions or statements repetitive. It may be reading each element of an array or it may be reading each line of a file, in all these cases we need to read again and again. ”for” Loop in PowerShell serve the same purpose, it provides a very powerful mechanism to execute certain code block again and again according to requirements.  For example, an array containing users and we want to check users with the name “Ajay”, so we need to check each element and for that, we need a “for”  loop and once the user “Rajya” found loop will exit. Before going to use ‘for’ loop, always remember loop are very much resource consuming, so we should try to write code in such a way that once we achieved our goal, the loop should stop.


for (<Initial value>; <Condition expression>; <Incremental>)
<Statement 1>
<Statement 1>

4 Main Sections of for Loop in PowerShell

For loop consists of 4 main sections, explanations are given below:

1. Initial value

The initial value is an integer value that starts from 0 to any number, this section executes once for the first time.In this section we initialise variable with initial value,example ($i = 0),($i = 0), ($j = 0).

2. Condition expression

In this part, on each repetition, it checks for condition true or false, so if the condition is true then it will go inside for loop and execute Statement1 and Statement2. Example $i -lt 10 , here if $i is less than 10 then Statement1 and Statement2 will execute.

3. Incremental

On every iteration value of $i will get increased, decreased or changed according to operator, examples $i++ here value will increase by 1 on each iteration,$i– in this case value will decrease by 1 on each iteration,$i+2 in this case value of $i will increase by 2 on every iteration.

4. Statement

In this section, one can write a piece of code or some group statement. In this section, we can also write conditions for braking or halting loop according to our requirements.

Flow diagram

The above diagram clearly shows 3 sections Initialisation, Condition, and Increment. If the value of $i is less than 10, that means TRUE. So the Statement block will execute which is statement1 and statement2, after that it will go to Increment block and increased the value of $i by 1. Once the value of $i will reach to 10 conditions will be FALSE and for loop will stop.

How for loop works in Powershell?

Once for loop start it first allocate initial value of variable into memory, this happens once when for loop get started. On the each iteration value of $i will change , along with checking condition(if condition is true or false). An examples below.

for ($i = 0; $i -lt 10; $i++) { Write-Host $i } .Here it will keep printing 0,1,2,3,4,5…..99 .Here Initial value of $i was 0 and over each iteration it’s value becomes 1,2,3,4,5,6…100 . Once the value of $i will reach 100 ,it will stop further execution and it would not print 100 as condition “$i -lt 100” get failed .


Below are the examples mentioned:

1. Simple For loop example

for($i=1; $i -le 10; $i++){Write-Host $i}


It will iterate for each value of $i till 10 , since our condition is $i -le 10 , which is <=(less than equal). So till 10 iterations will continue.Output of Program is 1,2,3,4,5,6,7,8,9,10.

The code execution screen for it is below.

for loop in powershell

2. For loop with a break

In certain situations, we may need to break our loop on specific conditions. In the example below, we are breaking the loop once it reached 12. So the output for code below will be 1,2,3,4,5,6,7,8,9,10,11,12 .

for($i=1; $i -le 20; $i++){
Write-Host $i
if ($i -eq 12){

The code execution screen for it is below.

For loop with a break

3. Nested For loop


We should always try to find an alternative way instead of nesting for loop, as it’s the time complexity is very high (time complexity is the measure for execution time for any program). For any nested for loop time complexity is O(n^2). A nested loop is a “for loop” within a “for loop” or an inner loop will be the statement for the outer loop. We can put a break in any inner or outer loop to halt the loop.

For($i=1;$i -le 5;$i++){
For($j=1;$j -le $i; $j++){
Write-Host "R" -NoNewline
Write-Host ""

The code execution screen for it is below.

nested for loop

4. For loop for multiplication table of 25

This is a table of 25, on each iteration, it multiplies 25 with increased number till $i value reached 10.

For ($i=1; $i -le 10; $i++) {
"25 * $i = $(25 * $i)"
For loop for multiplication table of 25

5. For loop on the string

In PowerShell length is the keyword to know the length of any string, so here we are checking if the length of a string is less than equals 5 or not(<=5). Once the length of string reached 6, loop exits. So the output id R, RR, RRR, RRRR, RRRRR. Here RRRRRR<=5, which means it reached 6 letters which is larger than 5.

For($string='' ;$string.length -le 5;$string=$string+'R'){
 Length of any String

6. For loop on the array

$number = “ranjan”,”vijay”,”raj”,”suraj”
for ($i = 0; $i -le ($number.length - 1); $i += 1) {
$number[$i] }

Here it is printing all the array memory value from 0-3. So in the first iteration value of $i was 0 so it prints attribute at 0 location which was “Ranjan”, the same way on the 2nd iteration it prints attribute on the memory location 1 and finally it will print the last attribute on the memory location of 3.

The Array Memory Value

7. Infinite loop

Here we are not giving any initial value, we have not given any condition and not even any increment so it will keep printing and will go till infinite.

For(  ;  ;  ) {
"Boom Boom Boom"
Initial Value

Leave a Reply

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