PowerShell Sort-Object puts objects in order by one or more property values. Pipe the objects in, name the property, and add -Descending if you want the largest values first:
$staff | Sort-Object -Property Salary -Descending
In this tutorial, I’ll show you how to use PowerShell Sort-Object with properties, numbers stored as text, dates, -Unique, -Top and hash tables. Every example below comes with its real output, plus the spots where PowerShell 7 and Windows PowerShell 5.1 disagree.
How Sort-Object works: the default sort order
With no parameters, Sort-Object sorts in ascending order, and string comparisons ignore case. Here’s a short list of city names with mixed capitalization:
'Seattle', 'austin', 'Denver', 'Boston' | Sort-Object
Output:
austin
Boston
Denver
Seattle
The lowercase austin still lands first, because the default sort treats A and a as the same letter. Sort-Object sorts what comes down the pipeline, so it works on anything from a simple array of strings to the objects a cmdlet returns.
When you don’t name a property, PowerShell uses the default sort property of the object type. File objects from Get-ChildItem sort by Name, for example. On Windows you can also type the short alias sort.
Sort objects by a property
Most of the time you sort objects, not plain strings. The next examples use this small staff list. Each entry is a custom object with four properties:
$staff = @(
[pscustomobject]@{ Name = 'Maria Lopez'; Department = 'Sales'; City = 'Austin'; Salary = 88000 }
[pscustomobject]@{ Name = 'Tom Baker'; Department = 'IT'; City = 'Denver'; Salary = 95000 }
[pscustomobject]@{ Name = 'Dana Reyes'; Department = 'Sales'; City = 'Seattle'; Salary = 81000 }
[pscustomobject]@{ Name = 'Kevin Hart'; Department = 'IT'; City = 'Boston'; Salary = 120000 }
[pscustomobject]@{ Name = 'Megan Bowen'; Department = 'HR'; City = 'Miami'; Salary = 68000 }
)
Pass a property name to -Property to sort by it. The parameter is positional, so Sort-Object Salary works too:
$staff | Sort-Object -Property Salary |
Select-Object Name, City, Salary
Output:
Name City Salary
---- ---- ------
Megan Bowen Miami 68000
Dana Reyes Seattle 81000
Maria Lopez Austin 88000
Tom Baker Denver 95000
Kevin Hart Boston 120000
Numbers sort as numbers here because Salary holds integers. I added Select-Object to pick the columns so the output stays narrow.
Sort in descending order with -Descending
Add -Descending to reverse the order, so the highest salary comes first:
$staff | Sort-Object -Property Salary -Descending |
Select-Object Name, Salary
Output:
Name Salary
---- ------
Kevin Hart 120000
Tom Baker 95000
Maria Lopez 88000
Dana Reyes 81000
Megan Bowen 68000
-Descending applies to every property in the command. To mix directions, use the hash table syntax two sections down.
The same switch ranks live data too. For example, Get-Process sorted by memory shows the busiest processes first.
How do I sort by multiple properties in PowerShell?
Separate the property names with commas. Sort-Object sorts by the first property, then breaks ties with the second one:
$staff | Sort-Object Department, Name |
Select-Object Department, Name
Output:
Department Name
---------- ----
HR Megan Bowen
IT Kevin Hart
IT Tom Baker
Sales Dana Reyes
Sales Maria Lopez
Both IT people sh