How to Reverse An Array in PowerShell?

Recently, I wanted to reverse elements of an array in PowerShell. I tried three methods to achieve this. In this PowerShell tutorial, I will explain how to reverse an array in PowerShell.

To reverse an array in PowerShell, use the array method .Reverse(). For example, $array.Reverse() will reverse the elements in place if $array is an ArrayList. For a regular array, you can use [array]::Reverse($array) and then the array will be reversed in place. If you need a new array with the reversed order, use $reversedArray = $array[:: -1].

Reverse An Array in PowerShell

An array is a data structure that holds multiple items that are usually of the same type. Each item in an array is called an element, and each element is accessible by its index, which represents its position in the array.

Here’s how you can create an array in PowerShell:

# Creating an array
$myArray = 1, 2, 3, 4, 5

Now, let us see how to reverse the above array in PowerShell using various methods.

Method 1: Using the [Array]::Reverse() Method

The simplest way to reverse an array in PowerShell is by using the static Reverse method from the [array] class. This straightforward method performs an in-place reversal, directly modifying the original array.

Here’s an example of how to use [array]::Reverse():

# Creating an array
$a = 1, 2, 3, 4, 5

# Reversing the array in-place
[array]::Reverse($a)

# Output the reversed array
$a

This script will output the reversed array, you can see the screenshot below:

5
4
3
2
1
Reverse An Array in PowerShell

Method 2: Using Array Slicing

Another way to reverse an array in PowerShell is by using array slicing. This method creates a new array that is the reversed version of the original one, leaving the original array unchanged.

Here’s an example of reversing an array with slicing:

# Creating an array
$a = 1, 2, 3, 4, 5

# Reversing the array using slicing
$b = $a[-1..-($a.Length)]

# Output the reversed array
$b

This script will also output:

5
4
3
2
1

Method 3: Using the ForEach-Object Cmdlet with a Pipeline

You can also reverse an array in PowerShell by piping it into the ForEach-Object cmdlet, although this is less common. This method can output the elements in reverse order without reversing the memory array.

# Creating an array
$a = 1, 2, 3, 4, 5

# Reversing the array using ForEach-Object
$a | ForEach-Object -Begin { $stack = [System.Collections.Stack]::new() } -Process { $stack.Push($_) } -End { while ($stack.Count -gt 0) { $stack.Pop() } }

This script uses a stack to reverse the elements and outputs:

5
4
3
2
1

Conclusion

When choosing a method to reverse an array in PowerShell, consider whether you need to modify the original array or if you require a new array with the reversed order. The [array]::Reverse() method modifies the array in place, which is efficient but changes the original data. Array slicing and using ForEach-Object with a pipeline create a new array or output the elements in reverse order, leaving the original array intact.

In this PowerShell tutorial, I will explain how to reverse an array in PowerShell using various methods with examples.

You may also like: