Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have Select-Object -Unique work on one property but display multiple?

Tags:

powershell

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:

enter image description here

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.

like image 953
colonel_claypoo Avatar asked Jan 01 '26 00:01

colonel_claypoo


1 Answers

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
}
like image 97
AdamL Avatar answered Jan 04 '26 13:01

AdamL



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!