spot_img

Comparison Operators

The comparison operators are used in PowerShell to compare the values. By default, all comparison operators are case-sensitive. These operators help us to find, test, compare, modify, and replace the data and information.

PowerShell supports the following comparison operators:

PowerShell Comparison Operators

  1. Equality Operators
  2. Matching Operators
  3. Containment Operators
  4. Replacement Operators
  5. Type Operators

Equality Operators

The equality operators are those operators, which check the equality of two values and also check that one value is greater or less than other value.

The following are the list of equality comparison operators:

  1. -eq (Equal)
  2. -ne (Not Equal)
  3. -gt (Greater than)
  4. -ge (Greater than or Equal to)
  5. -lt (Less than)
  6. -le (Less than or Equal to)

-eq (Equal)

If the values are equal, this operator returns the Boolean value TRUE, otherwise False.

Example: The following example describes how to use the -eq (equal) operator:

The last command in this example displays the Boolean Value TRUE because both the value of the variables are the same.

-ne (Equal)

If the values are not equal, this operator returns the Boolean value TRUE, otherwise False.

Example: The following example describes how to use the -eq (equal) operator:

The last command in this example displays the Boolean Value FALSE because both the value of the variables are same.

-gt (Greater than)

If the value of a variable on the left side of the operator is greater than the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -gt (greater than) operator:

The last command in this example displays the Boolean Value TRUE because the value of the variable $a is greater than $b.

-ge (Greater than or Equal)

If the value of a variable on the left side of the operator is greater than or equal to the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -ge (greater than or Equal) operator:

The last command in this example displays the Boolean Value TRUE because the value of both the variables $a, and $b are the same.

-lt (less than)

If the value of a variable on the left side of the operator is less than the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -lt (less than) operator:

The last command in this example displays the Boolean Value TRUE because the value of the variable $a is less than $b.

-le (less than or Equal)

If the value of a variable on the left side of the operator is less than or equal to the value of a variable on the right side of the operator, this operator returns the Boolean value TRUE, otherwise returns False.

Example: The following example describes how to use the -le (less than or Equal) operator:

The last command in this example displays the Boolean Value TRUE because the value of both the variables $a, and $b are the same.

Matching Operators

The matching operators are those operators, which compare the strings using regular expression or wildcard characters to find a match.

The following are the list of matching operators:

  1. -like
  2. -notlike
  3. -match
  4. -notmatch

-like

The -like operator returns the Boolean value TRUE if the strings are matched using the wildcard characters.

Examples: The following examples describe how to use the -like operator:

Example1:

The last command, in example1, returns the TRUE value because both the strings are same.

Example2:

The last command in this example returns the TRUE value because the “Shell” string is present in the variable $a.

-notlike

The -notlike operator returns the Boolean value TRUE if the strings do not match using the wildcard characters.

Examples: The following examples describe how to use the -notlike operator:

Example1:

The last command in the above example returns the TRUE value because the strings do not match.

Example2:

The last command in this example returns the False value because the “Shell” string is present in the variable $a.

-match

The -match operator returns the Boolean value TRUE if the strings are matched using the wildcard characters. If an input is a list, the -match operator returns the matching members of the list.

Example:

The last command in this example returns the string “February“.

-notmatch

The -notmatch operator returns the Boolean value True when the strings do not match using the wildcard characters.

Examples: The following examples describe how to use the -notmatch operator:

Example1:

The last command in this example returns the FALSE value because the string “ell” is present in the string of variable $a.

Example2:

The last command in this example returns the following strings:

January  March  April  

Containment Operators

The containment operators are similar to the equality operators. These operators always return a Boolean value True, when a value on the right side of the operator exists in the set of values on the left side of the operator, otherwise returns False.

The following are the list of Containment operators:

  1. -contains
  2. -notcontains
  3. -in
  4. -notin

-Contains

This operator returns the Boolean value TRUE when a value on the right side of the operator exists in the set of values on the left side of the operator.

Example:

The last command in this example returns True.

-notContains

This operator returns the Boolean value TRUE when a value on the right side of the operator does not exist in the set of values on the left side of the operator.

Example:

The last command in this example returns False.

-in

This operator returns the Boolean value TRUE when a value on the left side of the operator exists in the set of values on the right side of the operator.

Example:

The last command in this example returns True.

-notin

This operator returns the Boolean value TRUE when a value on the left side of the operator does not exist in the set of values on the right side of the operator.

Example:

The last command in this example returns False.

Replacement Operator

The replacement operator (-replace) is an operator, which replaces all or some part of value by the specified value using a regular expression.

The following statement is the syntax for the -replace operator:

This syntax uses two arguments: Original and substitute. These are separated by the comma.

Example:

The second command in this example displays the following output:

Windows and MacOS operating system  

Type Comparison Operators

The type comparison operators are those operators, which returns a Boolean value TRUE when the value on the left side of the operator is a specified as a Microsoft .NET type, otherwise returns False.

PowerShell supports the following two type comparison operators:

  1. -is
  2. -isnot

-is

This operator returns the True when the value on the left side of the operator is a specified as a Microsoft .NET type.

Example:

The second command in the example displays a Boolean value True because the value of the variable $a is an integer.

-isnot

The -isnot operator returns the Boolean value True when the value on the left side of the operator is not specified as a Microsoft .NET type.

Example:

The second command in the example displays a Boolean value True because the value of the variable $a is a string.


Next TopicLogical Operators

spot_img
Previous articleLearn MS Word Tutorial
Next articleWhat is PowerShell cmdlet