Add an -Until
script block parameter to Select-Object
#21610
Labels
Issue-Enhancement
the issue is more of a feature request than a bug
Needs-Triage
The issue is new and needs to be triaged by a work group.
WG-Cmdlets
general cmdlet issues
WG-NeedsReview
Needs a review by the labeled Working Group
Summary of the new feature / enhancement
As a user, I want to stop processing input after encountering a specific value in the pipeline.
A simple example:
A more realistic example:
Since
StopUpstreamCommandsException
, which is internally used bySelect-Object
to implement its early-exit behavior is internal, I don't believe there's currently any way to implement this without reflecting PowerShell internals. Using something like... | % {if (...) {break}; $_}
may seem like an attractive option, but as documented, it terminates much more (the whole runspace) than a user would naively expect.A more general solution could be to make
StopUpstreamCommandsException
public, so that users can write their own cmdlets with support for early exit, which is one of the possible solutions for #3821. However, even if that gets implemented, I believe the-Until
parameter could still be valuable for simpler use cases.Proposed technical implementation details (optional)
Add an
-Until
script block parameter toSelect-Object
, probably in a new parameter set, run the passed script block for each pipeline item and throwStopUpstreamCommandsException
when the script block returns a truthy value.I can open a PR if the feature is accepted. (although I'd kinda appreciate if someone found time to review my other PRs first, nudge nudge wink wink :) )
The text was updated successfully, but these errors were encountered: