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 - Conditional Operators

Introduction to PowerShell Conditional Operators

I think of PowerShell's conditional operators as data filters.  Perhaps you are suffering from a common problem - too much information?  If so, then choosing the most suitable PowerShell operator: -match, -like or -contains will help you to distil the key facts.

Introduction to: -match  -like and -contains.

-Match, -like and -contains are all similar PowerShell conditional operators, yet each has a subtle specialization.  My advice is keep experimenting until you find the particular conditional operator that suits your circumstance.  Have faith that you will be able to manipulate your data with one of this trio, and thus achieve the degree of pattern matching that you seek. 

Topics for PowerShell's Conditional Operators

Please note: the above operators are in addition to the ubiquitous comparison operators, -eq, 'If' and 'elseif'.

 ♣

Example 1 -Match Conditional Operator

The 'match' can be anywhere within the string.  Moreover, the pattern does not have to be a complete, and this is the biggest benefit of match.  -Match uses regular expressions for pattern matching.  Incidentally -match, and the other PowerShell conditional operators, all have a negative form, for example -notMatch.

Example 1a - Match does not have to be at the beginning

$Guy ="Guy Thomas 1949"
$Guy -match "Th"

# Result PS> True

Example 1b - Naturally a completely wrong name is no good

$Guy ="Guy Thomas 1949"
$Guy -match "Guido"

# Result PS> False

Example 1c - Wrong date

$Guy ="Guy Thomas 1949"
$Guy -match "1939"

# Result PS> False

Note 1c: While Guy Thomas 1949 has '19', it does not have '1939', thus returns False.

Example 1d - Wildcard?  Rides to the rescue

$Guy ="Guy Thomas 1949"
$Guy -match "19?9"

# Result PS> True

Note 1d:  Now we introduce the ? in the 3rd position and get a match for the other 3 digits, consequently the result is now 'True'.

Example 1e - WmiObject and Where, using * as a Wildcard

Here is a real-life -match example using WmiObject and a where clause, observe the Wildcard*.

Get-WmiObject -list | where {$_.name -match "cim*"}

See more -match comparisons.

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

Additional PowerShell Character Classes

A character class is a broader form of wildcard, which represents an entire group of characters.  You can use these for really wide-ranging searches.  PowerShell recognizes the following character classes:

\w matches any word character, meaning letters and numbers.
\s matches any white space character, such as tabs, spaces, and so forth.
\d matches any digit character.

There are also the reverse or negative versions which employ a capital letter.  (Can be hard to get a false with these!)

\W (capital W) any non-word character
\S any non-white space.
\D any non digit. 

Negative -notMatch

For negative conditions, there is an alternative form called -notmatch.  PowerShell also has ErrorAction silentlyContinue.

Example 2 -Like Conditional Operator

With PowerShell's -like, both sides of the expression have to be the same, fortunately, you can employ the usual wildcards * and ?

Example 2a - Having only part of the string is no good for -like

$Guy ="Guy Thomas 1949"
$Guy -like "Th"

# Result PS> False

Note 2a:  Subsitute -match for -like and the result would be 'True'.

Example 2b - Just the start of the string not enough

®

$Guy ="Guy Thomas 1949"
$Guy -like "Guy"

# Result PS> False

Example 2c - Wildcard * is useful for -like

$Guy ="Guy Thomas 1949"
$Guy -like "Guy*"

Result PS> True

Example 2d - Wildcard*  but the rest has to be correct

$Guy ="Guy Thomas 1949"
$Guy -like "Gzkuy*"

# Result PS> False

Note 2d:  Wildcards return more positives, but you have to have the base part correct.  In this instance 'Gzkuy' is not good enough to return 'True'.

Example 2e - * Wildcards *  Double wildcards are handy

$Guy ="Guy Thomas 1949"
$Guy -like "*Th*"

# Result PS> True

If your logic needs the negative, then try -notlike.  In addition, -like -and -notlike have variants that force case sensitivity. -clike and -cnotlike. 

The Difference Between PowerShell -like and -match

$Guy ="Guy Thomas 1949"
$Guy -like "Th*"

# Result PS> False

$Guy ="Guy Thomas 1949"
$Guy -match "Th*"

# Result PS> True

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 3 -Contains PowerShell Conditional Operator

The conditional operator -contains is similar to -eq, except it returns 'True' or 'False'.  -contains is designed for situations where you have a collection and wish to test one particular item.

Example 3a - Checks each item between the commas

# PowerShell -Contains
$name = "Guy Thomas", "Alicia Moss", "Jennifer Jones"
$name -contains "Alicia Moss"

# Result PS> True

Example 3b  -Contains requires exact equality

# PowerShell -Contains
$name = "Guy Thomas", "Alicia Moss", "Jennifer Jones"
$name -contains "Jones"

# Result PS> False

Example 3c - Wildcards are a waste of time with -contains

# PowerShell -Contains
$name = "Guy Thomas", "Alicia Moss", "Jennifer Jones"
$name -contains "*Jones"

# Result PS> False

About_Comparison_Operators

# For even more information about PowerShell Operaators try:

help About_Comparison_Operators

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 Windows PowerShell examples

PowerShell Home  • PowerShell If Statement  • PowerShell ElseIf  • PowerShell If -Not

PowerShell comparison operators  • PowerShell If -And  • PowerShell If -Or

Where Filter   • Loops  • Brackets  • -Match  • Switch  • Conditional Operators

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.