
Learn how to loop in PowerShell with this detailed guide. Explore For, ForEach, While, Do-While, and Do-Until loops in PowerShell along with some practical examples. PowerShell is a powerful scripting language and automation framework developed by Microsoft
Table of Contents
One of the fundamental concepts in PowerShell scripting is looping, which allows you to execute a block of code multiple times efficiently and in this article we’ll learn how to loop in PowerShell.
How to Loop in PowerShell (Introduction)
Loops enable scripts to execute code repeatedly based on a specified condition. PowerShell supports various loop constructs, including:
- For Loop
- ForEach Loop
- While Loop
- Do-While Loop
- Do-Until Loop
- ForEach-Object Loop
Each loop serves a different purpose and is useful in various scenarios. Let’s explore them in detail with examples.
2. For Loop
The For
loop runs a block of code a specific number of times. It consists of three parts:
- Initialization: Sets the starting value.
- Condition: Defines the stopping point.
- Iteration: Updates the loop variable.
Syntax:
for ($i = 1; $i -le 5; $i++) {
Write-Output "Iteration number: $i"
}
Example: Print numbers from 1 to 5
for ($i = 1; $i -le 5; $i++) {
Write-Output "Number: $i"
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
The For
loop is ideal when you know the exact number of iterations.
3. ForEach Loop
The ForEach
loop iterates over each element in a collection, making it useful for processing arrays and lists.
Syntax:
foreach ($item in $collection) {
# Code to execute
}
Example: Iterate over an array of fruits
$fruits = @("Apple", "Banana", "Cherry")
foreach ($fruit in $fruits) {
Write-Output "Fruit: $fruit"
}
Output:
Fruit: Apple
Fruit: Banana
Fruit: Cherry
The ForEach
loop is ideal for working with arrays, collections, and lists.
4. While Loop
The While
loop runs as long as a condition remains true.
Syntax:
while (condition) {
# Code to execute
}
Example: Print numbers from 1 to 5 using While Loop
$i = 1
while ($i -le 5) {
Write-Output "Count: $i"
$i++
}
Output:
Count: 1
Count: 2
Count: 3
Count: 4
Count: 5
The While
loop is useful when you don’t know the exact number of iterations beforehand.
5. Do-While Loop
The Do-While
loop executes the code block at least once before checking the condition.
Syntax:
do {
# Code to execute
} while (condition)
Example: Execute the loop at least once
$i = 1
do {
Write-Output "Iteration: $i"
$i++
} while ($i -le 5)
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
The Do-While
loop ensures that the code runs at least once, even if the condition is false initially.
6. Do-Until Loop
The Do-Until
loop works similarly to Do-While
, but it runs until the condition becomes true.
Syntax:
do {
# Code to execute
} until (condition)
Example: Keep looping until $i
reaches 5
$i = 1
do {
Write-Output "Iteration: $i"
$i++
} until ($i -gt 5)
Output:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
The Do-Until
loop ensures execution continues until the condition is met.
7. ForEach-Object Loop (Pipeline Processing)
The ForEach-Object
loop processes items from the pipeline, making it useful for handling large datasets.
Syntax:
$collection | ForEach-Object { $_ }
Example: Process an array using ForEach-Object
$numbers = 1..5
$numbers | ForEach-Object {
Write-Output "Number: $_"
}
Output:
Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
The ForEach-Object
loop is optimal for working with pipeline objects.
8. Using Break and Continue Statements
Break Statement
The Break
statement stops the loop immediately.
for ($i = 1; $i -le 10; $i++) {
if ($i -eq 5) {
break
}
Write-Output $i
}
Output:
1
2
3
4
Continue Statement
The Continue
statement skips the current iteration and proceeds to the next.
for ($i = 1; $i -le 5; $i++) {
if ($i -eq 3) {
continue
}
Write-Output $i
}
Output:
1
2
4
5
9. Nested Loops
PowerShell allows loops within loops.
for ($i = 1; $i -le 3; $i++) {
for ($j = 1; $j -le 2; $j++) {
Write-Output "Outer: $i, Inner: $j"
}
}
Output:
Outer: 1, Inner: 1
Outer: 1, Inner: 2
Outer: 2, Inner: 1
Outer: 2, Inner: 2
Outer: 3, Inner: 1
Outer: 3, Inner: 2
Conclusion
PowerShell provides several looping mechanisms, each suited to different tasks. Understanding how to use loops effectively helps in automating administrative tasks and processing data efficiently. Choosing the right loop depends on the scenario—whether working with fixed iterations, collections, or pipelines. Mastering loops in PowerShell enhances productivity and script efficiency.