It is understandable to expect:
1, 1, 2, 2 | Select-Object -Unique -First 2
to return 1, 2, but it actually returns just 1.
The reason is that -Unique is applied after -First 2 has been applied; generally, speaking, -Unique is applied to whatever output the other arguments result in (-First, -Last, -Index, ....), which is worth clarifying.
Workaround: Chain two Select-Object calls:
1, 1, 2, 2 | Select-Object -Unique | Select-Object -First 2
As an aside: If Select-Object -Unique were implemented efficiently, this would still preserve the streaming benefits and -First's ability to stop the pipeline on demand, but it currently isn't - see PowerShell/PowerShell#7707
Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.
It is understandable to expect:
to return
1, 2, but it actually returns just1.The reason is that
-Uniqueis applied after-First 2has been applied; generally, speaking,-Uniqueis applied to whatever output the other arguments result in (-First,-Last,-Index, ....), which is worth clarifying.Workaround: Chain two
Select-Objectcalls:As an aside: If
Select-Object -Uniquewere implemented efficiently, this would still preserve the streaming benefits and-First's ability to stop the pipeline on demand, but it currently isn't - see PowerShell/PowerShell#7707Document Details
⚠ Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.