Sorting Multiple Properties

by Jun 17, 2010

Sort-Object can sort on multiple properties at the same time. Have a look:

Get-Service | Sort-Object Status, Name

This will list stopped services first, then running services. Within the two blocks, services are sorted by name. To reverse sort order, you can add -Descending. Then, however, both properties would be sorted in descending order. Here is how you can specify sort order individually:

$prop1 = @{Expression='Status'=$true }
$prop2 = @{Expression='Name'=$true }
Get-Service | Sort-Object $prop1, $prop2