You should know about looping if you want to iterate through items until a condition is met in PowerShell. I will show you how to use the while loop in PowerShell in this tutorial with a few examples.
What is a While Loop in PowerShell?
A while loop is a control flow statement that enables you to execute a block of code repeatedly as long as a specified condition evaluates to true. It’s particularly useful when you need to perform repetitive tasks or iterate through items until a certain condition is met.
The while loop checks the condition before executing the block of code. If the condition is true, the code block runs; if the condition is false, the loop terminates, and the script continues executing the subsequent commands.
Syntax of the PowerShell While Loop
The syntax of a while loop in PowerShell is like the below:
while (condition) {
# Code block to execute
}condition: This is the expression that is evaluated before each iteration. If it returnstrue, the loop continues; if it returnsfalse, the loop stops.# Code block to execute: This is the block of code that runs repeatedly as long as the condition istrue.
Now, with a few examples, let us see how to work with the while loop in PowerShell.
PowerShell While Loop Examples
Let us see a few examples of a while loop in PowerShell.
Example 1: Basic While Loop Example
To understand the PowerShell while loop, let us first take a simple example of printing numbers from 1 to 5. Below is the PowerShell script.
$i = 1
while ($i -le 5) {
Write-Output $i
$i++
}In this example:
- We initialize a variable
$iwith the value1. - The condition
$i -le 5checks if$iis less than or equal to5. - Inside the loop, we print the value of
$iand then increment it by1using the++operator.
You can see the output in the screenshot below after I executed the above PowerShell script using VS code.

Example 2: While Loop with Array Example
Here is an example of how to use the PowerShell while loop to iterate through an array of strings and print each element.
$fruits = @('Apple', 'Banana', 'Cherry', 'Date', 'Elderberry')
$i = 0
while ($i -lt $fruits.Length) {
Write-Output $fruits[$i]
$i++
}Here:
- We define an array
$fruitscontaining a list of fruit names. - The condition
$i -lt $fruits.Lengthensures that the loop runs as long as$iis less than the length of the array. - We print each fruit name and increment
$iby1.
I also executed the above script, and you can see the exact required output in the screenshot below:

Example-3: While Loop With Variables and Operators
In this example, let me show you how to use variables and operators in a PowerShell while loop.
We can use PowerShell Variables, which hold values that change with each iteration, to effectively control the loop’s flow.
Here is an example:
$counter = 0
$maxLimit = 20
while ($counter -lt $maxLimit) {
if ($counter % 3 -eq 0) {
Write-Output "Counter $counter is divisible by 3"
}
$counter++
}This loop uses the $maxLimit variable to determine how many times the loop will iterate. It checks if $counter is divisible by 3 before printing a message.
In the same way, we can use logical operators like -and, -or, and -not to make conditions more flexible.
Here is the complete PowerShell script:
$isRunning = $true
$counter = 0
while ($isRunning -and $counter -lt 10) {
Write-Output "Counter is $counter"
$counter++
if ($counter -eq 5) {
$isRunning = $false
}
}In this example, the loop stops early when $counter reaches 5 because $isRunning is set to false.
Read PowerShell Do-Until Loop Examples
How to Use Break and Continue in While Loop
Let me show you now how to use the break and continue keywords to control how the for loops execute in PowerShell.
Break Statement
The break statement is used to exit the loop prematurely, regardless of the condition. It can be useful when you need to terminate the loop based on a specific condition inside the loop.
Here is an example of how to use the break in a for loop in PowerShell.
$i = 1
while ($i -le 10) {
if ($i -eq 5) {
break
}
Write-Output $i
$i++
}In this example:
- The loop starts with
$iinitialized to1and runs while$iis less than or equal to10. - When
$iequals5, thebreakstatement is executed, terminating the loop. - As a result, the loop prints numbers
1to4and then stops.
The screenshot below shows the exact output after I executed the above script.

