PowerTip: Sort an Array of Numbers with PowerShell

Summary: Learn how to sort an array of numbers with Windows PowerShell.

Hey, Scripting Guy! Question How can I use Windows PowerShell to easily sort an array of numbers?

Hey, Scripting Guy! Answer Pipe the array to the Sort-Object (sort is an alias) cmdlet, for example:

$a = 1,2,7,6,4,3,3,2,9

$a | sort

Comments (2)

  1. Drew Robinson says:

    Thanks for creating these tips. If I may make a suggestion, include examples for other types.. using strings, new powershell users won’t know to create ‘ or " when using strings.. ‘a’,’z’, ‘b’

  2. Mialto says:

    Good to note that this sort does not re-index the array.. if you want to re-index, you’d have to use the static method [array]::sort($a).

    Nonetheless, great tip as always!

Skip to main content