PowerTip: Reverse Array with PowerShell

PowerTip: Reverse Array with PowerShell

  • Comments 2
  • Likes

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)

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • 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"

  • @Eben

    Try this:

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