Case Insensitive Strings Comparison in PowerShell

This tutorial is about case-insensitive string comparison in PowerShell. This is a very common requirement when working with strings. I will show you how to do case-insensitive string comparisons in PowerShell using various methods.

To compare strings case-insensitively in PowerShell, you can use the -ieq operator, which explicitly specifies a case-insensitive comparison. For example:

$state1 = "California"
$state2 = "california"

if ($state1 -ieq $state2) {
    Write-Output "The states are the same."
} else {
    Write-Output "The states are different."

In this case, the output will be “The states are the same.” because -ieq performs a case-insensitive comparison.

Here are 5 different methods and examples for “PowerShell compare strings case insensitive”.

1. Using the -eq Operator

By default, the -eq operator in PowerShell performs a case-insensitive comparison. This makes it easy to compare two strings without worrying about their letter case.

Here is a complete script.

$city1 = "New York"
$city2 = "new york"

if ($city1 -eq $city2) {
    Write-Output "The cities are the same."
} else {
    Write-Output "The cities are different."
}

In this example, the output will be “The cities are the same.” because the -eq operator ignores case by default.

I executed the script above; you can see it gave the exact output below.

powershell compare strings case insensitive

Read Add Double Quotes in a String in PowerShell

2. Using the -ieq Operator

The -ieq operator explicitly specifies a case-insensitive comparison. It behaves the same as the -eq operator but makes it clear in your script that the comparison is intended to be case-insensitive.

Here is an example.

$state1 = "California"
$state2 = "california"

if ($state1 -ieq $state2) {
    Write-Output "The states are the same."
} else {
    Write-Output "The states are different."
}

The output will be “The states are the same.” due to the case-insensitive comparison.

After I execute the PowerShell script using VS code, you can see the screenshot below.

Case Insensitive Strings Comparison in PowerShell

3. Using the -match Operator

The -match operator in PowerShell is used for regular expression comparisons and is case-insensitive by default. This can be useful when you need to perform pattern matching.

Here is an example.

$address = "123 Main St, San Francisco, CA"
$pattern = "san francisco"

if ($address -match $pattern) {
    Write-Output "The address contains San Francisco."
} else {
    Write-Output "The address does not contain San Francisco."
}

In this example, the output will be “The address contains San Francisco.” because -match ignores case.

Read How to Trim Strings in PowerShell?

4. Using the -contains Operator

The PowerShell -contains operator checks if a collection contains a specific value and is case-insensitive by default.

Here is an example.

$cities = @("Los Angeles", "Chicago", "Houston")
$searchCity = "chicago"

if ($cities -contains $searchCity) {
    Write-Output "The city is in the list."
} else {
    Write-Output "The city is not in the list."
}

Here, the output will be “The city is in the list.” because -contains performs a case-insensitive check.

5. Using the .ToLower() Method

This is another useful method for case insensitive strings comparison in PowerShell.

You can convert both strings to lowercase (or uppercase) and then compare them. This method ensures that the comparison is case-insensitive in PowerShell.

Here is an example of how to use the .ToLower() method to convert both strings into lower case and then compare.

$president1 = "Barack Obama"
$president2 = "barack obama"

if ($president1.ToLower() -eq $president2.ToLower()) {
    Write-Output "The presidents are the same."
} else {
    Write-Output "The presidents are different."
}

In this case, the output will be “The presidents are the same.” because both strings are converted to lowercase before comparison.

Look at the screenshot below. I executed the above script, which shows the exact required output.

powershell string comparison case insensitive

Conclusion

PowerShell provides several ways to perform case-insensitive string comparisons. Like you, use PowerShell operators like -eq-ieq-match, or methods like .ToLower(), which ensures that your string comparisons are accurate and case-insensitive.

I hope this helps; please comment below if you still have any questions.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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