PSCustomObject does not work with Select-Object -Unique and Sort-Object -Unique
See original GitHub issuePrerequisites
- Write a descriptive title.
- Make sure you are able to repro it on the latest released version
- Search the existing issues.
- Refer to the FAQ.
- Refer to Differences between Windows PowerShell 5.1 and PowerShell.
Steps to reproduce
Given any collection of PSCustomObjects with different fields, they will not compare equal but still collapse into a single item under Select-Object -Unique or Sort-Object -Unique. Notable cmdlets outputting PSCustomObject collections are:
Select-Objectitself (-Uniqueworks when used immediately, but not later on)ConvertFrom-Csv
This behavior has been mentioned in https://github.com/PowerShell/PowerShell/issues/15806 and https://github.com/PowerShell/PowerShell/issues/12059 , but neither are for this exact problem, and it affecting Select-Object transformations and ConvertFrom-Csv makes it very prominent.
Expected behavior
> $files = Get-ChildItem | Select-Object Name
> $files
Name
----
dotnet-sdk-5.0.408-linux-arm64
dotnet-sdk-6.0.400-linux-arm64
> $files[0] -eq $files[1]
False
> $files | Select-Object -Unique
Name
----
dotnet-sdk-5.0.408-linux-arm64
dotnet-sdk-6.0.400-linux-arm64
Actual behavior
> $files | Select-Object -Unique
Name
dotnet-sdk-5.0.408-linux-arm64
Error details
No response
Environment data
Name Value
---- -----
PSVersion 7.3.0-preview.3
PSEdition Core
GitCommitId 7.3.0-preview.3-304-gd02c59addc24e13da3b8ee5e1a8e7aa27e00c745
OS Linux 5.15.0-1013-raspi #15-Ubuntu SMP PREEMPT Mon Aug 8 06:33:06 UTC 2022
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
Visuals
No response
Issue Analytics
- State:
- Created a year ago
- Comments:40 (16 by maintainers)
Top Results From Across the Web
Select unique objects from a set of powershell custom objects
I have an array of powershell custom objects, which may contain duplicates in it, and I want to remove the duplicates from the...
Read more >How can you select unique objects based on two ...
The object needs to be sorted because Get-Unique compares the strings adjacent to each other. Additionally, the comparison is case-sensitive.
Read more >Use PowerShell to Choose Unique Objects from a Sorted List
The Sort-Object cmdlet is not case sensitive when choosing unique objects from the list.
Read more >Get-Unique (Microsoft.PowerShell.Utility)
The Get-Unique cmdlet compares each item in a sorted list to the next item, eliminates duplicates, and returns only one instance of each...
Read more >Filtering for Unique Objects in PowerShell
An exploration of techniques for filtering for truly unique objects in PowerShell. This is something you might need when importing data.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@PowerShell/wg-powershell-cmdlets reviewed this and agree that the current behavior for PSCustomObject is incorrect. We believe this is likely a bucket 3 breaking change that users are relying on the current behavior. We considered an option to inform the user to use
-Property *if we detect that the objects are PSCustomObjects, however, it seems more useful to fix this behavior that future users can rely upon. So the change is only if the first object is aPSCustomObjectand-Propertyisn’t specified, then it gets set to-Property *.Looking at the results of the proposed fix, I am not satisfied. While the main scenario is fixed by this fix (usually all objects of the same type in the pipeline), I’m afraid that we’ll immediately get feedback that it doesn’t work if the first object isn’t a PSCustomObject. Since we’re doing a slow accumulation of objects from the pipeline anyway, there’s nothing stopping us from checking their type until we encounter a PSCustomObject.
This in turn makes me think that this fix is more of a workaround and the main problem is somewhere deeper and should be fixed, although it’s not trivial anymore.