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.

(10 -eq 10) (10 -eq 11) (10 -eq "10") (10 -eq "a")

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`

(10 -ne 10) (10 -ne 11) (10 -ne "10") (10 -ne "a")

# 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`

(10 -gt 5) (10 -gt "5") (10 -gt "a") (10 -gt 15)

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

(10 -ge 5) (10 -ge 10) (10 -ge 11)

# 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`

(10 -lt 5) (10 -lt "5") (10 -lt 15)

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

(10 -le 10) (10 -le 15) (10 -le 5)

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

("ismail" -like "*mail") ("ismail" -like "*m*") ("ismail" -like "*mi*")

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

("ismail" -match "[aZ]") ("1" -match "[az]")

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

$cities="ankara","istanbul","canakkale" $cities -contains "canakkale" $cities -contains "canakkal"