Reputation: 3
I have a PS Object with 3 properties: Status ID StartTime
The 'ID' property containts a lot of duplicates. For each unique ID, I only want to select the one with the most recent StartTime. And also important: I want to keep all the properties.
I already did the following:
$UniqueList = $CompleteList | Sort-Object -Property ID -Unique
This removes the duplicate ID's, but I have no control over which one is kept. I think I have to do something with sorting StartTime and then select -First 1 or something, but I can't get it right in my code.
Upvotes: 0
Views: 2079
Reputation: 361
$UniqueList = $completelist | Group-Object -Property ID | ForEach-Object{$_.Group | Sort-Object -Property StartTime -Descending | Select-Object -First 1}
Do a Group-Object by ID first, sort them by StartTime in each group, then select -First 1 in each group gives the result.
Upvotes: 2
Reputation: 754
Why not just add a sort on the time property first?
$UniqueList = $CompleteList | Sort-Object -Property starttime -Descending | sort-Object -Property ID -Unique
Does that have the expected results with your objects?
Upvotes: 1