question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

PSCustomObject does not work with Select-Object -Unique and Sort-Object -Unique

See original GitHub issue

Prerequisites

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-Object itself (-Unique works 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:open
  • Created a year ago
  • Comments:40 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
SteveL-MSFTcommented, Nov 2, 2022

@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 a PSCustomObject and -Property isn’t specified, then it gets set to -Property *.

1reaction
iSazonovcommented, Nov 5, 2022

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found