Geeks With Blogs

Connected Systems Chilled Out Blog Hanging stuff together in a meaningful way with some fun added

For clarity it is best to start with what a filter is and relate it to a function. So here are both descriptions:

Function: A function is a named sequential set of Powershell commands grouped into a code block that my be called once or  many times by its name from anywhere in the rest of the script.

Filter: A filter is similar to a function but is used in an entirely different manner. A filter is an  output target for a pipeline and its job is to filter and then present that data.

 

Filter Syntax:

Filter(Keyword) FilterName (parameters) {Script Block}

 

Example:

This code sample provides a date filter for a simple DIR call.

 

  1: Filter Select-FileAge { 
  2:        param($days) 
  3:        # is it a folder? Then omit: 
  4:        If ($_.PSisContainer) { 
  5:               # do not return folders effectively filtering them out 
  6:        } ElseIf ($_.LastWriteTime -lt (Get-Date).AddDays($days * -1)) { 
  7:               $_ 
  8:        } 
  9: } 
 10: dir "G:\+ Work +\_gsdata_" *.log | Select-FileAge 14 

The output form this call on my PC gives:

Mode      LastWriteTime           Length       Name                                                                                                 
----          -------------                 ------           ------                                                                                                                                               
-a---        31/07/2009     17:25     744014     2009-0731-171821-MACPC-Share.log

-a---        31/07/2009     17:48         2822     2009-0731-172521-MACPC-Share.log           

Posted on Sunday, August 16, 2009 7:13 PM | Back to top


Comments on this post: Powershell: Filters

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © BizTalk Visionary | Powered by: GeeksWithBlogs.net