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: ,

Read the complete post at http://richardsiddaway.spaces.live.com/Blog/cns!43CFA46A74CF3E96!2243.entry

Published Wed, Apr 22 2009 7:26 by Richard Siddaway's Blog
Filed under: