PowerShell 1..10 foreach Example


If you want to iterate a range on numbers from 1..10 (one to ten) using PowerShell, you can do that with the help of the foreach loop.

Let's take a look with a help of a few examples.


Example 1: One-Liner Example

PS /> 1..10 | ForEach-Object { Write-Host "Number: $_" }

Number: 1
Number: 2
Number: 3
Number: 4
Number: 5
Number: 6
Number: 7
Number: 8
Number: 9
Number: 10

Example 2: Using a PowerShell Script

# foreach 1..10
foreach ($number in 1..10) {
    Write-Host "Number: $number"
}
Example 1 - 1..10 foreach in PowerShell

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap