How to Check if a String Contains a Substring in PowerShell?

Recently, I got a requirement from one of my clients to check if a string contains a substring. I tried various methods in PowerShell. In this tutorial, I will explain how to check if a string contains a substring in PowerShell using various methods with examples.

To check if a string contains a substring in PowerShell, you can use the -like operator with wildcards (*substring*). You can also use the Contains() method ($string.Contains($substring)) for case-sensitive checks. Lastly, using the Select-String cmdlet or IndexOf method ($string.IndexOf($substring) -ne -1) can also achieve this.

Method 1: Using the -like Operator

In PowerShell, we can use the -like operator to check if a string contains a substring. It uses wildcard characters to match patterns.

Here is a complete PowerShell script.

$string = "Hello, PowerShell World!"
$substring = "PowerShell"

if ($string -like "*$substring*") {
    Write-Output "The string contains the substring."
} else {
    Write-Output "The string does not contain the substring."
}

In this example, the * wildcard character represents any sequence of characters, so *PowerShell* matches any string that contains “PowerShell”.

I executed the above script, and you can see the output in the screenshot below:

Check if a String Contains a Substring in PowerShell

Read Case Insensitive Strings Comparison in PowerShell

Method 2: Using the -match Operator

Here is another method to check if a string contains a substring in Powershell. You can use the -match operator with regular expressions to perform pattern matching. Here is another example.

$string = "Hello, PowerShell World!"
$pattern = "PowerShell"

if ($string -match $pattern) {
    Write-Output "The string contains the substring."
} else {
    Write-Output "The string does not contain the substring."
}

Here, the -match operator checks if the $string matches the regular expression pattern stored in $pattern.

You can check the screenshot below for the output after I executed the script using VS code.

How to Check if a String Contains a Substring in PowerShell

Method 3: Using the Contains Method

The Contains() method is another way to check if a substring exists within a string in PowerShell. This method is case-sensitive.

Here is the complete PowerShell script.

$string = "Hello, PowerShell World!"
$substring = "PowerShell"

if ($string.Contains($substring)) {
    Write-Output "The string contains the substring."
} else {
    Write-Output "The string does not contain the substring."
}

In this example, the Contains method returns True if $substring is found within $string, otherwise, it returns False.

Read Compare Strings in PowerShell

Method 4: Using the Select-String Cmdlet

The Select-String cmdlet is similar to the Unix grep command and is used to search for text patterns in strings and files. It’s particularly useful for searching through large text files or logs.

Here is an example.

$string = "Hello, PowerShell World!"
$substring = "PowerShell"

if ($string | Select-String -Pattern $substring) {
    Write-Output "The string contains the substring."
} else {
    Write-Output "The string does not contain the substring."
}

Here, Select-String searches for the pattern specified by $substring within $string and returns a match object if found.

Method 5: Using the IndexOf() Method

The IndexOf() method returns the zero-based index of the first occurrence of a substring within a string in PowerShell. If the substring is not found, it returns -1.

Here is another example.

$string = "Hello, PowerShell World!"
$substring = "PowerShell"

if ($string.IndexOf($substring) -ne -1) {
    Write-Output "The string contains the substring."
} else {
    Write-Output "The string does not contain the substring."
}

In this example, IndexOf checks if $substring exists in $string by returning its starting index or -1 if not found.

You can see the output in the screenshot below for the output.

PowerShell Check if a String Contains a Substring

Conclusion

PowerShell provides multiple ways to check if a string contains a substring; you can use the -like operator, regular expressions with the -match operator, the Contains() method, using the Select-String cmdlet, or the IndexOf() method, etc.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.