Comparison operators 

Comparison operators are needed to create expressions and conditions. Comparison operators are always between two objects, one on the left and one on the right. All of them return a Boolean value, either $true or $false, unless you don't compare a list of objects. In the latter scenario most operators would return the matching objects. For all of the comparison operations, there are additional keywords available to work case-sensitive (leading c character) or explicitly case-insensitive (leading i character). Case-sensitivity is a feature to not ignore upper and lower cases between two variables. Default operators work case-insensitive and are marked in bold. This gives you simple guidance. For the first steps, you only need to know about the operators in bold, because you will very rarely need to use the other ones:

Operator
Meaning
Description
 -eq

Equals

Returns equality between left and right value.

 -ieq

Equals—explicitly case-insensitive

Returns equality - explicitly case-insensitive.

 -ceq

Equals—case-sensitive

Returns equality - case-sensitive.

 -ne

Not equal

Returns equality - negated.

 -ine

Not equal—explicitly case-insensitive

Returns equality - negated - explicitly case-insensitive.

 -cne

Not equal—case-sensitive

Returns equality - negated - case-sensitive.

 -gt

Greather-than

Returns if left value is greater than right one.

 -igt

Greather-than - explicitly case-insensitive

Returns if left value is greater than right one - explicitly case-insensitive.

 -cgt

Greather-than - case-sensitive

Returns if left value is greater than right one - case-sensitive.

 -ge   

Greater-than or equal to

Returns if left value is greater than or equal to right one.

 -ige

Greater-than or equal to - explicitly case-insensitive

Returns if left value is greater than or equal to right one - explicitly case-insensitive.

 -cge

Greater-than or equal to - case-sensitive

Returns if left value is greater than or equal to right one - case-sensitive.

 -lt

Less-than

Returns if left value is less than right one.

 -ilt

Less-than - explicitly case-insensitive

Returns if left value is less than right one - explicitly case-insensitive.

 -clt

Less-than - case-sensitive

Returns if left value is less than right one - case-sensitive.

 -le

Less-than or equal to

Returns if left value is less than or equal to right one.

 -ile

Less-than or equal to - explicitly case-insensitive

Returns if left value is less than or equal to right one - explicitly case-insensitive.

 -cle

Less-than or equal to - case-sensitive

Returns if left value is less than or equal to right one - case-sensitive.

 -like

Matching a defined filter

Returns if left value is similar to right one, using a filter/wildcard.

 -ilike

Matching a defined filter - explicitly case-insensitive

Returns if left value is similar to right one, using a filter/wildcard - explicitly case-insensitive.

 -clike

Matching a defined filter - case-sensitive

Returns if left value is similar to right one, using a filter/wildcard - case-sensitive.

 -notlike

Not matching a defined filter

Returns if left value is not similar to right one, using a filter/wildcard.

 -inotlike

Not matching a defined filter - explicitly case-insensitive

Returns if left value is not similar to right one, using a filter/wildcard - explicitly case-insensitive.

 -cnotlike

Not matching a defined filter - case-sensitive

Returns if left value is not similar to right one, using a filter/wildcard - case-sensitive.

 -match

Matching a defined RegEx filter

Returns if left value is similar to right one, using RegEx.

 -imatch

Matching a defined RegEx filter - explicitly case-insensitive

Returns if left value is similar to right one, using RegEx - explicitly case-insensitive.

 -cmatch

Matching a defined RegEx filter - case-sensitive

Returns if left value is similar to right one, using RegEx - case-sensitive.

 -notmatch

Not matching a defined RegEx filter

Returns if left value is not similar to right one, using RegEx.

 -inotmatch

Not matching a defined RegEx filter - explicitly case-insensitive

Returns if left value is not similar to right one, using RegEx - explicitly case-insensitive.

 -cnotmatch

Not matching a defined RegEx filter - case-sensitive

Returns if left value is not similar to right one, using RegEx - case-sensitive.

 -contains

Collection containing an object

Returns if left collection contains one object, same as the right one.

 -icontains

Collection containing an object - explicitly case-insensitive

Returns if left collection contains one object, same as the right one - explicitly case-insensitive.

 -ccontains

Collection containing an object - case-sensitive

Returns if left collection contains one object, same as the right one - case-sensitive.

 -notcontains

Collection not containing an object

Returns if left collection does not contain one object, same as the right one.

 -icontains

Collection not containing an object - explicitly case-insensitive

Returns if left collection does not contain one object, same as the right one - explicitly case-insensitive.

 -cnotcontains

Collection not containing an object - case-sensitive

Returns if left collection does not contain one object, same as the right one - case-sensitive.

 -in

Value is in a collection of reference values

Returns if left object is included in collection on the right.

 -iin

Value is in a collection of reference values - explicitly case-insensitive

Returns if left object is included in collection on the right - explicitly case-insensitive.

 -cin

Value is in a collection of reference values - case-sensitive

Returns if left object is included in collection on the right - case-sensitive.

 -notin

Value is not in a collection of reference values

Returns if left object is not included in collection on the right

 -inotin

Value is not in a collection of reference values - explicitly case-insensitive

Returns if left object is not included in collection on the right - explicitly case-insensitive.

 -cnotin

If value is not in a collection of reference values - case-sensitive

Returns if left object is not included in collection on the right - case-sensitive.

 

Some examples are as follows:

# Initialization of variables
$numericValue = 42
$stringValue = 'Password.'
$hashtable = @('This', 'is', 'a', 'test', '!', 42)

# -eq
$numericValue -eq 42 # true
$numericValue -eq 24 # false

# -ne
$stringValue -ne 42 # true
$hashtable[5] -ne 42 # false

# -gt
$numericValue -gt 41 # false
$stringValue.Length -gt 5 # true
$hashtable.Count -gt 6 #false

# -ge
$numericValue -ge 41 # false
$stringValue.Length -ge 5 # true
$hashtable.Count -ge 6 #true

# -lt
$numericValue -lt 41 # true
$stringValue.Length -lt 5 # false
$hashtable.Count -lt 6 # false

# -le
$numericValue -le 41 # true
$stringValue.Length -le 5 # false
$hashtable.Count -le 6 # true

# -like
$stringValue -like '*Password.*' # true
$stringValue -like '42' # false

# -notlike
$stringValue -notlike '*Password.*' # false
$stringValue -notlike '42' # true

# -match
$stringValue -match 'Pass' # true
$Matches # Name = 0; Value = Pass
$hashtable -match 'is' # 'This', 'is'

# -notmatch
$hashtable -notmatch 'is' # 'a', 'test', !, 42

# -contains
$hashtable -contains 42 # true
$stringValue -contains 'Pass' # false
# contains validates collections and not strings - this is a typical error

# -notcontains
$hashtable -notcontains 4 # true
# not an exact match

# -in
42 -in $hashtable # true
'Pass' -in $stringValue # false
# in validates collections and not strings - this is a typical error
4 -in $hashtable # false
# not an exact match

# -notin
4 -notin $hashtable # true
# not an exact match, but negated
'is' -notin $hashtable # false
# an exact match - negated

As you can see in the example, most of the operators are straightforward. Just make sure that you understand that -in and -contains are dedicated to collections, and -match works with RegEx (Regular Expressions—a syntax to find and replace strings). In addition, -match will write its findings into the $Matches variable, which you can call after you have found matches.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset