Sort direction

By default Sort-Object uses an ascending sort direction.

Get-Command | Sort-Object -Property Source

will sort the commands based on the Source (module) in ascending Source order.

If you use multiple properties

Get-Command | Sort-Object -Property Source, Name, Version

Your output is sorted by Source, by Name within Source and by Version within Name.

All of these sorts are ascending sorts – A-Z or 1-9 etc

You can change the sort direction to Descending by using the Descending switch. If you have multiple properties involved in the sort they’ll be be sorted in a descending direction.

If you want some properties to be sorted ascending and some descending you need to explicitly specify the sort direction.

Get-Command | Sort-Object -Property @{Expression = ‘Source’; Ascending = $true}, @{Expression = ‘Name’; Ascending = $true}, @{Expression = ‘Version’; Descending = $true}

A hash table is used to specify the property (Expression) and the sort direction.

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s