Idera nSoftware Compellent

PowerShell.com

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

PowerTip of the Day

Email my PowerTip:
Twitter my PowerTip:

Stopping the Pipeline

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!

Copyright 2010 PowerShell.com. All rights reserved.