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 met. It is like FOR loop but the structure is different.
Diagram:
Syntax:
While (condition) {
Operation commands
}
Parameters:
- Condition: Conditional parameter is specified here. This condition may be one or more and output will be in the Boolean format ($True or $False). While loop runs based on this condition, and once the condition is false, the loop is terminated automatically.
- Operation Commands: These commands can be simple or complex commands. It runs every time when 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, first of all, initialization of variables and other parameters happen 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 then it again executes the body part. This same process repeats until the condition becomes false. Once the condition is false, the loop is terminated.
If the condition is false at the beginning then the script will not go inside the body function but remember to specify a condition or command that can make loop terminated eventually otherwise the loop will be executed forever. You can also terminate the execution of a block with the BREAK command when the specific criteria 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
Below script will first initialize the variable $i=1 and executes while loop. Here the condition $i value 1 is less than equal to 10, so it enters into the body statement and executes 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 and the condition gets false and the loop is terminated. If you don’t put $i++ then loop executes forever as it script can’t find any termination condition.
Code:
$i=1
while($i -le 10){
Write-Output "Value of i : $i"
$i++
}
Output:
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:
In the above example, if any of the conditions get false (When $i value becomes greater than 10 and $j value becomes less than 7) then loop terminates. Here, a value of $j first reaches to 6 and it can’t satisfy the while loop condition and the loop gets terminated.
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:
Here, in –OR condition, if any of the two statements satisfy then the loop gets executed.
Example #4 – Condition Inside the While Loop
Like normal function, 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:
Example #5 – While Loop with Break Statement
As shown in the above example, the loop executes until the while condition satisfies but if we want a loop to be terminated when if condition 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:
In the above example, when the variable value becomes 8 in the IF statement, then IF block executes and break is also executed and the entire loop is terminated. You can see the difference in the above two examples.
Example #6 – While Loop with Continue Statement
When the CONTINUE statement is executed, 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:
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:
In this example, the first main while loop is executed for the $i value of 0 and then the script executes full 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:
In the above example, while loop executes forever because of the $true is specified in the condition. To terminate the loop, use the Break command when condition satisfies.
Conclusion
While loop has the same functionality as the other loops as FOR, FOREACH, and Do-While. In the Do-While loop, the body statement is executed first and the condition is checked later.
Recommended Articles
This is a guide to PowerShell While Loop. Here we discuss the syntax and working of PowerShell While Loop along with examples and code implementation. You may also have a look at the following articles to learn more –
- Tools of PowerShell
- Syntax and Parameters of PowerShell Sort-Object
- Examples of Logical Operators in PowerShell
- How does Hashtable work in PowerShell?
2 Online Courses | 1 Hands-on Project | 4+ Hours | Verifiable Certificate of Completion
4.5
View Course
Related Courses