I will explain a fundamental topic today, PowerShell for loop 1 to 10. The simple “loop from 1 to 10” is the perfect starting point because it teaches you the fundamental pattern you’ll use to automate countless repetitive tasks. Once you understand this basic structure, you’ll be able to adapt it to loop through anything: files, users, database records, you name it.
What Is a For Loop in PowerShell, Anyway?
A for loop is a control structure that repeats a block of code a specific number of times. Think of it like saying: “Do this thing, starting at 1, and keep doing it until you reach 10.”
The beauty of the for loop is that it gives you precise control—you decide where to start, where to stop, and how to count (by 1s, 2s, backwards, whatever you need).
Method 1: The Classic For Loop
This is the most common and versatile way to loop from 1 to 10 in PowerShell.
The Basic Syntax
Below is the basic syntax.
for ($i = 1; $i -le 10; $i++) {
Write-Host "Current number: $i"
}How It Works (Breaking It Down)
Let me walk you through each part inside those parentheses:
$i = 1— This is the initializer. We’re creating a variable called$iand setting it to 1. This is where your loop starts.$i -le 10— This is the condition. The loop keeps running as long as this is true.-lemeans “less than or equal to,” so the loop continues while$iis 10 or less.$i++— This is the increment. After each loop iteration, we increase$iby 1. ($i++is shorthand for$i = $i + 1.)- The code block
{ }— Everything inside the curly braces runs each time through the loop.
What You’ll See
Current number: 1
Current number: 2
Current number: 3
...
Current number: 10Here is the exact output in the screenshot below:

Pro Tip: You can use any variable name instead of $i, but $i is a convention (short for “index” or “iterator”). Using standard conventions makes your code easier for others (and future you!) to read.
Check out How to Break Out of ForEach Loops in PowerShell?
Method 2: Using a Range Operator with ForEach
PowerShell has a neat shortcut using the range operator (..) combined with ForEach-Object or the foreach statement.
Using the Range Operator with Foreach Statement
foreach ($number in 1..10) {
Write-Host "Current number: $number"
}This reads almost like English: “For each number in the range 1 to 10, do this.” It’s simpler and cleaner when you just need to iterate through a sequence.
Here is the exact output in the screenshot below:

Using the Range Operator with ForEach-Object Cmdlet
1..10 | ForEach-Object {
Write-Host "Current number: $_"
}Here, we’re using the pipeline (|), which is very PowerShell-idiomatic. The range 1..10 generates numbers 1 through 10, and each one gets passed to ForEach-Object. Inside the block, $_ represents the current item in the pipeline.
When to Use Which?
- Use the classic for loop when you need precise control (like skipping numbers, counting backwards, or complex increment logic).
- Use the range operator when you just want a simple sequence—it’s cleaner and easier to read.
Check out PowerShell For Loop With Index and Range Examples
Method 3: Counting by Different Steps
What if you want to count by 2s? Or go backwards? The classic for loop handles this easily.
Count by 2s (Even Numbers Only)
for ($i = 2; $i -le 10; $i += 2) {
Write-Host "Even number: $i"
}Output:
Even number: 2
Even number: 4
Even number: 6
Even number: 8
Even number: 10Count Backwards from 10 to 1
for ($i = 10; $i -ge 1; $i--) {
Write-Host "Countdown: $i"
}Notice we changed:
- The condition to
-ge(greater than or equal to) - The increment to
$i--(decrease by 1)
Pro Tip: You can increment by any amount using $i += 5, $i -= 3, etc. This flexibility makes for loops incredibly powerful.
Read Concatenate Strings Inside Loops in PowerShell
Real-World Examples
Now, let me show you some real examples of PowerShell For Loop 1 to 10.
Example 1: Create 10 Test Files
Here is an example of using the PowerShell for loop 1 to 10 to create 10 test files.
for ($i = 1; $i -le 10; $i++) {
New-Item -Path "C:\TestFiles\file$i.txt" -ItemType File
Write-Host "Created file$i.txt"
}This creates file1.txt, file2.txt, … file10.txt in the specified folder. Perfect for testing scripts that process multiple files!
Example 2: Check Multiple Server Names
for ($i = 1; $i -le 10; $i++) {
$serverName = "SERVER$i"
if (Test-Connection -ComputerName $serverName -Count 1 -Quiet) {
Write-Host "$serverName is online" -ForegroundColor Green
} else {
Write-Host "$serverName is offline" -ForegroundColor Red
}
}This pings servers named SERVER1 through SERVER10 and reports their status.
Example 3: Generate Test Data
$users = @()
for ($i = 1; $i -le 10; $i++) {
$users += [PSCustomObject]@{
UserID = $i
UserName = "User$i"
Email = "user$i@example.com"
}
}
$users | Format-TableCreates an array of 10 user objects—great for testing database imports or report generation.
Check out PowerShell ForEach [Looping Through Collections]
Best Practices
Here are some best practices that you should follow while working with PowerShell for loop 1 to 10.
Pitfall: Off-by-One Errors
One of the most common mistakes is getting your start/end points wrong.
# This only runs 9 times!
for ($i = 1; $i -lt 10; $i++) { ... }-lt means “less than,” so it stops at 9. Use -le (less than or equal) if you want to include 10.
Best Practice: Initialize Variables Outside the Loop
If you’re building a collection (like an array), initialize it before the loop:
$results = @()
for ($i = 1; $i -le 10; $i++) {
$results += "Item $i"
}Best Practice: Use Meaningful Variable Names for Complex Loops
While $i is fine for simple counting, in more complex scripts, use descriptive names:
for ($serverNumber = 1; $serverNumber -le 10; $serverNumber++) {
# Much clearer what you're looping through
}Pro Tip: Add Progress Indicators for Long-Running Loops
for ($i = 1; $i -le 100; $i++) {
Write-Progress -Activity "Processing" -Status "Item $i of 100" -PercentComplete $i
# Your actual work here
Start-Sleep -Milliseconds 100
}This shows a nice progress bar—very helpful when processing hundreds of items!
Check out How to Use Multiple Conditions in Do-While Loop in PowerShell?
Advance Examples
Nested Loops
You can put loops inside loops (though be careful—they multiply quickly!):
for ($i = 1; $i -le 3; $i++) {
for ($j = 1; $j -le 3; $j++) {
Write-Host "i=$i, j=$j"
}
}Breaking Out Early
Use break to exit a loop before it finishes:
for ($i = 1; $i -le 10; $i++) {
if ($i -eq 5) {
Write-Host "Found 5! Stopping."
break
}
Write-Host $i
}Skipping Iterations
Use continue to skip to the next iteration:
for ($i = 1; $i -le 10; $i++) {
if ($i -eq 5) {
continue # Skip 5
}
Write-Host $i
}Conclusion
You now understand how to use PowerShell for loops to count from 1 to 10—and so much more. We covered the classic for loop syntax, the cleaner range operator approach, how to count by different steps, and several real-world examples. You also learned important pitfalls to avoid and best practices that you should follow.
Have questions or want to share what you’ve built with loops? Drop a comment below.
You may also like the following tutorials:
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.