Categories

Sorting

Sorting is a fairly common activity in PowerShell.  One scenario I don’t see very often is a requirement to sort in two different directions.  I have a list of users and their last logon dates – I want to sort users into alphabetical order (ascending) and lat logon into descending order.

001
Import-Csv logons.txt | sort @{Expression="Name";Descending=$false},@{Expression="LastLogon";Descending=$true}

We have to use a hash table for the fields and the direction if we want different sorting directions.  Looks a bit messy but works a treat.

Technorati Tags: ,

Leave a Reply