Home > front end >  What is the PowerShell "Filter" command type? [duplicate]
What is the PowerShell "Filter" command type? [duplicate]

Time:09-21

I noticed something interesting today while inspecting the output of Get-Command and listing the items underneath the Function: provider. I don't see this used on any loaded built-ins, but one of my third party modules showed an interesting CommandType rather than the expected Application, Function, Cmdlet, or Alias types:

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Filter          New-HashObject                                     3.3.2      Pscx

It is this function and this function only that I can find with this CommandType. I have never seen it before. I dug through the PowerShell docs as well and the only (nearly) direct reference I could find was the FilterInfo class, derived from FunctionInfo. Per its description, I can see that FilterInfo:

Provides information about a filter that is stored in session state.

But that's the end of it, and is about as descriptive as what FunctionInfo is to a Function. I was able to find some other vague references to class names with Filter in the name, but these:

  • Are all string typed and describe Functions (at least the ones I found are).
  • Several have been removed from the latest version of the documentation, but are documented for 5.1.
  • They looked like C documentation, not C# or VB.NET, despite being part of the PowerShell SDK documentation.
    • I know what C /CLI is but generally C examples on .NET constructs are less common than C# or VB.NET, and even less so on documentation dealing with PowerShell.

What is a Filter command? Based on what I've found so far is this seems like some new construct for creating filters in lieu of string-based filtering that hasn't been documented, but these are also programmatic types and there is absolutely no information I can find on what a Filter in PowerShell means as an executable construct. The closest thing I can find is the Input Filter Parameters document, which still only explains string-based filtering and its referenced Supporting Wildcard Characters in Cmdlet Parameters again goes into implementing string-based Filter parameters on cmdlets using wildcards.

CodePudding user response:

From about_Functions > Filters

A filter is a type of function that runs on each object in the pipeline. A filter resembles a function with all its statements in a Process block.

It's basically a simple construct similar to a function that will operate on pipeline input without all the advanced function setup

filter TodaysFiles{
    if ($_.LastWriteTime -gt [datetime]::Today) {$_ | select length, fullname}
}

dir | TodaysFiles
  • Related