As a PowerShell developer, you should know everything about how to perform both case-sensitive and case-insensitive string comparisons in PowerShell. In this tutorial, I will guide you through the various methods available in PowerShell for string comparison with complete code and examples.
In PowerShell, string comparisons can be performed both case-insensitively and case-sensitively. By default, operators like -eq and -like are case-insensitive, meaning they treat uppercase and lowercase letters as equivalent. For case-sensitive comparisons, you can use operators such as -ceq and -clike. Additionally, the .Equals() method allows for explicit case sensitivity settings, providing flexibility for various comparison needs.
String Comparisons in PowerShell
In PowerShell, string comparison can be performed using various operators and methods. By default, PowerShell is case-insensitive, meaning it treats uppercase and lowercase letters as equivalent. However, there are scenarios where case-sensitive comparisons are necessary. Let’s explore both case-insensitive and case-sensitive comparisons in detail.
Case-Insensitive String Comparison in PowerShell
Let me show you how to perform case-insensitive string comparisons in PowerShell.
1. Using -eq and -ne Operators
The -eq (equals) and -ne (not equals) operators are used for equality comparisons in PowerShell. By default, these operators are case-insensitive.
Let me show you an example to help you understand it better.
$string1 = "Hello"
$string2 = "hello"
if ($string1 -eq $string2) {
Write-Output "The strings are equal (case-insensitive)."
} else {
Write-Output "The strings are not equal."
}In this example, the output will be:
The strings are equal (case-insensitive).I executed the above PowerShell script, and you can see the output in the screenshot below:

Check out How to Convert String to Camel Case in PowerShell?
2. Using -like and -notlike Operators
The -like and -notlike operators are used for pattern matching with wildcards. These operators are also case-insensitive by default. Here is the complete example with the complete script.
$string1 = "Hello"
$string2 = "h*o"
if ($string1 -like $string2) {
Write-Output "The string matches the pattern (case-insensitive)."
} else {
Write-Output "The string does not match the pattern."
}The output will be:
The string matches the pattern (case-insensitive).Here is the output in the screenshot below after I executed the above PowerShell script.

Check out PowerShell If-Else String Comparison
Case-Sensitive String Comparison in PowerShell
Now, let me show you how to make case-sensitive string comparisons in PowerShell.
1. Using -ceq and -cne Operators
To perform case-sensitive comparisons in PowerShell, you can use the -ceq (case-sensitive equals) and -cne (case-sensitive not equals) operators.
Here is an example to help you understand it better.
$string1 = "Hello"
$string2 = "hello"
if ($string1 -ceq $string2) {
Write-Output "The strings are equal (case-sensitive)."
} else {
Write-Output "The strings are not equal."
}In this example, the output will be:
The strings are not equal.You may check out the outputs in the screenshot below after I executed the PowerShell script.

2. Using -clike and -cnotlike Operators
Similarly, for case-sensitive pattern matching, you can use the -clike and -cnotlike operators in PowerShell. Here is an example to help you understand it better.
$string1 = "Hello"
$string2 = "h*o"
if ($string1 -clike $string2) {
Write-Output "The string matches the pattern (case-sensitive)."
} else {
Write-Output "The string does not match the pattern."
}The output will be:
The string does not match the pattern.You can see the output in the screenshot below:

Read PowerShell Comparison Operators
Using Methods for String Comparison in PowerShell
Now, let me show you how to use methods for string comparison in PowerShell.
1. .Equals() Method
The .Equals() method in PowerShell can also be used for string comparison. It offers an option to specify case sensitivity.
Here is an example.
$string1 = "Hello"
$string2 = "hello"
if ($string1.Equals($string2, [System.StringComparison]::OrdinalIgnoreCase)) {
Write-Output "The strings are equal (case-insensitive using Equals method)."
} else {
Write-Output "The strings are not equal."
}
if ($string1.Equals($string2, [System.StringComparison]::Ordinal)) {
Write-Output "The strings are equal (case-sensitive using Equals method)."
} else {
Write-Output "The strings are not equal."
}In this example, the first comparison will output:
The strings are equal (case-insensitive using Equals method).The second comparison will output:
The strings are not equal.Here is the exact output in the screenshot below you can see:

Read How to Remove Newline from String in PowerShell?
2..Contains() Method
The .Contains() method in PowerShell is inherently case-sensitive. To perform a case-insensitive search, you must convert both strings to the same case.
Let me show you an example.
$string1 = "Hello"
$string2 = "he"
# Case-sensitive
if ($string1.Contains($string2)) {
Write-Output "The string contains the substring (case-sensitive)."
} else {
Write-Output "The string does not contain the substring."
}
# Case-insensitive
if ($string1.ToLower().Contains($string2.ToLower())) {
Write-Output "The string contains the substring (case-insensitive)."
} else {
Write-Output "The string does not contain the substring."
}The output will be:
The string does not contain the substring.
The string contains the substring (case-insensitive).Conclusion
In this tutorial, I have explained how to perform both case-sensitive and case-insensitive string comparisons in PowerShell. I am sure these examples will help you understand the difference between case-sensitive and case-insensitive string comparisons in PowerShell.
You may also like the following tutorials:
- How to Find Lines Starting with a Specific String in PowerShell?
- PowerShell -contains Operator
- How to Count the Number of Characters in a String in PowerShell?
- How to Replace Multiple Strings in a File Using PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.