Does Select-Object -First terminate native executable processes in the pipeline?
0 reputation · 15 Nov 2021, 08:00 UTC
In PowerShell, Select-Object -First N is documented to stop the upstream pipeline once the specified number of objects is received. This mechanism prevents upstream cmdlets from running to completion, which is critical for performance when handling large datasets.
However, there is uncertainty regarding how this early termination behaves when the upstream source is a native executable rather than a PowerShell cmdlet. While a cmdlet may stop emitting objects, a native process writing to stdout may behave differently depending on the PowerShell edition and how the pipe is handled by the underlying host.
Constraints
- The goal is to determine if the native process is actually terminated or if it continues to execute and write to a closed pipe.
- Behavior may vary between Windows PowerShell 5.1 and PowerShell 7.x.
Which behavior is expected when piping a long-running native command into Select-Object -First? Does the native process receive a signal to terminate, or does it continue running until completion despite the pipeline being closed by the downstream cmdlet?