PowerShell comparison operators -eq, -lt, -gt, -contains, -like, -match

If you are used to operators such as > or < or =, you have to do some rethinking. As with batch scripts, PowerShell uses abbreviations of the corresponding English words. -eq Equal -ne Not equal -lt Less than -le Less than or equal -gt Greater than -ge Greater than or equal You don’t need an if statement to test the result of a comparison operation. Without the if statement, the output of the comparison is, simply, TRUE or FALSE.
For example:
“Operator”.length -gt 7
results in TRUE because the word “Operator” has more than seven letters.
Comparison of different data types ^
As with most scripting languages, in PowerShell you can apply comparison operators to different data types. However, this makes sense primarily for numerical values, although the comparison for strings is possible. In the case of strings, only –eq and -ne are useful to determine equality or inequality. Size comparisons of strings are rarely needed.
“Peter” -gt “Paul”
results in TRUE, but not because “Peter” is a longer string than “Paul.” Rather, the operation compares the strings character by character until it detects two different characters. The one with the larger ASCII value determines whether a string is considered to be greater or smaller. In the above example, “Peter” is greater than “Paul” because “e” has a greater ASCII value than “a.”
Danger of confusion with assignment operator
One possible pitfall that might be encountered is the confusion of -eq with “=.” In PowerShell, the equal sign serves as an assignment operator and not as a comparison operator. If you use it incorrectly in an if statement, it may lead to unwanted results if a variable is on the left side of the statement:
if($a = “Peter”){

In such cases, the result of the if condition is always true, the dependent commands will always be executed, and the value of the variable will be destroyed.
Comparing strings ^
Even though you can apply the above operators to strings, other operators should be used for this purpose. Basically, the operators described below perform substring searches and pattern matching.
-like and wildcards ^
You can do a substring search easily with the -like operator by determining whether a string is contained in another string. If the strings are not identical, then you need wildcards:
“PowerShell” -like “Pow*”
This expression results in TRUE; however, without a wildcard, it would be FALSE. If you want to find a string within another string, you have to use wildcards at the beginning and at the end. In the above example, you could search “she” in “PowerShell” with *she*. An alternative to “*” is “?”, which you can use as a placeholder for a single character. The comparison with –like and –eq ignores uppercase and lowercase characters. As with -eq, -like has a counterpart, which is called -notlike.
Search in collections with -contains ^
Another operator that could be mistakenly seen as a substring search operator is -contains. However, its purpose is to decide whether an array contains a certain item:
$w = “December”,”January”,”February”
$w -contains “February”
The result of the comparison is TRUE. However, if you want to use wildcards along the lines of -like, the comparison will fail because wildcards are not supported. Substrings without wildcards result in FALSE as well because an exact match with an item of the collection is required. To check whether a certain array doesn’t contain a certain item, you can use the operator -notcontains.
Find a substring with -match ^
A third operator called -match is useful for substring search and pattern matching with regular expressions. If you go without RegEx, you can easily determine if a string is contained in a second string:
“PowerShell” -match “ower”
results in TRUE. The test doesn’t require wildcards, even though the string you are searching for is contained within another string. The -match operator has a counterpart as well: -notmatch.