EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • 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
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell While Loop

PowerShell While Loop

Chirag Nagarekar
Article byChirag Nagarekar
Priya Pedamkar
Reviewed byPriya Pedamkar

Updated June 23, 2023

PowerShell While Loop

Introduction to PowerShell While Loop

While loop in PowerShell is an iterative loop, which runs until the condition satisfies. You can write multiple lines of code inside the while block and run it several times until the criteria are met. It is like FOR loop, but the structure is different.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Diagram:

Diagram powershell

Syntax:

While (condition) {
Operation commands
}

Parameters:

  • Condition: Conditional parameter is specified here. This condition may be one or more, and the output will be in the Boolean format ($True or $False). While loop runs based on this condition, the loop is terminated automatically once the condition is false.
  • Operation Commands: These commands can be simple or complex commands. It runs whenever the condition is satisfied.

The above syntax is equivalent to,

while (true) {
if (!condition)
break;
statements;
}

How PowerShell While Loop Works?

During the While block in PowerShell, variables and other parameters are initialized first, and then there is a condition check. The condition can be one or more, and based on the condition, if it becomes true, then it executes the Body function; again, it repeats the check after executing the body part; and if the condition is satisfied again, it executes the body part again. This same process repeats until the condition becomes false. Once the condition is false, the loop is terminated.

If the condition is initially false, the script will not go inside the body function. However, remember to specify a condition or command that can eventually terminate the loop; otherwise, the loop will be executed forever. You can also terminate the execution of a block with the BREAK command when the specific criteria are met or skip the current iteration using the CONTINUE statement.

Examples of PowerShell While Loop

Let us see some of the examples of while loop in PowerShell, which are given below:

Example #1 – Using Simple While Loop

The script below will initialize the variable $i=1 and execute the while loop. Here the condition $i value 1 is less than equal to 10, so it enters into the body statement and executes the Write-Output command and then increments the value of $i to 2 and then again checks the condition and enters into a loop. The same process works until the $i value reaches 11, the condition gets false, and the loop is terminated. If you don’t put $i++, the loop executes forever, as the script can’t find any termination condition.

Code:

$i=1
while($i -le 10){
Write-Output "Value of i : $i"
$i++
}

Output:

While Loop in PowerShell eg1

Example #2 – Multiple Conditions with –AND Operator

Code:

$i=5
$j=10
while(($i -le 10) -and ($j -ge 7) ){
Write-Output "Value of i : $i"
Write-Output "`nValue of j : $j"
$i++
$j--
}

Output:

While Loop in PowerShell eg2

In the above example, if any of the conditions get false (When $i value becomes more significant than 10 and $j value becomes less than 7), then the loop terminates. When the value of $j first reaches 6, it fails to satisfy the while loop condition and terminates the loop.

Example #3 – Multiple Conditions with -OR Operator

Code:

$i=5
$j=10
while(($i -le 10) -or ($j -ge 7) ){
Write-Output "Value of i : $i"
Write-Output "`nValue of j : $j"
$i++
$j--
}

Output:

While Loop in PowerShell eg3

In the –OR condition, if any of the two statements satisfy, then the loop gets executed.

Example #4 – Condition Inside the While Loop

Like normal functions, you can specify conditions inside the while loop.

Code:

$i=5
while($i -ge 10){
if($i -eq 8){
Write-Output "Variable value reached to 8"
}
$i++
}

Output:

While Loop in PowerShell eg4

Example #5 – While Loop with Break Statement

As shown in the above example, the loop executes until the while condition is satisfied, but if we want a loop to be terminated when if condition is met, then we need to use the Break command. Once the break command is executed, it terminates all the loops (Parent and Child).

Code:

$i=5
while($i -le 10){
if($i -eq 8){
Write-Output "Break command is going to run now"
Break
}
Write-Output "After break this will not executed"
$i++
}

Output:

eg5

In the above example, the IF block executes when the variable value reaches 8 in the IF statement. After executing the break, it terminates the entire loop. You can see the difference in the above two examples.

Example #6 – While Loop with Continue Statement

When executing the CONTINUE statement, it skips the current loop and returns to the main loop. In the below example, when the value reaches 8, it skips the rest of the program and returns to the main WHILE loop.

Code:

$i=5
while($i -le 10){
$i++
if($i -eq 8){Continue}
Write-Output $i
}

Output:

eg6

Example #7 – Nested While Loop

You can also use the nested WHILE loop. See the example below.

Code:

$i=0
$j=0
while($i -lt 3){
While($j -lt 3){
Write-Output "`ni = $i"
Write-Output "j = $j"
$j++
}
$i++
$j=0
}

Output:

eg7

In this example, the script executes the main while loop first for the $i value of 0; then, it runs the entire inner while loop. Again for the $i=1, the script executes the inner while loop, and it continues until the value of $i meets the condition.

Example #8 – While loop with $TRUE

You can also structure while loop as below.

Code:

$i = 5
while($true){
Write-Output $i
if($i -eq 8){Break}
$i++
}

Output:

eg8

In the above example, while loop executes forever because the $true is specified in the condition. To terminate the loop, use the Break command when the condition satisfies.

Conclusion

Although the while loop shares functionality with other loops like FOR, FOREACH, and Do-While, the Do-While loop executes the body statement first and checks the condition later.

Recommended Articles

This is a guide to PowerShell While Loop. Here we discuss the syntax and working of PowerShell While Loop, examples, and code implementation. You may also have a look at the following articles to learn more –

  1. Tools of PowerShell
  2. Syntax and Parameters of PowerShell Sort-Object
  3. Examples of Logical Operators in PowerShell
  4. How does Hashtable work in PowerShell?
ADVERTISEMENT
All in One Excel VBA Bundle
500+ Hours of HD Videos
15 Learning Paths
120+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
Financial Analyst Masters Training Program
2000+ Hours of HD Videos
43 Learning Paths
550+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Data Science Bundle
2000+ Hour of HD Videos
80 Learning Paths
400+ Courses
Verifiable Certificate of Completion
Lifetime Access
ADVERTISEMENT
All in One Software Development Bundle
5000+ Hours of HD Videos
149 Learning Paths
1050+ Courses
Verifiable Certificate of Completion
Lifetime Access
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

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

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Extended Cyber Monday Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW