Like vs. Match: Battle of The Powershell Comparison Operators

By Adam Bertram on February 19, 2015

Powershell offers a few different ways to see if a string is inside another string. The most common method  is to use the Like and Match operators. These two operators may seem similar in functionality but, under the hood, they're two very different beasts. Let’s go over each one to see which would win a battle royale! We’ll have two rounds, with the point system as follows: one point for speed, and one point for syntax ease. To get a true comparison of both operators we’ll use the entire text of the book "War and Peace," by Leo Tolstoy. To get this book’s text into a string I’m using the Get-Content cmdlet with the -Raw parameter, which assigns the 1,440 pages of content in a single string rather an an array. $WarAndPeaceString = Get-Content C:\WarAndPeace.txt -Raw The competition will be to see if the text in the novel "War and Peace" contains the word “waldo” at the end of a sentence, so the string will be “waldo.” Before this fight-to-the-death begins, it’s important to give a brief explanation of a Powershell operator. We’ll then take a look at each of our contenders; we want to remember the losing opponent or, at least, the next of kin to notify when it's over.

A Brief Explanation of Powershell Comparison Operators

Like and Match are both Powershell operators; more specifically, they are comparison operators. At their most basic, comparison operators are used to compare values and return either a boolean True or False value. However, this article will focus on using the Like and Match operators to find a string inside another string. Both of our contenders can do this, but each performs the task in a slightly different manner.

Meet the Like operator

The Like operator is a Powershell comparison operator that uses wildcards. A very simple example to find the word "day" in the string, "It is a great day,: would go something like this: PS> 'it is a great day' -like '*day*' This statement would return a True value. However, if I do something like this, it would return a False value: PS> 'it is a great day' -like 'day' Why the difference? Note the asterisks surrounding day in the first example. These are wildcards that must be present in order to represent any kind of text on either side of the word day. In plain English, the first statement is basically saying, “Does the string 'it is a great day' have the word 'day' inside it, with any text to the left or to the right?” Meanwhile, the second statement says, “Does the string 'it is a great day' have the word 'day' in it, but no text to the left and to the right?” The Like operator works well for simple matching like this when you have a simple string with no special characters, and when you know exactly which string you’re looking for.

Meet the Match operator

The Match operator is another Powershell comparison operator. Although similar to Like, it's much more Powershell (yet a little more complicated). The Match operator uses regular expressions (regex). This is an enormous benefit and gives Match a definite leg up on Like. However, if you've never used regular expressions before, prepare yourself. A coworker, when describing regular expressions, once said, “Whoever created regular expressions had to be on drugs.” This couldn’t be more truthful. Let’s go with our previous example string again. PS> 'it is a great day' -match '*day*' I just substituted the word like for match. What do you think would happen? Would it return True, like our previous example, or something else? regexfailure You can see that it operates differently than Like. You’re not just working with simple text. You’ve entered the realm of regex, and things are not the same in regex world. To properly make this work with the Match operator you’d have to remove the asterisks, because asterisks have a special meaning in the regex world. PS> 'it is a great day' -match 'day' The Match operator can do a lot more than just simple matches like this, but for this battle I’ll attempt to make it fair by just comparing similar attributes. With backgrounds out of the way, let’s get down to business.

Round No. 1: Like vs. Match: Which is faster?

Like Let’s see if the like operator can find out if our example string contains “waldo,” and how fast it was. waldolikeresult Pretty fast! The Like operator was able to find the match within about 149 milliseconds. Match Now let’s bring in the Match operator and see how it stacks up. waldomatchresult The winner here is clear; one point for the match operator! Like: 0, Match: 1

Round No.2: Like vs. Match: Syntax ease

To make this round as equal as possible I’m defining “syntax ease” as the operator that is the easiest to understand and requires the least characters. Like Up first, the Like operator. PS> $WarAndPeaceString -like '*waldo.*' Not bad at all with eight characters, but does require you to remember the asterisks on either side of our search string. Match Next up, the Match operator enters the ring and show us what it’s made of! PS> $WarAndPeaceString -match 'waldo\.' I see no asterisks here, so the character count is only seven. However, what is that backslash doing in there? That’s extremely confusing. Since the match operator uses regex, regex requires you to escape certain special characters in the search string. That’s way too weird for a newcomer, so I’m giving Like the point here. Like: 1, Match: 1 From our purely tongue-in-cheek battle it appears that the Like and Match operators are evenly matched. Even though this may or may not be actual reality, I hope you’ve gotten a good demonstration of what each operator can do, along with a little about the syntax differences.

Get our content first. In your inbox.

Loading form...

If this message remains, it may be due to cookies being disabled or to an ad blocker.


Adam Bertram

is an independent consultant, technical writer, trainer and presenter. Adam specializes in consulting and evangelizing all things IT automation mainly focused around Windows PowerShell. Adam is a Microsoft Windows PowerShell MVP, 2015 PowerShell hero and has numerous Microsoft IT pro certifications. He is a writer, trainer and presenter and authors IT pro course content for Pluralsight. He is also a regular contributor to numerous print and online publications and presents at various user groups and conferences. You can find Adam at his site listed below or on Twitter at @adbertram.