0
0
PowerShellscripting~5 mins

Range operator (..) in PowerShell

Choose your learning style9 modes available
Introduction
The range operator (..) helps you create a list of numbers quickly without typing each one.
When you want to count from 1 to 10 in a script.
To create a list of numbers for looping through tasks.
When you need to generate a sequence of numbers for calculations.
To quickly make a list of days or months in a script.
When you want to select a range of items by their number.
Syntax
PowerShell
$start..$end
The operator creates an array of numbers from start to end, including both.
If start is greater than end, it creates a descending list.
Examples
Creates numbers from 1 to 5: 1, 2, 3, 4, 5.
PowerShell
1..5
Creates numbers from 10 down to 7: 10, 9, 8, 7.
PowerShell
10..7
Creates numbers from 3 to 6 using variables.
PowerShell
$a = 3; $b = 6; $a..$b
Sample Program
This script creates numbers from 1 to 5 and prints each with a label.
PowerShell
$numbers = 1..5
foreach ($num in $numbers) {
    Write-Output "Number: $num"
}
OutputSuccess
Important Notes
The range operator always includes both the start and end numbers.
You can use the range operator inside loops to repeat actions easily.
If you use it with strings, it will not work as expected because it is for numbers.
Summary
The range operator (..) creates a list of numbers between two values.
It works both for counting up and counting down.
Use it to simplify creating number lists in your scripts.