I have an array of objects with display names and email addresses. I'd like to filter it so that only unique display names are shown. The same display name can have different email addresses like so:

However, when I do:
$objectArray | Select-Object DisplayName, LoginName, Url -Unique
I will not get a single DisplayName but instead still all four objects.
How can I achieve it so that only one object with is shown?
Thanks for your help.
You could first get unique list of DisplayNames and in a loop select 1st object for each DisplayName. Instead of simple sort below you could apply different logic, eg get fields that are not empty:
$UniqueArray = $objectArray | Select-Object DisplayName -Unique | ForEach-Object {
$DisplayName = $_.DisplayName
$objectArray | Where-Object DisplayName -eq $DisplayName | Sort-Object LoginName | Select-Object -First 1
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With