Guy recommends :
Free Solarwinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



PowerShell -Filter Parameter

PowerShell -Filter Parameter

PowerShell's -filter is an alternative solution for sieving data to the | where-Object {$_.clause}.  Using -filter instead of 'where-Object' 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:  Try 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 only 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 1:  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
Get-WmiObject -class Win32_NetworkAdapterConfiguration `
-Filter "IpEnabled = 'True' " | `
Format-Table IPAddress, DefaultIPGateway, MACAddress -auto

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

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

Note 3: -Filter is technically superior to methods such as 'Where' or 'Include' because it sifts the objects as it retrieves them.

c)  PowerShell Where Filter for Comparison

# 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 1:  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: WMI Monitor and It's Free!Solarwinds Free WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft 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.

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 1: 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 1:  The Keyword is Win32_OperatingSystem.

Note 2:  6.0% would be Vista.

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 Microsoft PowerShell WMI Examples:

Home   • PowerShell Get-WmiObject   • PowerShell -Query   • PowerShell Select

Win32_pingstatus   • WMI Win32_NetworkAdapter   • Win32_NetworkAdapterConfig

Disable NIC   • PowerShell -Filter   • Windows PowerShell   • PowerShell 3.0 Network

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

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.

Fortunately, Solarwinds have created the Free WMI Monitor so that you can actually see and understand these gems of performance information.  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

 

Home Copyright © 1999-2012 Computer Performance LTD All rights reserved

Please report a broken link, or an error.