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.

Wolfgang Sommergut

Wolfgang Sommergut has over 20 years of experience in IT journalism. He has also worked as a system administrator and as a tech consultant. Today he runs the German publication WindowsPro.de.

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:

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.

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:

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:

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:

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:

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.

Join the 4sysops PowerShell group!

3+
Share
5 Comments
  1. Anonymous 4 years ago

    Thanks for this nice POst!!

    2+

  2. vijay 4 years ago

    thanks for the good post.

     

     

    -1+

  3. Swathi 3 years ago

    this post was really very helpful..thank you.

    0

  4. Vinod Gupta 3 years ago

    Awesome post. Thanks.

    0

  5. Xavi 1 year 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>

    0

Leave a reply

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

*

© 4sysops 2006 - 2019

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