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 -Like

Introduction to PowerShell's Examples of -Like

On this page I will focus on how to filter data with PowerShell's -Like comparator.

One day I wanted to create a WMI script using the network adapter, but even using PowerShell I could not find the right properties, so I went back to basics and listed all the cmdlets containing 'Adapter'.

Topics for PowerShell -Like Operator

 ♣

Differences Between -Like and -Match

In a nutshell, if you are thinking, 'I am probably going to need a wildcard to find this item', then start with -Like.  However, if you are pretty sure of most of the letters in the word you are looking for, then you are better off experimenting with -Match.

Here is a more technical distinction: -Match is a regular expression, whereas -Like is just a wildcard comparison, a subset of -Match.

Example 1: PowerShell's -Like Comparator

This example only lists those cmdlets that end with the letters 'adapter'.

# PowerShell -Like Operator
# Author: Guy Thomas
# Version September 2011 tested on PowerShell v 2.0

Clear-Host
Get-WmiObject -list | Where-Object {$_.name -like "*Adapter"}

Learning Points

Note 1:  Observe how Get-WmiObject uses the -list parameter.

Note 2:  Most comparator scripts are introduced with Where-Object, followed by the pipeline construction: $_.

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 2: Double Wildcards -Match *Adapter*

The problem with Example 1 is that I suspect there are more cmdlets containing the word Adapter, for instance 'adapter' maybe in the middle of the WMI class name.

# PowerShell example to demonstrate -Like
# Author: Guy Thomas
# Version September 2011 tested on PowerShell v 2.0

Clear-Host
Get-WmiObject -list | Where-Object {$_.name -like "*Adapter*"}

Learning Points

Note 2a:  Once again, realise the importance of the construction $_.   Dollar, underscore, dot means 'in this data stream'.

Note 2b:  Thanks to the second *, Example 2 should returns more cmdlets.

See more PowerShell -Match examples.

Example 3: The -Match Comparator Instead of -Like

I like to be flexible with PowerShell's comparators, if the results are not as I anticipated then I may try an alternative such as -Match or -Contains.

# PowerShell example to check with -Match
# Author: Guy Thomas
# Version September 2011 tested on PowerShell v 2.0

Clear-Host
Get-WmiObject -list | Where-Object {$_.name -Match "Adapter"}

Learning Points

Note 3a:  Actually this example should list the same WMI classes as Example 2, but in other scripts you may getter better results using -Match instead of -Like.

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.

Example 4: More Comparators -NotLike

Time for a recap: The simplest comparator is 'equals'.  And the way you code this in PowerShell with -eq  (not =).  However, in these examples, -eq would not be effective, because realistically you would have to know the answer before you could ask the question!

I would like to take this opportunity to examine other comparators.  It is surprising how often the negative -NotLike operator produces a neat solution to a scripting problem.  For instance, there are several WmiObjects beginning with CIM, one way of excluding them would be to Not Like "CIM*".

Furthermore, by using multiple criteria in the filter, you can fine tune the output, to achieve this goal I have added '-And'.  If for any reason -And fails remember this, when appending a second comparator the secret is to add another instance of the comparator ($_.name) after the -And.

# PowerShell example to list demonstrate -NotLike with -and
# Author: Guy Thomas
# Version September tested on PowerShell v 2

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

Learning Points

Note 4a:  My mission is always to get you started.  Now it's over to you; experiment with different filters, substitute your ideas for "CIM", and "__".  Perhaps best of all would be to combine -NotLike and -Like.

Note 4b:  I appended code to count the number of WMI classes, this helps to see which variation of my script returns the most items.

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 -Like Operator

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

Site Home

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

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

Please report a broken link, or an error to: