Skip to content

Select-Object: Add a -NotIndex parameter to complement -Index to select all BUT the objects at the specified positions #13769

@mklement0

Description

@mklement0

See also: #11752

Summary of the new feature/enhancement

Select-Object -Index allows me to select a subset of the input objects by an array of indices (0-based positions in the input stream):

PS> 1..5 | Select-Object -Index 1, 3  # here the same as: (1..5)[1,3]
2
4

It would be helpful to be able to select the complement, i.e. all elements other than the ones specified, using a new -NotIndex parameter:

Update: The original proposal suggested a separate -Not switch to be combined with -Index, but this was changed based on @jhoneill's suggestion.

# WISHFUL THINKING: -Not returns everything *but* the objects at the specified positions.
PS> 1..5 | Select-Object -NotIndex 1, 3 
1
3
5

For instance, it would allow you to exclude a line with a given number from an array of a file's input lines:

# WISHFUL THINKING
PS> 1..3 + 'exclude me' + 4..5 > sample.txt; Get-Content sample.txt | Select-Object -NotIndex 3
1
2
3
4
5

Note: While it is hypothetically possible to allow combining -Index with -NotIndex, it wouldn't be easy to reason about by the user and is probably of little practical value.
Thus, -Index and -NotIndex should be made mutually exclusive.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Issue-Enhancementthe issue is more of a feature request than a bugWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions