C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Comparison OperatorsThe 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:
Equality OperatorsThe 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:
-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: $a=10 $b=10 ($a -eq $b) 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: $a=10 $b=10 ($a -ne $b) 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: $a=20 $b=10 ($a -gt $b) 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: $a=10 $b=10 ($a -ge $b) 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: $a=10 $b=20 ($a -lt $b) 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: $a=10 $b=10 ($a -le $b) 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 OperatorsThe 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:
-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: $a="PowerShell" $b="PowerShell" $a -like $b The last command, in example1, returns the TRUE value because both the strings are same. Example2: $a="PowerShell" $b="*Shell" $a -like $b 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: $a="PowerShell" $b="windows" $a -notlike $b The last command in the above example returns the TRUE value because the strings do not match. Example2: $a="PowerShell" $b="*Shell" $a -notlike $b 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: $a="January", "February", "March", "April" $b="Feb" $a -match $b 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: $a="PowerShell" $b="ell" $a -notmatch $b The last command in this example returns the FALSE value because the string "ell" is present in the string of variable $a. Example2: $a="January", "February", "March", "April" $b="Feb" $a -notmatch $b The last command in this example returns the following strings: January March April Containment OperatorsThe 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:
-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: $a="January", "February", "March", "April" $b="March" $a -contains $b 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: $a="January", "February", "March", "April" $b="March" $a -notcontains $b 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: $b="March" $b="February", "March", "April" $a -in $b 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: $b="March" $b="February", "March", "April" $a -notin $b The last command in this example returns False. Replacement OperatorThe 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: <input> <operator> <original> , <substitute> This syntax uses two arguments: Original and substitute. These are separated by the comma. Example: $a="Windows and Linux Operating system" $a -replace "Linux" , "MacOS" The second command in this example displays the following output: Windows and MacOS operating system Type Comparison OperatorsThe 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:
-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: $a=1 $a -is [int] 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: $a="1" $a -is [int] The second command in the example displays a Boolean value True because the value of the variable $a is a string.
Next TopicLogical Operators
|