Remove duplicate entries from PowerShell object while keeping PSObject intact

supermerio picture supermerio · Oct 14, 2014 · Viewed 9.7k times · Source

I have created a PSObject with multiple properties. I would like to remove duplicate entries from the PSObject using a specific property as a reference.

I'd like to do this while keeping the PSObject in its original format.

Examples I have seen extract the non-duplicate values. I want to delete the duplicate values.

I'm trying to turn this:

DistinguishedName                             Name    
-----------------                             ----
OU=Users,DC=Domain,DC=Local                   Users
OU=Users,DC=Domain,DC=Local                   Users

Into this:

DistinguishedName                             Name    
-----------------                             ----
OU=Users,DC=Domain,DC=Local                   Users

I have a feeling i'm missing a trick here...

Thanks

Answer

Mathias R. Jessen picture Mathias R. Jessen · Oct 14, 2014

That looks like the Format-Table output of a collection of PSObjects with the same properties.

To weed out duplicates, use Sort-Object -Unique:

$uniqObjects = $psobjects |Sort-Object -Unique