« Weekly Post #7 | Main | Twitter? »
PowerShell Import-CSV and Sort-Object
By Adam Bell | July 20, 2009
Recently I noticed that when I was sorting data imported from a CSV file, the behaviour wasn’t quite as expected.
$scores = Import-Csv .\scores.csv $scores | Sort-Object -Property Score -Descending
I’m pretty sure Julie’s score of 300 should be higher then Homer’s!
$scores | Get-Member
A quick look shows that even though the score is a number it’s being treated as a system.string. To resovle this we need to change it back to a number. This is done by wrapping it into a code block and recasting as an integar:
$scores | Sort-Object -Property {[int] $_.Score} -Descending
Fixed! More information can be found in this Hey,Scripting Guy Article.
- Managing group membership in Active Directory with PowerShell (Part 1)
- Control Access Rights in Active Directory
- Retrieving Active Directory FSMO roles with PowerShell
- Searching Active Directory with PowerShell
- /n Software NetCmdlets
Topics: PowerShell | No Comments »