How to Compare Two Strings in PowerShell (With Examples)


You can use the following methods in PowerShell to compare two strings:

Method 1: Compare two strings (case-insensitive)

$string1 -eq $string2

This example checks if two strings are equal, regardless of the case.

Method 2: Compare two strings (case-sensitive)

$string1 -ceq $string2 

This example checks if two strings are equal, including the case.

Method 3: Compare two arrays of strings

($array_of_strings1 -join '') -eq ($array_of_strings2 -join '')

This particular example checks if two arrays contain all of the same strings.

The following examples shows how to use each of these methods in practice.

Example 1: Compare Two Strings (Case-Insensitive)

We can use the following syntax to define two string variables, string1 and string2, and then use the –eq operator to check if the two strings are equal, regardless of the case:

$string1 = "hello there everyone"
$string2 = "HELLO there everyone"

$string1 -eq $string2

The following screenshot shows how to use this syntax in practice:

PowerShell compare two strings case-insensitive

The output displays a value of True, which indicates that the two strings are equal, regardless of whether or not they have the same case.

Example 2: Compare Two Strings (Case-Sensitive)

We can use the following syntax to define two string variables, string1 and string2, and then use the –ceq operator to check if the two strings are equal, including the case:

$string1 = "hello there everyone"
$string2 = "HELLO there everyone"

$string1 -ceq $string2

The following screenshot shows how to use this syntax in practice:

PowerShell compare two strings case-sensitive

The output displays a value of False, which indicates that the two strings are not equal including the case.

We can see that the two strings have all of the same characters but they don’t have the same case (since “HELLO” is uppercase in the second string) so the -ceq operator returns False.

Example 3: Compare Two Arrays of Strings

We can use the following syntax to define two arrays of strings, array_of_strings1 and array_of_strings2, and then use the -eq operator to check if both arrays contain all of the same strings:

$array_of_strings1 = @("one", "two", "three")
$array_of_strings2 = @("one", "two")

($array_of_strings1 -join '') -eq ($array_of_strings2 -join '')

The following screenshot shows how to use this syntax in practice:

PowerShell compare two arrays of strings

The output displays a value of False, which indicates that the two arrays do not contain all of the same strings.

Note that we used the -join operator with no space to concatenate all of the strings in each array together into one string before comparing them.

Thus, in this example we used the -eq operator to check if the string onetwothree was equal to onetwo.

Since these two strings are not equal, we received a value of False.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

How to Compare Two Files in PowerShell
How to Compare Two Arrays in PowerShell
How to Compare Dates in PowerShell

Leave a Reply