Powershell Comparison Operators Like Equal, Greater, Lesser, Contains, Regex

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/powershell-comparison-operators-like-equal-greater-lesser-contains-regex/
PINTEREST
PINTEREST
INSTAGRAM

Comparison operator is used to compare given values and return an boolean value like true and false . These operators are generally used by if , while and similar decision making keywords.

All comparison operators are case-insensitive by default. So the uppercase or lowercase expression do not changes the behaviour of these comparison operators.

Equal

One of the most used comparison operator is equal. This operator check whether given values are equal or not if they are equal a true value is returned, if they are not equal false value is returned. If parameters are variables their type must be comparable. This operator is expressed as -eq which is short version of equal word.

Equal

Equal

As we can see from examples we get always a true or false value according to comparison.

Not Equal

The opposite of the equal operator is not equal which will simply give reverse results. If given values are not equal this operator will return a true boolean result. If given values are equal this will be a false boolean result. We will express this operator with -ne which is short form of not equal

Not Equal

Not Equal

Greater Than

We can use greater than operator to find which value is greater than other. This operator is used with similar data types but if given values can be converted each other different type of data can be used too. If the first value is greater than second value the statement will return boolean true if not returns boolean false

Greater Than

Greater Than

Greater Than or Equal

In previous part we examined the greater than comparison operator. If we want to check the equality with greater than we will use greater than or equal operator. If the first value is greater than or equal to the second value this will return boolean true result if lesser than it will return boolean false result.

Greater Than or Equal

Greater Than or Equal

Less Than

We can compare for the first value lesser than second value. This is the reverse implementation of greater than operator. If the first value is lesser than second value it will return boolean true if not it will return boolean false result. This operator is expressed as -lt

Less Than

Less Than

Less Than or Equal

This operator is similar to less than operator this operator also checks equality. If first value is lesser than or equal to the second value the result is boolean true if not result is boolean false . We will use -le as operator.

Less Than or Equal

Less Than or Equal

Wildcard Comparison

Another useful comparison operator in Powershell is wildcard comparison. This operator compare first value in a wildcard manner. If the second value exists in the first value this will return boolean true result , if not it will return boolean false result. We will express this with -like operator.

LEARN MORE  How To Get Python String Length?

Wildcard Comparison

Wildcard Comparison

Regular Expression Comparison

Regular expression is used to express structured strings. Regular expression is a powerful language. We can use regular expressions in our comparison operations. We will use -match operator to check given regular expression in the value.

Regular Expression Comparison

Regular Expression Comparison

Containment

This operator is used in types like collection, array or hashtable. Containment operator will check given collection, array or hashtable whether second value exists in these values. If exist this will return boolean true if not return boolean false

In this example we have a array named cities and contains city names istanbul , ankara and canakkale . We check this array if it contains given values.

Containment

Containment

RSS
EMAIL
FACEBOOK
FACEBOOK
GOOGLE
GOOGLE
https://www.poftut.com/powershell-comparison-operators-like-equal-greater-lesser-contains-regex/
PINTEREST
PINTEREST
INSTAGRAM

You may also like...

Leave a Reply

Your email address will not be published.

Enjoy this blog? Please spread the word :)