How to Compare Strings in PowerShell?

If you are working with strings in PowerShell, you might get requirements to compare strings in PowerShell. In this PowerShell tutorial, I will explain how to compare strings in PowerShell using various methods with examples.

To compare strings in PowerShell, you can use the -eq operator for case-insensitive equality checks or -ceq for case-sensitive comparisons. The .Equals() method provides flexibility with options like OrdinalIgnoreCase for case-insensitivity. For pattern matching, use the -like operator with wildcards or -match for regular expressions. Additionally, Compare-Object can compare two sets of strings to highlight differences.

Compare Strings in PowerShell

Here are the multiple methods you can use to compare strings in PowerShell.

1. Using the -eq Operator

The PowerShell -eq operator is used to check if two strings are exactly equal. By default, this comparison is case-insensitive.

Example:

$string1 = "NewYork"
$string2 = "newyork"

if ($string1 -eq $string2) {
    Write-Output "The strings are equal."
} else {
    Write-Output "The strings are not equal."
}

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

The screenshot below shows the output after I executed the above PowerShell script using VS code.

Compare Strings in PowerShell

For a case-sensitive comparison, you can use the -ceq operator in PowerShell, like the below example.

$string1 = "NewYork"
$string2 = "newyork"

if ($string1 -ceq $string2) {
    Write-Output "The strings are equal."
} else {
    Write-Output "The strings are not equal."
}

Here, the output will be “The strings are not equal.” because -ceq considers the case of the strings.

Here you can see the output in the screenshot below:

How to Compare Strings in PowerShell

Read Convert Variables to Strings in PowerShell

2. Using the -ne Operator

The -ne operator checks if two strings are not equal in PowerShell. This is also case-insensitive by default.

Example:

$string1 = "JaneDoe"
$string2 = "JaneSmith"

if ($string1 -ne $string2) {
    Write-Output "The strings are not equal."
} else {
    Write-Output "The strings are equal."
}

The output will be “The strings are not equal.” because JaneDoe is different from JaneSmith.

I executed the above code, and you can see the output in the screenshot below:

powershell compare strings

3. Using the .Equals() Method

Here is another method in PowerShell to compare strings, .Equals() method. The .Equals() method allows you to compare strings by specifying case sensitivity.

Example:

$string1 = "MichaelSmith"
$string2 = "michaelsmith"

if ($string1.Equals($string2, [System.StringComparison]::OrdinalIgnoreCase)) {
    Write-Output "The strings are equal."
} else {
    Write-Output "The strings are not equal."
}

In this example, the output will be “The strings are equal.” because OrdinalIgnoreCase makes the comparison case-insensitive.

For a case-sensitive comparison, you can use Ordinal:

if ($string1.Equals($string2, [System.StringComparison]::Ordinal)) {
    Write-Output "The strings are equal."
} else {
    Write-Output "The strings are not equal."
}

Here, the output will be “The strings are not equal.” because Ordinal enforces case sensitivity.

Read Split a String by Word in PowerShell

4. Using the Compare-Object Cmdlet

The Compare-Object cmdlet is useful when comparing two sets of strings and finding differences in PowerShell.

Example:

$array1 = @("AliceJohnson", "BobSmith", "CharlieBrown")
$array2 = @("AliceJohnson", "CharlieBrown", "DavidWilliams")

$comparison = Compare-Object -ReferenceObject $array1 -DifferenceObject $array2

foreach ($difference in $comparison) {
    Write-Output $difference
}

The output will show the differences between the two arrays:

InputObject SideIndicator
----------- -------------
BobSmith    <=
DavidWilliams =>

5. Using the -like and -match Operators

You can also use the -like and -match operators to compare strings in PowerShell.

The -like operator is used for wildcard pattern matching, while the -match operator uses regular expressions.

Example with -like:

$string = "JessicaJones"

if ($string -like "Jessica*") {
    Write-Output "The string starts with 'Jessica'."
}

The output will be “The string starts with ‘Jessica’.” because -like supports wildcard patterns.

Example with -match:

$string = "JessicaJones"

if ($string -match "^Jessica") {
    Write-Output "The string starts with 'Jessica'."
}

The output will be the same as the previous example, but -match uses a regular expression to perform the comparison.

Conclusion

Comparing strings in PowerShell can be done in several ways, like equality checks with -eq, or using .Equals(), or using advanced pattern matching with -like and -match.

I hope now you understand very well, how to compare strings in PowerShell using various methods.

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.