- Courses
- 💻Programming Languages
- 🪟PowerShell
- Scripting Fundamentals
- Control Flow
- Loops
On this page
Loops
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Loops allow a script to repeat an action multiple times. Each loop type has a specific purpose and syntax. This section explains how each loop works, how the syntax is structured, and provides examples that students can run directly on their machines.
1. for Loop
The for loop is used when you know exactly how many times you want to repeat something.
Its syntax mirrors C‑style loops:
for (<initialization>; <condition>; <iteration>) {
<code>
}
- initialization: runs once before the loop starts
- condition: checked before each iteration
- iteration: runs after each iteration
Example: counting from 1 to 5
for ($i = 1; $i -le 5; $i++) {
"Iteration: $i"
}
Explanation:
$i = 1sets the starting value.$i -le 5keeps the loop running while$iis 1 through 5.$i++increases$iby 1 each time.- The loop prints:
Iteration: 1
Iteration: 2
Iteration: 3
Iteration: 4
Iteration: 5
Example: summing numbers
$sum = 0
for ($i = 1; $i -le 5; $i++) {
$sum += $i
}
$sum
This calculates 1 + 2 + 3 + 4 + 5 and prints 15.
2. foreach Loop
foreach is used to iterate through each element in a collection (arrays, lists, command output).
Syntax:
foreach (<item> in <collection>) {
<code>
}
Example: iterating through an array
$colors = "red", "green", "blue"
foreach ($color in $colors) {
"Color: $color"
}
Explanation:
$colorscontains three strings.- On each iteration,
$colorholds one element. - The loop prints each color.
Example: processing files
$files = Get-ChildItem
foreach ($file in $files) {
$file.Name
}
Explanation:
Get-ChildItemreturns an array of file objects.$file.Nameprints the name of each file.
3. while Loop
while repeats as long as a condition remains true.
Syntax:
while (<condition>) {
<code>
}
The condition is checked before each iteration.
Example: count down from 5
$i = 5
while ($i -gt 0) {
"Countdown: $i"
$i--
}
Explanation:
$istarts at 5.- The loop runs while
$iis greater than 0. $i--decreases the value each time.
Example: waiting for a condition
$number = 0
while ($number -lt 3) {
"Number is $number"
$number++
}
This prints:
Number is 0
Number is 1
Number is 2
4. do Loop
A do loop always runs at least once, because the condition is checked after the loop body.
There are two forms:
do { } while (<condition>)do { } until (<condition>)
4.1 do … while
Runs the block, then repeats while the condition is true.
Example
$i = 1
do {
"Value: $i"
$i++
} while ($i -le 3)
Explanation:
- The loop prints values 1, 2, and 3.
- The condition is checked after each iteration.
4.2 do … until
Runs the block until the condition becomes true.
Example
$i = 1
do {
"Value: $i"
$i++
} until ($i -gt 3)
Explanation:
- The loop runs until
$ibecomes greater than 3. - It prints the same output as the previous example.
5. Choosing the Right Loop
Loop TypeUse WhenforYou know the exact number of iterationsforeachYou want to process each item in a collectionwhileYou want to repeat until a condition changesdo … while / untilYou need the loop to run at least once
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
If Switch
Next
Script Blocks and Scoping Rules
-
1.
forLoop- Example: counting from 1 to 5
- Example: summing numbers
-
2.
foreachLoop- Example: iterating through an array
- Example: processing files
-
3.
whileLoop- Example: count down from 5
- Example: waiting for a condition
-
4.
doLoop -
4.1
do … while- Example
-
4.2
do … until- Example
-
5. Choosing the Right Loop
