Home » PowerShell » How to Check PowerShell String Contains Multiple Values

How to Check PowerShell String Contains Multiple Values

Use the PowerShell match operator to check if the string contains multiple values inside it or not. Use the string Join method to join the multiple values with OR (|) before we use it with the match operator.

PowerShell match operator is case-insensitive and checks for the string value inside a string and returns True if the string contains the values else returns False.

In this article, we will check how to use the PowerShell match operator to check if the PowerShell string contains multiple values inside it.

Check PowerShell String Contains Multiple Values

Use the string Join method to join the multiple values with | ( or). The PowerShell match operator uses the string value joined by the | operator to check if the given string contains multiple values.

Let’s consider an example, we have a string Copy item from source to destination and multiple values as item, source to find inside the given string.

Use the string Join() method to join multiple values with the | operator. Use the PowerShell match operator to check these multiple values in the given string.

# Declare the string
$str = "Copy item from source to destination" 

# Specify multiple values
$values = @('item','source')

# Use Join to join multiple values with | operator 
$regexValues = [string]::Join('|',$values) 

# Use match operator to check if string contains multiple values
$str -match $regexValues 

The output of the above PowerShell script returns the True values as it checks if either of the multiple values is present inside the string.

The above PowerShell script output is:

Check PowerShell String Contains Multiple Values
Check PowerShell String Contains Multiple Values

Conclusion

I hope the above article on how to check if a PowerShell string contains multiple values inside it using the match operator is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.