EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial for loop in PowerShell
 

for loop in PowerShell

Priya Pedamkar
Article byPriya Pedamkar

Updated March 24, 2023

for loop in PowerShell

 

 

Introduction to 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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax:

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

For loop in PowerShell 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 .

Examples

Below are the examples mentioned:

1. Simple For loop example

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

Explanation:

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){
break
}
}

The code execution screen for it is below.

For loop with a break

3. Nested For loop

Explanation:

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'){
$string
}

 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

Conclusion

To conclude, a for loop statement allows us to execute a statement or group of statements multiple times on the basis of certain conditions.

Recommended Articles

This is a guide to For loop in PowerShell. Here we discuss the introduction, 4 Main Sections for loop in PowerShell with the Flow diagram, examples codes and their output. You may also look at the following articles to learn more –

  1. Switch Case in PowerShell
  2. PowerShell Date
  3. Comparison Operator Work in PowerShell
  4. PowerShell Tools

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW