How to Check if an Array Contains a String Case-Insensitive in PowerShell?

One common task when working with arrays in PowerShell is checking if an array contains a specific string. In PowerShell, string comparison can be done either case-sensitively or case-insensitively. In this PowerShell tutorial, I will explain how to check if an array contains a string case-insensitive in PowerShell.

To check if an array contains a string without considering case in PowerShell, use the -inotcontains operator, which performs a case-insensitive comparison. For example, $containsString = $array -inotcontains ‘string’ will return $true if the string ‘string’ is found in the array $array, regardless of case. Alternatively, you can use -ccontains for a case-sensitive check.

Check if an Array Contains a String Case-Insensitive in PowerShell

A case-sensitive check takes into account the letter case (uppercase or lowercase) when comparing strings. Conversely, a case-insensitive check disregards the case and treats ‘PowerShell’ the same as ‘powershell’ or ‘POWERSHELL’.

PowerShell provides different operators and methods to perform string comparison. Some are inherently case-insensitive, while others are case-sensitive by default but can be modified to perform case-insensitive comparisons.

Using the -contains Operator

PowerShell has a built-in operator called -contains that checks if a collection, such as an array, includes a specific element. The -contains operator is case-insensitive by default.

Here’s a simple example:

$array = 'Apple', 'Banana', 'Cherry'
$stringToCheck = 'apple'

if ($array -contains $stringToCheck) {
    Write-Host "The array contains the string."
} else {
    Write-Host "The array does not contain the string."
}

In this example, even though ‘Apple’ in the array has a capital ‘A’ and the $stringToCheck variable is all lowercase, the -contains operator will confirm that ‘apple’ is in the array.

You can check out the screenshot below:

Check if an Array Contains a String Case-Insensitive in PowerShell

Using .Contains() Method

Another way to check for the presence of a string in an array is to use the .Contains() method in PowerShell. However, unlike the -contains operator, the .Contains() method is case-sensitive. To make it case-insensitive, both the array elements and the string to check must be converted to the same case using the .ToLower() or .ToUpper() methods before comparison.

Here’s how you can use the .Contains() method in a case-insensitive manner:

$array = 'Apple', 'Banana', 'Cherry'
$stringToCheck = 'apple'

# Convert both to the same case
$lowerCaseArray = $array.ToLower()
$lowerCaseStringToCheck = $stringToCheck.ToLower()

if ($lowerCaseArray.Contains($lowerCaseStringToCheck)) {
    Write-Host "The array contains the string."
} else {
    Write-Host "The array does not contain the string."
}

Once you run the PowerShell script using vs. code or Windows PowerShell ISE, you can see the output in the screenshot below:

How to Check if an Array Contains a String Case-Insensitive in PowerShell

Using Where-Object Cmdlet

The Where-Object cmdlet in PowerShell allows you to filter objects based on their properties or script blocks. You can use this cmdlet to perform a case-insensitive search by using a script block that compares the array elements to the desired string, converting both to the same case.

Here’s an example using Where-Object:

$array = 'Apple', 'Banana', 'Cherry'
$stringToCheck = 'apple'

$result = $array | Where-Object { $_.ToLower() -eq $stringToCheck.ToLower() }

if ($result) {
    Write-Host "The array contains the string."
} else {
    Write-Host "The array does not contain the string."
}

Using -match Operator

The -match operator uses regex (regular expressions) to compare strings and is case-insensitive by default in PowerShell. This operator can be useful when you need to check for patterns rather than exact strings.

Here’s an example using -match:

$array = 'Apple', 'Banana', 'Cherry'
$stringToCheck = 'apple'

$result = $array -match $stringToCheck

if ($result) {
    Write-Host "The array contains the string."
} else {
    Write-Host "The array does not contain the string."
}

Conclusion

Checking if an array contains a specific string is a common task in PowerShell scripting. PowerShell provides multiple ways to perform this check, with case-insensitivity being the default behavior for some operators and methods, like -contains and -match. For methods that are case-sensitive by default, such as .Contains(), you can easily perform a case-insensitive comparison by converting the strings to the same case before checking.

In this PowerShell tutorial, I will explain how to check if an array contains a string case-insensitive in PowerShell using the below methods:

  • Using the -contains Operator
  • Using .Contains() Method
  • Using Where-Object Cmdlet
  • Using -match Operator

You may also like: