For conditional statements or loops, you have to compare values to control the progress of the script. Like all modern scripting languages, PowerShell supports if/else, switch, and the corresponding comparison operators. However, PowerShell differs here from other popular programming languages for Windows and the Web.
Avatar

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.

-eqEqual
-neNot equal
-ltLess than
-leLess than or equal
-gtGreater than
-geGreater 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:

Subscribe to 4sysops newsletter!

"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.

10 Comments
  1. Avatar
    Anonymous 8 years ago

    Thanks for this nice POst!!

  2. Avatar
    vijay 8 years ago

    thanks for the good post.

     

     

  3. Avatar
    Swathi 8 years ago

    this post was really very helpful..thank you.

  4. Avatar
    Vinod Gupta 7 years ago

    Awesome post. Thanks.

  5. Avatar
    Xavi 5 years ago

    Hi,

    I have a file where the tags are being like below. I am able to pick the value for just rule tag. but i am not able to do for string numeric. Can someone please help?

    <rule0>value<rule0>

    <rule1>value<rule1>

  6. Avatar
    Hemalatha 4 years ago

    Hi, I want powershell command to compare the contents from Image file with text file and export the mismatch contents in the excel. Can anyone help me out from this

    • Avatar
      Leos Marek (Rank 4) 4 years ago

      Hello, what do you mean by content from Image? You mean the binary data which are in the file?

  7. Avatar
    Christian B McGhee 2 years ago

    Your example “PowerShell” -match “ower” does NOT result in TRUE. Rather it results in FALSE. The -match operator is looking for an exact match. Not a partial match as your example would suggest

    • Avatar Author

      Not need to discuss this. Try it out and you will see that the result is TRUE.

      • Avatar
        Christian B McGhee 2 years ago

        I had the strings reversed you are correct

Leave a reply to Xavi Click here to cancel the reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account