PowerShell Array Range – How to Work

I will explain everything about the PowerShell array range in this PowerShell tutorial. We will see with examples.

What is an Array Range in PowerShell?

An array range is a subset of an array in PowerShell. It allows you to select a sequence of elements from the array. In PowerShell, you can define a range using the range operator ...

For example:

$myArray = 1..5 # Creates an array with numbers 1 to 5

This is equivalent to manually creating a PowerShell array like $myArray = 1, 2, 3, 4, 5.

Create Array Ranges in PowerShell

You can create an array range by specifying the starting and ending numbers in PowerShell. Here’s an example:

$range = 10..15
$range

This command creates an array containing the numbers 10 through 15 in PowerShell.

You can see the output in the screenshot below:

PowerShell Array Range

Access a Range of Elements in PowerShell Array

To access a range of elements within an array, you can also use the range operator. For instance, if you want to access the elements from index 1 to index 3 in $myArray, you would use the following syntax:

$subArray = $myArray[1..3] # Returns 2, 3, 4

Using Array Ranges in Loops in PowerShell

Array ranges are particularly useful when used in loops. For example, you can use a range to iterate over a sequence of numbers:

for ($i = 0; $i -le 5; $i++) {
    Write-Host $i
}

This loop will print numbers 0 through 5 to the console.

Reversing a Range in PowerShell Array

You can also create a reversed range by specifying a higher number first and then a lower number:

$reversedRange = 5..1 # Creates an array with numbers 5 to 1

Combine Ranges with Other Arrays in PowerShell

You can combine ranges with other elements to create larger arrays. For example:

$combinedArray = (1..3) + 4 + (5..7) # Creates an array with numbers 1 to 7

Modify PowerShell Array Elements Using Ranges

You can use ranges to modify multiple elements in an array at once. For instance:

$myArray[1..3] = 'a', 'b', 'c'

This command will replace the second, third, and fourth elements of $myArray with ‘a’, ‘b’, and ‘c’ respectively.

Using Array Ranges with Commands

Array ranges can also be used to pass a sequence of numbers to a command. For example, you can use a range to create multiple directories:

1..3 | ForEach-Object { New-Item -ItemType Directory -Name "Folder$_" }

This command will create three directories named Folder1, Folder2, and Folder3.

Advanced PowerShell Array Range Operations

In PowerShell, you can perform more advanced operations with ranges, such as creating sequences with specific patterns. For example, to create a sequence of even numbers, you can use a loop with a range:

$evenNumbers = for ($i = 2; $i -le 10; $i += 2) { $i }

This loop will populate $evenNumbers with the even numbers from 2 to 10.

Conclusion

Array ranges in PowerShell are a simple yet powerful feature that can help you create and manipulate collections of data efficiently. By understanding how to create and use ranges, you can streamline your scripts and make them more readable and maintainable.

In this PowerShell tutorial, I have explained everything about PowerShell array ranges.

You may also like: