PowerShell -Filter Parameter

PowerShell -Filter WMI Parameter

PowerShell's -filter is an alternative solution to the | Where-Object {$_.clause} when sieving data.  Using -filter instead of 'Where' is a classic case of when you find a good technique, look for an even better modus operandi.

Topics for PowerShell Filters

 ♣

Preparation with PowerShell's Get-Help

Before we use PowerShell's -filter parameter, I suggest that we investigate the cmdlet with Get-Help.  For example, Get-Help Get-WmiObject.  My point is that we need to check that any particular cmdlet supports the -filter parameter.

# Help with PowerShell WMI object:
Get-Help Get-WmiObject -full

Note 1: If you prefer to see examples, this is why I append -full.

Note 2: Check other cmdlets such as Get-ChildItem (contains -filter) or Format-Table (no -filter parameter).

Example 1: PowerShell Filter Files

Here is a simple example to reduce the number of files displayed, we are only interested in those which are dlls.

# PowerShell Filter for dll files
Get-ChildItem -Path "C:\Windows\System32\" -filter *.dll

Example 2: PowerShell Get-WmiObject Filter

PowerShell's -Filter uses the syntax of the WMI Query Language (WQL), which is a subset of SQL.  One point to clarify is that WQL uses 'traditional' operators such as "=", whereas PowerShell uses "-eq".

a) Preliminary: No Filter

# PowerShell Get-WmiObject
Win32_NetworkAdapterConfiguration |
Format-Table IPAddress, DefaultIPGateway, MACAddress -auto

Note 3: The problem could be too many items, or a mixed bag results.  Suppose that we wanted to perform an operation on only some of the returned items?  The answer is to employ PowerShell and filter the output.

b) Same Command, but Filtered for IpEnabled Adapters

# PowerShell Get-WmiObject Filter
Clear-Host
Get-WmiObject -class Win32_NetworkAdapterConfiguration -Filter "IpEnabled = 'True' " |
Format-Table IPAddress, DefaultIPGateway, MACAddress -auto

Note 4: Remember the "Double Quotes" around your -Filter parameter.

Note 5: = 'True', or ='False' is correct.  WQL does not understand PowerShell's -eq or -ne.

Note 6: -Filter is technically superior to methods such as 'Where' or 'Include' because it sifts the objects as it retrieves them.  See more on the PowerShell -Filter command.

c)  PowerShell Where-Object for Comparison with Above -Filter

# PowerShell Get-WmiObject -Filter replaced with where-Object
Get-WmiObject -class Win32_NetworkAdapterConfiguration |
Where-Object {$_.IpEnabled -eq 'True'} |
Format-Table IPAddress, DefaultIPGateway, MACAddress -auto

Note 7:  Where-Object achieves exactly the same result, but it needs an extra pipe (|).  My problem is remembering the $_. syntax.  I often forget the _ as in: $.IpEnabled.  PowerShell -filter output is shorter than the where-Object clause.

Guy Recommends: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft's operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try - it's free.

Download your free copy of WMI Monitor

Example 3: Logical Disk Filter

# PowerShell filter for disks
Get-WmiObject -Class Win32_LogicalDisk -Filter "MediaType=12"

Note 8: MediaType 12 for LogicalDisk means 'Fixed hard disk media'  as opposed to floppy.

Example 4: WMI Filter GPO

WMI Filters allow you to select only computers that meet your chosen criteria.  Naturally, your Group Policy will only apply to the objects that match your filter.  Here is a WMI GPO filter for Windows 7

Select * from Win32_OperatingSystem WHERE Version like "6.1%" AND ProductType="1"

Note 9:  The Keyword is Win32_OperatingSystem.

Note 10:  6.0% would be Vista, and 6.2% is Windows 8.

See more on WMI GPO Filtering »

Summary of PowerShell -Filter

When you need to sift data you have a choice between a where-Object clause and a -Filter parameter.  The -Filter solution is easier and probably quicker. PowerShell's -Filter parameter looks for a WMI Query Language (WQL) statement.

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.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to take the guess work out of which WMI counters to use for applications like Microsoft Active Directory, SQL or Exchange Server.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2017 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: