Home » PowerShell » PowerShell Compare Two Strings

PowerShell Compare Two Strings

Comparing two strings in PowerShell can be easily done using different ways like using the equal() method, -eq operator, or PowerShell -like operator to compare two strings.

A powerShell string is a sequence of characters that represents the text and contains letters, symbols, numbers, and special characters. In PowerShell, comparing two strings is required when dealing with validation, data comparison, and data manipulation.

In this article, we will discuss how to compare two strings in PowerShell using a variety of ways available.

PowerShell String Equals method to Compare Strings Content

The PowerShell string Equals() method checks if two strings are equal. This method is case-sensitive and returns the value True if two strings are equal else it will return False.

$str2 = "ShellGeek Tutorials"                                                              $str3 = "shellgeek tutorials"                                                               $str4 = "ShellGeek Tutorials"                                                                                                                                                          

# Check if two strings are equal
$str2.Equals($str4)             

# Check if strings equal                                                                                                                                                 $str2.Equals($str3)                                                                              

In the above PowerShell script, the $str2 variable contains "ShellGeek Tutorials" , $str3 variable contains content "shellgeek tutorials" and $str4 variable contains "ShellGeek Tutorials".

$str2.Equals($str4) string comparison in PowerShell results in True both variables’ string values are equal.

$str2.Equals($str3) string comparison of two strings in PowerShell results in False as the PowerShell string equal() method uses case-sensitive to compare two strings.

The output of the above PowerShell script using the string Equal() method for comparison is:

PowerShell String Equal method to Compare Strings
PowerShell String Equal method to Compare Strings

PowerShell Compare Two Strings using -eq Operator

The equality operator -eq in PowerShell checks, if the two strings’ content is equal. It returns True when both string content match else returns False.

The -eq operator is case-insensitive while comparing two strings in PowerShell.

$str2 = "ShellGeek Tutorials"
$str3 = "shellgeek tutorials"  
$str4 = "ShellGeek" 

In the above PowerShell script, $str2, $str3 and $str4 contain the string data.

$str2 -eq $str3 

Use the -eq operator in PowerShell to compare two strings $str2 and $str3

The above PowerShell script compares two strings’ content using the -eq operator.

Output:

True

Compare two strings $str3 and $str4 contents using the PowerShell -eq operator.

$str2 -eq $str4 

Output:

False

To use a case-sensitive equality check while comparing two strings’ content, use the -ceq operator in PowerShell.

$str2 -ceq $str3 

In the above PowerShell script, the -ceq operator checks case-sensitive equality while comparing string contents.

Output:

False

PowerShell -Like Operator to Compare Two Strings

PowerShell -like operator checks two string content if strings are equal or not. It returns the boolean value True if two strings are equal else False if they are not matched with each other.

$str2 = "ShellGeek Tutorials"
$str3 = "shellgeek tutorials"  
$str4 = "ShellGeek" 

In the above PowerShell script, $str2, $str3 and $str4 contain the string data.

Use the -like operator in PowerShell to compare two strings $str2 and $str3

$str2 -like $str3

The above PowerShell script compares two strings’ content using the -like operator.

Output:

True

Compare two strings $str3 and $str4 contents using the PowerShell -like operator.

$str2 -like $str4

Output

False

To use case-sensitive equality check while comparing two strings’ content, use the -clike operator in PowerShell.

$str2 -clike $str3 

In the above PowerShell script, the -clike operator checks case-sensitive equality while comparing string contents.

Output:

False

Cool Tip: How to merge multiple text files into one in PowerShell!

Conclusion

I hope the above article on how to compare two strings in PowerShell using the -eq, -like operator, and PowerShell string equals() method is helpful to you.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.