How to Check if a String Contains Multiple Values in PowerShell?

While working on a PowerShell script, I was required to check if a string contains multiple values. I tried different methods, and in this tutorial, I will show you how to do so with complete scripts and examples.

To check if a string contains multiple values in PowerShell, you can use the -match operator with a regular expression. It allows you to check for multiple patterns in a single line (e.g., $string -match “quick|lazy|fox”).

Now, let us check all the methods.

Method 1: Using the -match Operator with Regular Expressions

In PowerShell, you can use the -match operator with regular expressions to check if a string contains any of multiple substrings.

Here is a complete PowerShell script.

$string = "The quick brown fox jumps over the lazy dog"
$patterns = "quick|lazy|fox"

if ($string -match $patterns) {
    Write-Output "The string contains one of the specified values."
} else {
    Write-Output "The string does not contain any of the specified values."
}

In this example, the -match operator checks if $string contains any of the words “quick”, “lazy”, or “fox”. The pipe (|) character in the pattern acts as an OR operator.

Look at the screenshot below after I executed the PowerShell script using VS code.

Check if a String Contains Multiple Values in PowerShell

Read How to Split Strings by Newlines in PowerShell?

Method 2: Using the Contains() Method in a Loop

Here is another method to check if a string contains multiple values in PowerShell. You can use the Contains() method in a loop to check each substring individually.

Here is a complete example.

$string = "The quick brown fox jumps over the lazy dog"
$substrings = @("quick", "lazy", "fox")
$contains = $false

foreach ($substring in $substrings) {
    if ($string.Contains($substring)) {
        $contains = $true
        break
    }
}

if ($contains) {
    Write-Output "The string contains one of the specified values."
} else {
    Write-Output "The string does not contain any of the specified values."
}

Here, we loop through each substring in the $substrings array and use the Contains method to check if it exists in $string. If any substring is found, the loop breaks early for efficiency.

How to Check if a String Contains Multiple Values in PowerShell

Method 3: Using Select-String Cmdlet

The PowerShell Select-String cmdlet is another approach that can search for patterns within strings.

Here is an example.

$string = "The quick brown fox jumps over the lazy dog"
$patterns = "quick|lazy|fox"

if ($string | Select-String -Pattern $patterns) {
    Write-Output "The string contains one of the specified values."
} else {
    Write-Output "The string does not contain any of the specified values."
}

In this example, Select-String is used to search for the patterns within $string. This cmdlet is particularly useful when working with large blocks of text or files.

PowerShell Check if a String Contains Multiple Values

Read Split a String into Variables in PowerShell

Method 4: Using the -contains Operator with Arrays

In PowerShell, you can also use the -contains operator to check if any substring is present in the string by converting the string to an array of substrings.

Here is a complete PowerShell script.

$string = "The quick brown fox jumps over the lazy dog"
$substrings = @("quick", "lazy", "fox")
$stringArray = $string.Split(" ")

if ($substrings | ForEach-Object { $stringArray -contains $_ }) {
    Write-Output "The string contains one of the specified values."
} else {
    Write-Output "The string does not contain any of the specified values."
}

Here, we split the string into an array of words and then check if any of the specified substrings are present in this array using the -contains operator.

Conclusion

In this PowerShell tutorial, I explained how to check if a string contains multiple values using various methods like:

  • Using the -match Operator with Regular Expressions
  • Using the Contains() Method in a Loop
  • Using Select-String Cmdlet
  • Using the -contains Operator with Arrays

You may also like the following PowerShell 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.