Use a PowerShell for loop when a counter, index, or deliberate step pattern controls how many times commands run. Its header initializes a value, tests a condition before each pass, and updates the value between passes:
for ($i = 1; $i -le 10; $i++) {
Write-Host $i
}
This prints the numbers 1 through 10. The condition is checked before the body runs, so the loop stops once it becomes false. Microsoft documents the syntax in its PowerShell for statement guide.
How a PowerShell for loop works
The general form is:
for (<Init>; <Condition>; <Repeat>) {
<Statement list>
}
Microsoft Learn describes for as a language construct that runs commands in a command block while a specified condition evaluates to $true. The header has three parts, separated by semicolons:
- Init: Runs once before the loop begins. It commonly assigns an initial value to a counter.
- Condition: Evaluated before every pass through the body. If it is true, the body runs; if false, the loop ends.
- Repeat: Runs after the body on each pass, usually changing the counter so the condition will eventually become false.
The commands between the braces are the loop body. They run once for each iteration allowed by the condition.
#1 Best Overall
Write a basic counter loop
This loop starts at 1 and counts through 10:
for ($i = 1; $i -le 10; $i++) {
Write-Host $i
}
$i = 1initializes the counter.$i -le 10checks whether the counter is less than or equal to 10 before each pass.Write-Host $iruns for the current counter value.$i++increments the counter after the body.
After the body runs with $i equal to 10, the repeat expression changes it to 11. The next condition check fails, so the loop exits without printing 11.
Change the step or range
The repeat expression does not have to increment by one. For example, this loop visits even numbers from zero through twenty:
Rank #2
for ($i = 0; $i -le 20; $i += 2) {
Write-Host $i
}
Adjust the starting value, condition, and repeat expression together to define the values you want. For a descending count, use a condition that permits the starting value and a repeat expression that decreases the counter, such as $i--.
Make sure the loop can stop
A counter loop depends on its condition becoming false. If the repeat expression does not move the counter toward that outcome, the loop can continue indefinitely. Check that:
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Rank #3
- The initial value satisfies the condition when you expect the body to run.
- The repeat expression changes the relevant value in the intended direction.
- The condition eventually excludes the updated value.
A for header can omit one or more of its sections. In particular, for (;;) { ... } has no condition to turn false, so it repeats until something in the body stops it or you interrupt it with Ctrl+C. Use that form only when an intentionally unbounded loop is appropriate.
Use multiple header operations or line breaks
The initializer, condition, and repeat sections can contain multiple operations, and the condition can combine tests with logical operators. Microsoft notes that operations other than pre- or post-increment may not work with every multiple-operation syntax, so keep such headers simple and verify the form against the official for guide when using less common combinations.
For readability, the header sections can also be separated by line breaks instead of semicolons:
for ($i = 0
$i -lt 10
$i++) {
$i
}
For commands entered interactively, the single-line semicolon form is generally more convenient.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Best Value
Choose between for, foreach, and while
| Statement | Best fit | Control pattern |
|---|---|---|
for |
A numeric counter, index range, subset of an array, or deliberate step pattern | Initialization, condition, and repeat expression appear together |
foreach |
Processing every value in a collection | Moves through collection members |
while |
Repeating as long as a condition remains true when a counter-style header adds little clarity | Condition is the main control; initialization or updates can be handled elsewhere |
Microsoft recommends considering foreach when the goal is to process all values in an array. Choose for when the index or step matters, and while when the condition itself is the clearest description of when to continue. Microsoft’s while statement guide documents its condition-first syntax.
Documentation scope
The linked for guide is Microsoft Learn’s PowerShell 7.5 documentation view; the PowerShell language specification is available in the PowerShell 7.6 view. These sources document PowerShell syntax; they do not establish that the examples were independently executed in Windows PowerShell 5.1.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




