header image


Posted by: | April 22, 2009 | No Comment |

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.

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

under: PowerShell original