Continue Statement
The continue statement skips the remaining code in the current iteration and moves to the next iteration of the loop. It’s useful to skip certain iterations based on a condition.
Here is an example of how to use the continue statement in a for loop in PowerShell.
$i = 1
while ($i -le 10) {
$i++
if ($i % 2 -ne 0) {
continue
}
Write-Output $i
}In this example:
- The loop runs while
$iis less than or equal to10. - We increment
$iat the beginning of each iteration. - If
$iis an odd number ($i % 2 -ne 0), thecontinuestatement skips the rest of the code in the current iteration. - As a result, only even numbers are printed.
You can also look at the screenshot below for the output:

Read Create and Use Functions in PowerShell
How to Use Multiple Conditions in While Loop in PowerShell
In a PowerShell while loop, we can also use multiple conditions. Let me show you how.
In PowerShell, you can use multiple conditions in a while loop by combining them with logical operators such as -and, and -or. The while loop will continue to execute as long as the combined condition is evaluated as true.
Syntax
The basic syntax for a while loop with multiple conditions is:
while (<condition1> -and <condition2> -and <condition3>) {
# Code to execute as long as all conditions are true
}or
while (<condition1> -or <condition2> -or <condition3>) {
# Code to execute as long as at least one condition is true
}Example
Here’s an example that explains a while loop with multiple conditions using the -and operator. This loop will continue to run as long as both conditions are true:
$counter = 0
$limit = 10
$continue = $true
while ($counter -lt $limit -and $continue) {
Write-Output "Counter is $counter"
$counter++
# Simulate a condition to exit the loop
if ($counter -eq 5) {
$continue = $false
}
}In this example:
- The loop will continue as long as
$counteris less than$limitand$continueis$true. - Inside the loop, the
Write-Outputcmdlet prints the current value of$counter. - The
$countervariable is incremented by 1 in each iteration. - When
$counterreaches 5,$continueis set to$false, causing the loop to exit after the current iteration.
Here is the output you can see in the screenshot below:

Read How to Use Multiple Conditions in PowerShell If Else Statement?
Nested While Loop in PowerShell
This is very useful; you should know how to work with nested while loops in PowerShell.
Nested while loops in PowerShell are used when you need to perform iterative operations within another set of iterative operations.
Nested While Loop Syntax
The basic syntax for nested while loop is:
while (<outer condition>) {
while (<inner condition>) {
# Code to execute in the inner loop
}
# Code to execute in the outer loop
}Nested While Loop Example
Here’s an example that shows nested while loops by generating a simple grid of coordinates:
# Initialize outer loop variables
$x = 0
$xLimit = 3
# Outer while loop
while ($x -lt $xLimit) {
# Initialize inner loop variables
$y = 0
$yLimit = 3
# Inner while loop
while ($y -lt $yLimit) {
# Display the current coordinates
Write-Output "Coordinates: ($x, $y)"
# Increment the inner loop variable
$y++
}
# Increment the outer loop variable
$x++
}In this example:
- The outer
whileloop iterates over thexcoordinate. - The inner
whileloop iterates over theycoordinate. - For each iteration of the inner loop, the current coordinates
(x, y)are displayed usingWrite-Output. - The inner loop variable
$yis incremented in each iteration of the inner loop. - After the inner loop completes, the outer loop variable
$xis incremented, and the process repeats until the outer condition is no longer true.
If you execute the above code using VS code, you can see the output in the screenshot below:

Conclusion
I hope you got an idea of how to work with while loop in PowerShell with various examples.
I have also explained how to use break and continue in a PowerShell while loop and how to use multiple conditions in a while loop in PowerShell. I explained how to work with nested for loop in PowerShell at the end using an example.
If you still have any questions, feel free to let us know in the comment box below.
You may also like the following tutorials:
- PowerShell ForEach Loop
- How to Handle Errors with Try-Catch in PowerShell?
- How to Use Multiple Conditions in Do-While Loop in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.