How to Reverse an Array in PowerShell

Published by Nyau Wai Hoe - Updated on

PowerShell is like a Swiss army knife for automating tasks and managing configurations on Windows. It’s packed with tools to play around with data, including arrays. Flipping arrays backwards is something you might need to do now and then, whether it’s for sorting data, meeting the needs of a specific algorithm, or just showing information in reverse.

This guide will walk you through how to reverse arrays in PowerShell. It doesn’t matter if you’re just starting with PowerShell or you’ve been scripting for ages; you’ll find something useful here on flipping arrays back to front in this handy framework.

How to Reverse an Array in PowerShell

First up, what’s an array in PowerShell?

Before we jump into reversing arrays, let’s get the basics down. An array is a collection of items lined up in order, which can be anything really. In PowerShell, arrays are super flexible – you can mix up different types of items and the array can grow or shrink as needed. To create an array, just list the items separated by commas:

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

This makes an array of numbers. But you can mix it up with different types too:

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

PowerShell Arrays

Each thing in an array has a spot, starting from 0. You use this spot number to grab whatever’s there:

$array[0] # Gives you the first thing, which is 1

Now that we’ve covered array basics, let’s get into how to flip them over in PowerShell.

Also check out: How to Run CMD or PowerShell as SYSTEM or TrustedInstaller

Three ways to reverse an array in PowerShell

There are a few different paths you can take to reverse an array in PowerShell, each with its own perks and uses. Let’s look at three main ones.

1. Flip it with the array class’s reverse method

Since PowerShell is part of the .NET world, you can use .NET’s tricks directly in your scripts. The Array class has a bunch of helpful functions, including a Reverse method to flip an array around.

Here’s the deal:

When you use the Reverse method, it changes the original array right where it stands, turning it backwards.

For example:

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

PowerShell reverse array order

What you get:

5
4
3
2
1

Why it’s cool:

  • It’s memory-friendly because it flips the existing array without needing extra space for a new one. This is great for big arrays where saving memory matters.
  • It’s fast, especially for big arrays, because it’s a straight-up .NET function.

But keep in mind:

  • The original array changes. If you need the original order too, make a copy first.

When to use this:

  • When saving memory is key.
  • When you need things done fast, especially with big arrays.
  • When you don’t care about keeping the original order or you’ve made a copy.

Related reading: Using PowerShell to Test Port Connection Without Telnet

2. Use PowerShell’s magic with the pipeline and range operator

The pipeline and range operator in PowerShell let you flip an array without dipping into .NET directly. It’s a neat trick using PowerShell’s own toolbox.

How it does its magic:

By telling PowerShell to go from the last item to the first, you get a new array that’s the mirror image of the original.

Check this out:

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

PowerShell reverse array pipeline

Why it’s handy:

  • The original stays as is, which is great when you need both versions.
  • It feels more natural if you’re used to PowerShell’s way of doing things, using its built-in features.

But remember:

  • It might not be as quick as the .NET way for very big arrays.

Best for:

  • Keeping the original untouched.
  • Sticking with PowerShell’s own tools.
  • Dealing with not-too-huge arrays where speed isn’t everything.

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

3. Do it yourself with a for loop

A for loop lets you manually go through the array backwards and build a new one flipped the right way.

What’s the process?

You start with an empty array and fill it up in reverse order from the original.

Like this:

$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

Pros:

  • The approach is easy to understand, even if you’re new to PowerShell or coding.
  • The original array stays the same.

Cons:

  • It could be slower for really big arrays compared to built-in functions.

Great for:

  • When you like to see exactly how things are done, step by step.
  • If you’re not dealing with massive arrays and want to keep the original safe.

With these methods in your toolbox, you’ll be well-equipped to choose the right one for flipping arrays in PowerShell, depending on what you need in terms of memory use, speed, and whether you want to keep the original array intact.

More to explore: How to Check Office 365 User License Using PowerShell

Thinking about performance when reversing arrays in PowerShell

When you’re working with arrays, especially big ones, performance matters. Here’s a quick look at how each method stacks up and when you might want to use one over the others.

1. Going straight to .NET

Using .NET methods like [Array]::Reverse($array) is usually the fastest, especially for big arrays, because .NET is designed for efficiency.

Note: These methods are kind to your memory but be careful not to mess up your data.

2. Keeping it PowerShell-style

Flipping an array with the pipeline and range might be a bit slower for huge arrays, but it’s usually fine for smaller ones and it’s easier to read for PowerShell folks.

3. Thinking about memory

Direct .NET methods don’t need extra space since they work on the original array. But creating a new array with PowerShell’s features uses more memory, which could be a big deal with large arrays or limited memory.

A few tips for big arrays:

  • If you’re dealing with a huge array and memory is tight, lean towards the .NET methods.
  • Test your script on a small piece of your data first to avoid surprises on large datasets.

Wrapping up

PowerShell gives you several ways to reverse arrays, each with its own pros and cons, especially regarding performance and memory usage. By considering your data size and needs, you can pick the most suitable method for your task.


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