PowerShell 7.3 - How to Filter Out Non-Unique Objects from an Array Based on a Property?
I've looked through the documentation and I'm still confused about I keep running into Iām working with an array of custom objects in PowerShell 7.3, and I need to filter out duplicates based on a specific property. I have an array of objects where each object represents a user with properties like `Name`, `Email`, and `Role`. The goal is to return only unique users based on the `Email` property. I tried using the `Select-Object` cmdlet with the `-Unique` parameter, but I'm not getting the expected results. Here's what I have: ```powershell # Sample array of user objects $users = @( [PSCustomObject]@{ Name = 'Alice'; Email = 'alice@example.com'; Role = 'Admin' }, [PSCustomObject]@{ Name = 'Bob'; Email = 'bob@example.com'; Role = 'User' }, [PSCustomObject]@{ Name = 'Alice'; Email = 'alice@example.com'; Role = 'User' } ) # Attempt to filter unique users based on Email $uniqueUsers = $users | Select-Object -Property * -Unique $uniqueUsers ``` Running this gives me all three user records instead of just the unique ones based on the `Email`. The output shows: ``` Name Email Role ---- ----- ---- Alice alice@example.com Admin Bob bob@example.com User Alice alice@example.com User ``` I also tried using `Group-Object` to group by `Email`, but Iām not sure how to extract the first user from each group effectively. Can anyone provide a solution to achieve this? I'm looking for a clean and efficient way to get only one object per unique email, preserving the desired properties from the original objects. My team is using Powershell for this web app. I appreciate any insights! This is for a service running on Ubuntu 22.04. What are your experiences with this?