PowerShell -Filter Command or Parameter

PowerShell -Filter Parameter

Information overload?  The ideal method when PowerShell delivers too much data is to append -Filter.  Incidentally, newbies refer to PowerShell's -Filter as a command rather than a parameter.

Topics for PowerShell's -Filter Parameter

 ♣

Example 1a: Filter CommonProgramFiles for Executables

The scenario is that we want to find a particular type of file; in this instance executables amongst the Common Program Files.

Get-Childitem -Path $env:CommonProgramFiles -Filter *.exe -Recurse

Note 1: On my machine, $env:CommonProgramFiles translates to C:\Program Files\Common Files>.

Example 1b: Affects of -Filter
Let us tot-up the .exe files filtered using the .count method.

Clear-Host
(Get-Childitem $env:CommonProgramFiles -Filter *.exe -Recurse).count
(Get-Childitem $env:CommonProgramFiles -Recurse).count

#My Results
52 (-Filter *.exe)
1666 (Total with no filter)

Note 2: These examples are purely to put the -Filter parameter though its paces; for a specific task there may be better methods that don't involve -Filter at all.

PowerShell's Explanation of the -Filter Parameter

To find out more about a cmdlet's parameters type:
Get-Help cmdlet-name -Full

-Filter <String>
Specifies a filter in the provider's format or language. The value of this parameter qualifies the Path parameter. The syntax of the filter, including the use of wildcards, depends on the provider.

Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

Required? false
Position? 2
Default value
Accept pipeline input? false
Accept wildcard characters? true

Note 3: The default position for -Filter is 2, after -Path; all very logical.  My point is that if you don't explicitly declare your parameters, whatever is in the second position is assumed to be a filter.

# Both these command filter .exe files, and produce the same results.

Get-Childitem -Path $env:CommonProgramFiles -Filter *.exe -Recurse

Get-Childitem $env:CommonProgramFiles *.exe -Recurse

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Example 2a: Compare PowerShell -Filter with Where-Object

Each time I use PowerShell's Where-Object cmdlet I ask myself, could I get better performance using -Filter instead?  To compare the two techniques I will employ Measure-Object

#Speed of the PowerShell -Filter Command
Clear-Host
Measure-Command{
Get-Childitem $env:CommonProgramFiles -Filter *.exe -Recurse
}
# Result TotalMilliseconds: 103

Example 2b: Comparative Slowness of Where-Object
Where-Object needs to do it's sifting once it receives the stream from the pipeline (|).  The -Filter parameter is quick because the provider applies the filter as it retrieves the objects. 

Measure-Command{
Get-Childitem $env:CommonProgramFiles -Recurse |
Where-Object {$_.Extension -eq "exe"}
}
# Result TotalMilliseconds: 328

Note 3: There are situations where you need Where-Object, but it's always worth thinking, 'Would PowerShell's -Filter command be better here?

Note 4: Occasionally, you get different results with Where-Object and -Filter; for instance -Filter interprets *.exe as *.exe*

List of PowerShell Cmdlets Featuring -Filter

Let us research other which PowerShell cmdlets contain the -Filter parameter. The technique employs Get-Command to retrieve all the cmdlets; then it filters using the .parameter.keys property.

# Research PowerShell Cmdlets for 'filter' command / parameter
Clear-Host
Get-Command | Where-Object { $_.parameters.keys -Contains "filter"} |
Format-Table Name, ParameterSets -AutoSize

Note 5: Here is a useful task for Where-Object.  My installation of PowerShell shows 31 cmdlets containing the parameter "filter".

Note 6: I prefer to employ the conditional operator -Contains rather than -Match.

Selected Results: Cmdlets Containing -Filter

Most of the results for -Filter use either the noun 'Item' or 'Content'.

Name
—-
Add-Content
Clear-ItemProperty
Copy-Item
Get-Acl
Get-ChildItem
Get-Content
Get-Item
Get-Job
Get-WmiObject
Invoke-Item
Move-Item
New-ItemProperty
Remove-Item
Set-Content
Set-Item
Test-Path

Note7: Observe how most of the results contain either the noun 'Item', or 'Content'.

However, I must give a special mention for Get-WmiObject as I have found many examples of -Filter speeding up scripts that formerly employed Where-Object.

Engineer's Toolset v10Guy Recommends: SolarWinds Engineer’s Toolset v10

This Engineer’s Toolset v10 provides a comprehensive console of 50 utilities for troubleshooting computer problems.  Guy says it helps me monitor what’s occurring on the network, and each tool teaches me more about how the underlying system operates.

There are so many good gadgets; it’s like having free rein of a sweetshop.  Thankfully the utilities are displayed logically: monitoring, network discovery, diagnostic, and Cisco tools.  Try the SolarWinds Engineer’s Toolset now!

Download your fully functional trial copy of the Engineer’s Toolset v10

Example 3: PowerShell Sifting on Multiple Parameters

To date, I have not been able to persuade the -Filter parameter to accept multiple values.  There seems no way to introduce the concept 'or' to -Filter.  One work-around is to reconsider the Where-Object construction; now you have a rich source of comparison parameters (-Match -Contains), not to mention -Or itself.

Get-ChildItem $env:CommonProgramFiles -Recurse |
Where-Object {$_.extension -match "exe" -Or $_.Extension -match "dll"}

Filter with -Include
Another Work-around is to employ Get-ChildItem's -Include parameter.

Get-ChildItem $env:CommonProgramFiles -Recurse -Include "*.exe", "*.dll"

Note 8: Testing with Measure-Command reveals there is no difference in the time it takes to return the list of files.

Note 9: Measuring with the .count method reveals that Where-Object finds slightly more files than -Include.

See more on PowerShell WMI -Filter »

Summary: PowerShell -Filter Command

PowerShell has several methods for dealing with cmdlets producing too much data.  My first choice would be to append -Filter.  Alternative techniques would involve the Where-Object cmdlet, or else the -Include parameter.

If you like this page then please share it with your friends

 


See more examples of PowerShell Parameters

PowerShell Parameters  • Syntax  • PowerShell functions  • PowerShell Wmi Filter

PowerShell -confirm  • WhatIf  • -Match  • -Like  • Where  • Free WMI Monitor

PowerShell -Filter Command  • PowerShell Filter   • -Replace  • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.