Functions vs Filters

Functions vs Filters

  • Comments 3

In my previous entry, I referred to Rsbohn's blog PowerShell $Profile and his function to slow down output

function slowly ($tempo=100) { process { $_; Start-Sleep -milliseconds $tempo}}

This is a good candidate for a FILTER. A filter is function that just has a process scriptblock. So you could have written his as:

filter slowly ($tempo=100) { $_; Start-Sleep -milliseconds $tempo}

Enjoy!

Jeffrey Snover [MSFT]
Windows PowerShell/MMC Architect
Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

Leave a Comment
  • Please add 3 and 8 and type the answer here:
  • Post
  • Funny, I just read about FILTERs in Bruce Payette's book. Now if I can just figure out Chapter 8...

  • So uhm (I know this is late, but this is still the #1 Google hit for Functions vs Filters): _Why_ would it be better as a filter? Is it just because the filter name would let everyone know that all it has is a process block?  Or is there some actual benefit to this...

  • I don't get the sentence "A filter is function that just has a process scriptblock". Isn't that wrong?

    I just wrote a test filter (below); the code in the BEGIN and END block also gets executed.

    Filter DoIt {

     BEGIN {

       ...

     }

     PROCESS {

       ...

     }

     END {

       ...

     }

    }

Page 1 of 1 (3 items)