Thursday, 9 May 2013

Flooding your powershell pipeline

This one get's me every time.

I come to reading the input pipeline, using

$input

Only to find, that the object I thought would be there, was something else.

What it normally means is that the previous part of my pipeline, has some how dumped rubbish into the pipeline that I didn't want, or was expecting. And, $input now includes this.

As a rule, I normally use Write-Output to put an object into the pipeline, and its the last thing I do in my script.

This makes it easier for my next pipeline action to pickup

$input | Select-Object -last 1

This works most of the time for me, as whatever else leaks into the pipeline, I know that my object, the one that counts, is last in the line.

It's on my list of things to understand better:

  1. Can I flush the output before my script exists
  2. Aside from using Out-Null on any scripts or cmdlets I invoke, is there any other way to surpress their output.