How to Reverse an Array in PowerShell

Published by Nyau Wai Hoe - Updated on

In PowerShell, you can work with arrays. Sometimes, you will want to reverse an array to sort some data, meet the needs of an algorithm or a script, or just to show the data in a different order.

In this tutorial, we’ll show you different ways to reverse arrays in PowerShell, the benefits of each of these methods, and when to use them.

How to Reverse an Array in PowerShell

What’s an array in PowerShell?

An array is simply a collection of items arranged in a specific order. Each item can be of any type. Arrays in PowerShell are very flexible. They can handle different types of data and can be resized on the go. To make an array, just use a comma , to separate items:

$array = 1,2,3,4,5

This line of code makes an array of numbers. But you can mix things up with different types of items too:

$mixedArray = 1, "hello", $true, 2.5

PowerShell Arrays

Each item in an array has an index, starting from 0. You use this index number to pick out the items:

$array[0] # Returns the first item, which is 1

Also see: How to Run CMD or PowerShell as SYSTEM or TrustedInstaller

3 methods to reverse an array in PowerShell

In PowerShell, you can reverse an array in generally 3 different ways.

Use the array class’s reverse method

PowerShell works with the .NET ecosystem, which means you can use built-in .NET classes in your scripts. The Array class is one of these, and it has a bunch of functions to work with arrays. The Reverse method from this class lets you reverse an array right in place.

How it works:

When you use the Reverse method from the Array class, it changes the array right where it is. This means that after you call the method, the array items are flipped around.

Example:

$array = 1,2,3,4,5
[Array]::Reverse($array)
$array

PowerShell reverse array order

Output:

5
4
3
2
1

What’s good about it:

  • As the method works on the original array, no extra memory is needed for a new one. This is great for really big arrays where saving memory matters.
  • Since this is a direct .NET function, it usually works faster, especially with big arrays, compared to other ways.

What’s the downside:

  • The main thing to think about with this method is that it changes the original array. If you need to keep the array the way it was, you should make a copy before reversing it.

When to use:

  • When saving memory is important.
  • When you need fast results, especially with big arrays.
  • When it’s okay to change the order of the original array, or if you’ve already made a copy.

Related resource: Using PowerShell to Test Port Connection Without Telnet

Use PowerShell’s pipeline and range operator to reverse an array

PowerShell’s strong pipeline and its range operator can work together to reverse an array without directly using .NET methods. The pipeline lets data flow from one command or function to another, and the indexing system has clever ways to access array items.

How it works:

The range [-1..-($array.Length)] basically grabs the items from the end to the start. The pipeline then creates a new flipped array.

Example:

$array = 1,2,3,4,5
$reversedArray = $array[-1..-($array.Length)]
$reversedArray

PowerShell reverse array pipeline

What’s good about it:

  • This method doesn’t change the initial array, so you can keep both the original and the reversed versions for later use.
  • For those who know their way around PowerShell, this method might seem more natural and taps into PowerShell’s own features.

    What’s the downside:

    • For small arrays, you might not notice much difference, but for larger ones, this method could be a bit slower than using direct .NET methods.

    When to use:

    • When you want to keep the original array the way it was.
    • If you like using PowerShell’s built-in features instead of .NET methods.
    • When the array is not too big and top performance isn’t the biggest worry.

    Useful tip: PowerShell Script to Copy All OneDrive Files to Another User

    Use a for loop to reverse an array

    A for loop is a simple but effective way to reverse an array in PowerShell. By going through the array from the last item to the first, we can put together a new flipped array.

    How it works:

    We start with an empty array and add items from the original array in reverse order using a for loop.

    Example:

    $array = 1,2,3,4,5
    $reversedArray = @()
    for ($i = $array.Length - 1; $i -ge 0; $i--) {
    $reversedArray += $array[$i]
    }
    $reversedArray

    Reversing an array in PowerShell using For loop

    What’s good about it:

    • The process is easy to follow, making it great for beginners or anyone new to PowerShell or coding.
    • The original array stays the same.

      What’s the downside:

      • This method might be slower with very big arrays compared to some built-in functions.

      When to use:

      • If you like to see each step of the process.
      • When working with not-too-large arrays and wanting to keep the original as is.

      Relevant guide: How to Check Office 365 User License Using PowerShell

      Things to consider when reversing arrays in PowerShell

      When working with big chunks of data, performance always matters and should be taken into account. Let’s go over how each method to reverse arrays stacks up and when you might prefer one over another.

      Direct .NET methods

      Using .NET methods directly, like [Array]::Reverse($array), can be the most efficient way, especially for very big arrays. These methods are built and optimized for such tasks. If quick results are what you need and your array is large, this is probably your best choice.

      Note: Although these methods save memory by working on the array you already have, remember that they will change your original data.

      PowerShell-native ways

      Using the range operator and the pipeline, like with $reversedArray = $array[-1..-($array.Length)], may be a bit slower for big arrays than the direct .NET methods. But for smaller arrays, you might not notice much of a difference. This method is good for its readability, especially if the script will be used by other PowerShell users who might not be as familiar with .NET methods.

      When memory is important

      Although the direct .NET methods reverse arrays right where they are and don’t need extra memory, the PowerShell-native method makes a new array. This means you might use twice as much memory for a while. If you’re tight on memory or dealing with a huge array, this could help you decide which method to go with.

      One last thing

      Always test your script on a smaller set of data before using it on a big dataset. This can help you spot any potential issues with performance or memory to avoid messing things up.


      Nyau Wai Hoe
      Nyau Wai Hoe is the Founder and Chief Editor of WindowsDigitals.com. With a degree in software engineering and over 12 years of experience in the tech support industry, Nyau has established himself as an expert in the field, with a primary focus on the Microsoft Windows operating system. As a tech enthusiast, he loves exploring new technologies and leveraging them to solve real-life problems.

      Share via
      Copy link