Skip to content

Clarify that -Unique is applied to the output selected by Select-Object's other arguments #8224

@mklement0

Description

@mklement0

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.

Metadata

Metadata

Assignees

Labels

area-utilityArea - Microsoft.PowerShell.Utility module

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions