How To Check If Array Contains Part Of String In PowerShell?

Do you need to check whether an array contains a part of a string or a substring in PowerShell? In this PowerShell tutorial, I will explain how to check if an array contains part of a string in PowerShell.

To check if an array in PowerShell contains an element that is part of a string, you can use the -match operator with a loop or array filtering. For example, $containsPart = $array -match ‘partOfString’ will return all elements that include ‘partOfString’. If you want to check for any match and get a boolean result, you can use $array -match ‘partOfString’ -ne $null.

Check If Array Contains Part Of String In PowerShell

Now, let us check out different methods of how to check if an array contains part of a string in PowerShell with examples.

Method 1: Using -match Operator

The -match operator in PowerShell is used for pattern matching with regular expressions. It can be used to check if any element in an array matches a particular pattern or contains a substring in PowerShell.

Here is an example:

$array = @('apple', 'banana', 'cherry', 'date')
$substring = 'an'

foreach ($item in $array) {
    if ($item -match $substring) {
        Write-Host "Match found: $item"
    }
}

This script will output ‘banana’ because it contains the substring ‘an’.

I run the PowerShell script using VS code and you can see the output in the screenshot below:

Check If Array Contains Part Of String In PowerShell

Method 2: Using -contains Operator

The -contains operator checks if a collection contains a specified value in PowerShell. However, it’s important to note that -contains checks for an exact match and not a partial match.

Here is the complete script to check if the array contains part of the string in PowerShell using the -contains operator.

$array = @('apple', 'banana', 'cherry', 'date')
$value = 'banana'

if ($array -contains $value) {
    Write-Host "$value is in the array."
}

This script will output ‘banana is in the array.’ because it is an exact match in the array.

Method 3: Custom Function for Partial Matches

Since -contains does not work for partial matches, you can create a custom function that iterates through each element in the PowerShell array and checks if the substring is present.

Here is an example.

function Find-PartialMatch {
    param (
        [string[]]$array,
        [string]$substring
    )
    foreach ($item in $array) {
        if ($item -like "*$substring*") {
            Write-Host "Partial match found: $item"
        }
    }
}

$array = @('apple', 'banana', 'cherry', 'date')
$substring = 'an'
Find-PartialMatch -array $array -substring $substring

This script will output ‘Partial match found: banana’ because ‘banana’ contains the substring ‘an’.

You can see the output in the screenshot below:

powershell check if array contains part of string

Method 4: Using -like Operator with Wildcards

The -like operator allows you to use wildcards to match patterns in strings. The asterisk * wildcard represents any number of characters. This is another best way to check if the array contains part of the string in PowerShell.

Here is the complete PowerShell script.

$array = @('apple', 'banana', 'cherry', 'date')
$substring = '*an*'

foreach ($item in $array) {
    if ($item -like $substring) {
        Write-Host "Match found: $item"
    }
}

This script will output ‘Match found: banana’ because ‘banana’ matches the pattern with the substring ‘an’.

Conclusion

PowerShell provides several methods to check if an array contains a substring. While -match and -like operators can be used for pattern matching, the -contains operator checks for exact matches. For partial matches, you can either use the -like operator with wildcards or create a custom function to iterate through the array and check each element.

In this PowerShell tutorial, I have explained how to check if an array contains part of a string in PowerShell using various methods like:

  • Using -match Operator
  • Using -contains Operator
  • Custom Function for Partial Matches
  • Using -like Operator with Wildcards

You may also like: