Windows PowerShell

Guy recommends:
Free config generator

Solarwinds Config Generator

This CG will put you in charge of controlling changes to network routers and other SNMP devices.

Download your free Config Generator



PowerShell -Filter Parameter

PowerShell -Filter Parameter

PowerShell's -filter is a neater solution for sieving data than 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 -neq.

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 Its Free!Solarwinds WMI Monitor

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft operating systems.  Fortunately, Solarwinds have created the WMI Monitor so that you can examine these gems of performance information for free.  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

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.

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.

See More Microsoft PowerShell WMI Examples:

Home   • PowerShell WMI   • WMI Services   • Memory   • Disk   • DNS   • Win32_pingstatus

WMI Class   • Win32_NetworkAdapter   • Win32_NetworkAdapterConfiguration   • Disable NIC

Examples   • Win32_product   • PowerShell -Query   • PowerShell -Filter

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.