PowerTip: Reverse Array with PowerShell

Summary: Use Windows PowerShell to reverse an array.

Hey, Scripting Guy! Question How can I use Windows PowerShell to sort an array that is going in the wrong direction?

Hey, Scripting Guy! Answer Call the Reverse static method from the [array] class, for example:

PS C:\> $a = 1,2,3,4,5

PS C:\> [array]::Reverse($a)

Comments (4)

  1. Boe Prox says:


    Try this:

    $Line = "this is a sentence" -split ‘ ‘
    $line[$line.count..0] -join ‘ ‘

  2. Eben says:

    I’ve combed the interwebs, but how would you go about changing word order? Like "this is a sentence" and reversing it to "sentence a is this"

  3. JohnLBevan says:

    NB: One gotcha to watch out for; the variable is passed by reference; the function doesn’t return any output. This means
    $a = [array]::Reverse($a)
    will set $a to $null.

    The correct usage is:

  4. Enrique Garcia says:


    I don’t know if you already find the solution, but testing (because I am looking for the same), saw that the [array]::Reverse($var) indeed affect your $var order. So, you can review the order in the $var
    PS C:\> $a = 1,2,3,4,5
    PS C:\> [array]::Reverse($a)
    PS C:\> $a