Stopping the Pipeline

by Mar 12, 2010

Usually, once a pipeline runs, you cannot stop it prematurely, even if you already received the information you were seeking. Simply use this filter to stop a pipeline:

filter Stop-Pipeline([scriptblock]$condition = {$true}) {
$_
if (& $condition) {continue}
}

Now, to stop a pipeline, use it like this:

Get-EventLog Application | Stop-Pipeline { $_.InstanceID -gt 10000}

This will get you all Application events until you find one with an InstanceID of greater than 10,000. The pipeline exists once it is found.

If you'd like to use this technique inside a script, you will need to make sure Stop-Pipeline does not stop your entire script. You can do that by embedding the pipeline inside a dummy loop:

$result = do {
Get-EventLog Application | Stop-Pipeline { $_.InstanceID -gt 10000}
} while ($false)

Twitter This Tip! ReTweet this Tip!