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.



Windows PowerShell Examples of -Match and -Like

Introduction to PowerShell's -Match and -Like

I want to show you how to filter data with PowerShell's -Match.  The scenario is that we want research WmiObjects in general and 'network' classes in particular.  The problem is that we list WMI classes we are swamped with hundreds of names.  Our solution will be to use a 'Where-Object' clause, with a comparator of -Match or -Like.

Topics for PowerShell -Match and -Like

 ♣

Differences between -Match and -Like

The way that I remember the difference is that I think of '-Match' as meaning a pure pattern match.  Whereas to me, '-Like' is a more nebulous concept that is going to need the wildcard* symbol.  A more technical definition is as follows: -Like is just a wildcard comparison, while -Match is a regular expression, and a superset of -Like.

Example 1: The -Match Comparator in a 'Where' Clause

WmiObject is our parent clause for this example.  The where-Object statement employs a comparator to find the pattern "network".  Because we use -Match, "network" can be anywhere in the WMI object's name.  Incidentally, this combination of 'Where' with '-Match' is my favourite method of filtering data.

# PowerShell example to list demonstrate -Match
# Author: Guy Thomas
# Version 1.4 December 2008 tested on PowerShell v 1.0

clear-Host
$WMI = Get-WmiObject -list | where-Object {$_.name -match "network"}
$WMI
Write-Host $WMI.count "WMI objects contain the word network"

Learning Points

Note 1:  Take special note of the important construction $_.   Dollar, underscore, dot means 'in this data stream'.

Note 2:  Observe how I have employed PowerShell's signature tune the (|) pipe.  The purpose of the pipe is to make the output of list, the input into the where-Object clause.

Example 2: The -Like Comparator

I have made two changes compared with Example 1; I substituted -Like for -Match and adding a wildcard* in front of network.  -Like says to me 'sort of', 'fuzzy', or 'around about'.

# PowerShell example to demonstrate -Like
# Author: Guy Thomas
# Version 1.4 December 2008 tested on PowerShell v 1.0

clear-Host
$WMI = Get-WmiObject -list | where-Object {$_.name -like "*network"}
$WMI
Write-Host $WMI.count "WMI objects contain the word network"

Learning Points

Note 1:  The variable $WMI.count enables us to compare these results with those of example 1; you should see that -Like produces fewer WmiObjects then -Match.  To make -Like show all the "network" object you need to add a second wildcard: "*network*".

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: The -Match Comparator with an If Construction

To give you perspective, I have introduced variations, for example employing an 'If' construction instead of the 'Where' in example 1 and 2.

clear-Host
$i=0
Get-WmiObject -list | foreach {
    if($_.name -match "System") {
    write-Host $($_.name) ; $i++
    }
}
"There are $i objects containing System"

Learning Points

Note 1:  Unlike 'Where', the 'If' statement needs the additional command 'foreach'. What this does is initialize the loop containing $_.name.

Note 2:  The syntax of the 'If' construction is (test) then {script block}

Note 3:  In the line: "There are $i objects containing System"
admire how PowerShell interprets the counting variable ($i) in the middle of that text string.  This is called expanding a variable, and only works if you encase in "Double quotes".

See more PowerShell -Match examples.

Example 4: More Comparators -NotMatch also -And

The simplest and most common comparator is 'equals'.  The way you code this in PowerShell with -eq  (not =).  However, in the present examples, -eq would not be much use because effectively we would need to know the name of the specific class.  Whereas in this context we don't know the answer, and we want to broaden our research into possible names containing 'Win32'.

Thus let examine other comparators.  It is surprising how often the negative -NotMatch produces a neat solution to a scripting problem.  For instance, there are several WmiObjects beginning with CIM, thus one way of excluding them would be to use -NotMatch "CIM".

Furthermore, using multiple criteria for your filter is easy once you master the deceptively simple, '-And' syntax.  Just remember that you need to still need to add the left side of the comparator ($_.name) after the -And.

# PowerShell example to list demonstrate -NotMatch and -and
# Author: Guy Thomas
# Version 1.2 December 2008 tested on PowerShell v 1.0

clear-PSAhost
$WMI = Get-WmiObject -list `
| where-Object {$_.name -NotMatch "CIM" -And $_.name -NotMatch "__"}
$WMI
Write-Host $WMI.count "WMI objects not contain CIM or __"

Learning Points

Note 1: It would make my day if you experimented with different filters.  Substitute your ideas for "CIM", and "__".  Perhaps best of all would be to combine -NotMatch and -Match.

Guy Recommends:  Solarwinds' Free Bulk Import ToolFree Download of Solarwinds  Bulk Import Tool

Import users from a spreadsheet.  Just provide a list of the users with their fields in the top row, and save as .csv file.  Then launch this FREE utility and match your fields with AD's attributes, click to import the users.  Optionally, you can provide the name of the OU where the new accounts will be born.

There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

Follow-up

As usual with my scripts, the mission is to get you started.  If you want to know more about -Match, -Like and their relatives, then start with PowerShell's own help thus:
Get-help about_comparison_operators.  (Or try Get-help about*)

These help files introduce a whole world of specific terms, for example, 'regular expression comparisons' and 'wildcard comparison'.  Once you need and understand such extra information, then I have succeeded in my mission of introducing you to -Match and -Like.

Summary of PowerShell Conditional Operators

So often we suffer from information overload. Working with PowerShell is no different, however it does supply three conditional operators to filter your information: -match, -like and -contains. Each operator has different properties; with research, you can get just the filter you need, and thus filter the desired stream of information into your script's output.

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

 


See more PowerShell examples for syntax advice

PowerShell Tutorials  • Syntax  • PowerShell functions  • Plist  • RegEx  • -Com

PowerShell -confirm  • WhatIf  • -Match  • -Like  • -Online  • Where

-ErrorAction  • -Replace  • Windows PowerShell  • PowerShell module directory

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.

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.