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.

# Checking containment in arrays
"apple", "orange", "grape" -contains "mango" "apple", "orange", "grape" -contains "orange"

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

# Checking containment in string
$string = "Tomasz Decker"
$string -contains "Decker"
$string -contains "Tomasz Decker"

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:

$string = "Hello, this is Tomasz Decker"
# Check if the $string starts with "Hel"
$string -like "Hel*"
# Check if the $string contains the substring "Tomasz" $string -like "*Tomasz*" # Matches a substring in the $string that starts with "T" followed by a vowel. "To" is matched. $string -like "*T[aeiou]*" # -like operator is case insensitive $string -like "*This*" # $string does not contain the substring "Helo" $string -like "*Helo*"

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.

$string = "Hello 2719, this is Tomasz Decker"
# Checks if "Tomasz" is in $string
$string -match "Tomasz"
# Checks if $string starts with "Hello"
$string -match "^Hello"
# Checks if $string ends with whitespace(s) (\s+) followed by “Decker”
$string -match "\s+Decker$"
# Check if the $string contains a number
$string -match "\d+"
# Check if $string contains whitespace
$string -match "\s+"
# Check if $string contains the substring "this is"
$string -match "this is"

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 = "Hello World!"
if($string -like "*World*"){
Write-Host "String contains the substring."
}else{
Write-Host "String does not contain the substring."
}

Output:

String contains the substring.

Conclusion

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.