PowerShell String Contains Operator

PowerShell is a powerful tool for automation and administration tasks in Windows operating systems. It comes with a lot of built-in features and commands.

This article will discuss three operators to check whether a string contains a specific substring. These operators are:

  1. <string> -contains <substring> (be careful with this one)
  2. <string> -like <substring [wildcards accepted]>
  3. <string> -match <substring [regex accepted]>

The <string> -contains operator

The -contains operator is very strange. You probably want to use it to check if <string> contains <substring> in

<string> -contains <substring>

But it may not work as you might expect. This operator cannot check for a substring in a given string; instead, it is used to check if a given collection (an array, hashtable or string) contains a given item.

The -contains operator yields what we might have expected on arrays but not on strings. Let’s see an example.

You might have expected $string -contains “Decker” to return True because $string contains the substring “Decker”, but the operator returned False. This is because the operator can only match the complete string, as shown in the second example.

This problem is solved by the -like and -match operators discussed below.

The <string> -like operator

The -like (and its negation -notlike) finds elements that match or don’t match a given pattern. The operators support wildcard expressions (containing *, ? and [ ]).

The anchor * matches zero or more characters:

expression explanation example
*is* string containing the substring “is” is, list, heist
*or string ending with “or” actor, editor, minor
lo* string starting with “lo” love, loss, lotion
he*o string starting with “he” and ending with “o”. hello, hero, helio

“?” matches any single character:

expression explanation example
m???s any 5-letter string starting with “m” and ending in “s”. mires, miles, midas

“[ ]” matches a range of characters:

expression explanation example
h[aeo][aeo]p any 4-letter string that starts with “h”, followed by “a”, “e”, or “o”, and then ends with “p”. heap, hoop

Let’s see some examples on PowerShell:

Note: the -like operator is case insensitive by default. You can use -clike for case-sensitive matching.

The <string> -match operator

Unlike the -like operator, which matches wildcard characters only, -match (and its negation -notmatch) accepts regular expressions (regex). This allows -the match operator to capture more complicated patterns.

Here are some examples.

Note: Like -like operator, -the match is case insensitive. If you want to conduct a case-sensitive search, use the -cmatch operator.

You can read more about regular expressions in regular expressions quick reference and about regular expressions.

Note also that you can integrate these operators into conditional statements, for example.


String contains the substring.


This article discussed 3 PowerShell operators(-contains, -like, and -match operators) to check whether a string contains a specific substring.

While the -contains operator can only check for complete string matches, the -like and -match operators offer more powerful pattern-matching capabilities using wildcard and regular expressions, respectively.