Comparison Operators

The PowerShell comparison operators allow you to compare expressions against each other. By default, PowerShell’s comparison operators are case insensitive. For all operators where case sensitivity applies, the –i prefix makes this case insensitivity explicit, while the –c prefix performs a case-sensitive comparison.

Windows PowerShell comparison operators

Operator

Meaning

-eq

The equality operator:

$leftValue –eq $rightValue

For all primitive types, returns $true if $leftValue and $rightValue are equal.

When used with arrays, returns all elements in $leftValue that are equal to $rightValue.

When used with any other type, PowerShell uses that type’s .Equals() method if it implements one.

-ne

The negated equality operator:

$leftValue –ne $rightValue

For all primitive types, returns $true if $leftValue and $rightValue are not equal.

When used with arrays, returns all elements in $leftValue that are not equal to $rightValue.

When used with any other type, PowerShell returns the negation of that type’s .Equals() method if it implements one.

-ge

The greater-than-or-equal operator:

$leftValue –ge $rightValue

For all primitive types, returns $true if $leftValue is greater than or equal to $rightValue.

When used with arrays, returns all elements in $leftValue that are greater than or equal to $rightValue.

When used with any other type, PowerShell returns the result of that object’s .Compare() method if it implements one. If the method returns a number greater than or equal to zero, the operator returns $true.

-gt

The greater-than operator:

$leftValue –gt $rightValue

For all primitive types, returns $true if $leftValue is greater than $rightValue.

When used with arrays, returns all elements in $leftValue that are greater than $rightValue.

When used with any other type, PowerShell returns the result of that object’s .Compare() method if it implements one. If the method returns a number greater than zero, the operator returns $true.

-lt

The less-than operator:

$leftValue –lt $rightValue

For all primitive types, returns $true if $leftValue is less than $rightValue.

When used with arrays, returns all elements in $leftValue that are less than $rightValue.

When used with any other type, PowerShell returns the result of that object’s .Compare() method if it implements one. If the method returns a number less than zero, the operator returns $true.

-le

The less-than-or-equal operator:

$leftValue –le $rightValue

For all primitive types, returns $true if $leftValue is less than or equal to $rightValue.

When used with arrays, returns all elements in $leftValue that are less than or equal to $rightValue.

When used with any other type, PowerShell returns the result of that object’s .Compare() method if it implements one. If the method returns a number less than or equal to zero, the operator returns $true.

-like

The like (simple text matching) operator.

$leftValue –like <Pattern>

Evaluates the pattern against the target, returning $true if the match is successful.

The like operator supports the following simple wildcard characters:

?        Any single unspecified character
*        Zero or more unspecified characters
[a-b]    Any character in the range of a-b
[ab]     The specified characters a or b

For example:

PS >"Test" -like "[A-Z]e?[tr]
"True

-notlike

The negation of the like operator. Returns $true when the –like operator would return $false.

-match

The match operator.

Target” –match <Regular Expression>

Evaluates the regular expression against the target, returning $true if the match is successful. Once complete, PowerShell places the successful matches into the $matches variable.

The $matches variable is a hashtable that maps the individual matches to the text they match. 0 is the entire text of the match, 1 and on contain the text from any unnamed captures in the regular expression, and string values contain the text from any named captures in the regular expression.

For example:

PS >"Hello World" -match "(.*) (.*)"
True
PS >$matches[1]
Hello

For more information on the details of regular expressions, see the “Regular Expression Reference” appendix.

-notmatch

The negation of the –match operator. Returns $true when the –match operator would return $false.

The –notmatch operator still populates the $matches variable with the results of match.

-contains

The contains operator.

$list –contains $value

Returns $true if the list specified by $list contains the value $value.

-notcontains

The negation of the contains operator. Returns $true when the –contains operator would return $false.

-is

The type operator.

$list –is [type]

Returns $true if $value is of the specified .NET type.

-isnot

The negation of the type operator.

Returns $true when the –type operator would return $false.

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

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