Do you need to know about PowerShell sort arrays? In this PowerShell tutorial, I will explain how to sort an array in PowerShell using various methods and real examples.
To sort an array in PowerShell, use the Sort-Object cmdlet. For ascending order, simply pipe the array into the cmdlet, like $myArray | Sort-Object. To sort in descending order, append the -Descending parameter: $myArray | Sort-Object -Descending.
Sort an Array in PowerShell
An array is a data structure that holds a collection of items. These items can be of any data type, and in PowerShell, arrays can even hold items of different types. Arrays are incredibly useful for storing and manipulating sets of data.
Let us explore different methods to sort a PowerShell array.
Sorting a PowerShell Array with Sort-Object
The primary cmdlet for sorting in PowerShell is Sort-Object. This cmdlet sorts objects in ascending or descending order based on object property values. If you don’t specify a property, PowerShell will sort based on the default property for the object type.
Basic Sorting
Here’s a simple example of sorting an array of numbers in PowerShell using sort-object:
$array = 3, 1, 4, 1, 5, 9, 2
$sortedArray = $array | Sort-Object
$sortedArrayThis will output the numbers in ascending order:
1
1
2
3
4
5
9Here is another example of how to sort a string array in PowerShell.
# Define a string array
$stringArray = 'orange', 'apple', 'banana'
# Sort the array in ascending order using Sort-Object
$sortedArray = $stringArray | Sort-Object
# Output the sorted array
$sortedArrayThis script will output the strings ‘apple’, ‘banana’, and ‘orange’ in ascending alphabetical order. You can see the output in the screenshot below after I executed the PowerShell script using VS code:

If you wanted to sort in descending order, you would modify the Sort-Object line to include the -Descending parameter:
# Sort the array in descending order
$sortedArray = $stringArray | Sort-Object -DescendingYou can see the output in the screenshot below; I have used VS code; you can use any editor to execute the PowerShell script.

Sorting in Descending Order
To sort a PowerShell array in descending order, use the -Descending switch:
$sortedArray = $array | Sort-Object -Descending
$sortedArraySort String Array in PowerShell
You can also sort a string array in PowerShell. By default, the sort is case-insensitive:
$stringArray = 'Banana', 'apple', 'Cherry'
$sortedStringArray = $stringArray | Sort-Object
$sortedStringArrayThis sorts the strings in alphabetical order:
apple
Banana
CherrySort an Array in PowerShell using Custom Expression
Let us see how to sort an array in PowerShell using a custom expression. You can sort based on expressions, such as parts of a string or mathematical calculations:
$array = 'a1', 'a10', 'a2', 'a3'
$sortedArray = $array | Sort-Object { [int]($_.Substring(1)) }
$sortedArrayThis will sort the array based on the numerical part of each string:
a1
a2
a3
a10Sort a PowerShell Array with Multiple Properties
You can also sort a PowerShell array by multiple properties. This is useful when you have complex objects, and you want to sort by one property, then another:
$people = @(
@{ Name = 'John'; Age = 30 },
@{ Name = 'Jane'; Age = 25 },
@{ Name = 'John'; Age = 22 }
)
$sortedPeople = $people | Sort-Object -Property Name, Age
$sortedPeopleThe above PowerShell script will sort people by name and then by age within each name.
Sort Array with Hash Tables in PowerShell
Let me show you how to sort an array with hash tables in PowerShell.You can use hash tables to specify different sorts for different properties:
$sortedPeople = $people | Sort-Object -Property @{Expression="Name";Descending=$false}, @{Expression="Age";Descending=$true}
$sortedPeopleThis sorts by name in ascending order and then by age in descending order.
Conclusion
Sorting arrays in PowerShell is a very common requirement, and we can achieve it using various methods. The Sort-Object cmdlet is the go-to for most sorting tasks, with options for sorting in ascending or descending order, case sensitivity, and unique value sorting.
In this PowerShell tutorial, I have explained how to work with PowerShell array sorting, especially different methods to sort a PowerShell array.
Yu may also like:
- PowerShell If Array Length Greater Than Example
- Create Empty Array Of Objects In PowerShell
- How to Clear an Array in PowerShell?
- How To Create Empty Array And Add Items In PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.