Using Windows PowerShell as an IT Pro – Part 17

In my last post I looked at some Comparison Operators. Now I will examine some more Comparison Operators.

Greater than and Less than operators

The greater than operator (-gt) returns a value of TRUE or the matches when one or more of the input values is greater than the specified pattern. The less than operator (-lt) returns a value of TRUE or the matches when one or more of the input values is less than the specified pattern. When the ‘or equals to’ operators (-ge, -le) are used, they also compare to see if the value is equal to the specified pattern.

The following examples show the effects of these operators.

8 -gt 7

CompOper05

8 -ge 8

CompOper06

"c" -lt "a"

CompOper07

"a" -le "c"

CompOper08

1, 2, 3 -le 2

CompOper09

Containment Operators

The containment operators (-contains and -notcontains) are similar to the equality operators. However, the containment operators always return a Boolean value, even when the input is a collection.

Also, unlike the equality operators, the containment operators return a value as soon as they detect the first match. The equality operators evaluate all input and then return all the matches in the collection. In a very large collection, the -contains operator returns results quicker than the equal to operator. The following examples show the effect of the -contains operator.

1, 2, 3 -contains 2

CompOper10

"PowerShell" -contains "Shell"

CompOper11

"Windows", "PowerShell" -notcontains "Shell"

CompOper12

In my next post we will finish looking at Comparison Operators.

Share

Posted Thursday, April 22nd, 2010 at 4:19 pm by peterl
4,846 views

Comments are closed.