DriversRecommendedOutdated drivers can make a good PC feel brokenScan driver issues before chasing fixes manually.Scan NowOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsClean PCRecommendedOne scan can reveal what keeps slowing WindowsLook for cleanup and repair opportunities.Run Scan×
Skip to content
Redmond desk3 min

How to Use a `for` Loop in Windows PowerShell

A PowerShell for loop combines initialization, a condition, and a repeat expression to control a counter, index, or step pattern.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Write a basic counter loop

This loop starts at 1 and counts through 10:

for ($i = 1; $i -le 10; $i++) {
    Write-Host $i
}
  1. $i = 1 initializes the counter.
  2. $i -le 10 checks whether the counter is less than or equal to 10 before each pass.
  3. Write-Host $i runs for the current counter value.
  4. $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:

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
PC Slower Than It Used to Be?Free scan - under a minute
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.