Reputation: 61
In powershell I have an array that I've imported from CSV with the following information
UserName RecordDate @{UserName=user1; RecordDate=05/18/2013} @{UserName=user1; RecordDate=05/18/2012} @{UserName=user1; RecordDate=04/18/2013} @{UserName=user1; RecordDate=01/18/2013} @{UserName=user22; RecordDate=05/18/2013} @{UserName=user22; RecordDate=05/18/2012} @{UserName=user22; RecordDate=04/18/2013} @{UserName=user22; RecordDate=01/18/2013}
I want to remove the duplicated entries based on date and leave only the entry with the latest date for each user in username
Thank you!!
Upvotes: 0
Views: 630
Reputation: 200293
Try this:
Import-Csv "C:\path\to\your.csv" `
| select UserName, @{
n='RecordDate';
e={[DateTime]::ParseExact($_.RecordDate, 'MM\/dd\/yyyy', $null)}
} `
| group UserName `
| % { $_.Group | sort RecordDate -Desc | select -First 1 }
Upvotes: 3
Reputation: 29450
You just need to convert your property to a DateTime
so that it's sorted properly. Just change the sort to look like this:
sort {[DateTime]$_.RecordDate}
Upvotes: 1