The comparison operators are used to compare values, as well as to find values that match specific patterns. Table 1 lists the comparison operators available in Windows PowerShell.
Table 1. Windows PowerShell Comparison Operators
Operator | Description |
---|
-eq | Equal to |
-ne | Not equal to |
-gt | Greater than |
-lt | Less than |
-le | Less than or equal to |
-ge | Greater than or equal to |
-like | Match using the wildcard character (*) |
-notlike | Does not match using the wildcard character (*) |
-match | Evaluates a regular expression against the operand on the left; returns True if the match is successful |
-notmatch | Evaluates a regular expression against the operand on the left; returns True if the match is not successful |
-replace | Replaces all or part of a value with the specified value using regular expressions |
The -eq operator returns True or
an array of matching values if it can match the value on the right with
one or more values on the left. The operator returns True if an exact match is made.
PS > "http://SPServer01" -eq "http://SPServer01"
True
PS > "http://SPServer01" -eq "http://SPServer01/Site"
False
In the first example, we use the –eq
operator to compare two identical strings. In the second example, we
add a few lines to the value on the right. Since the strings are not
identical, False is returned.
We can also use the –eq operator to match an argument on the right side with an array of values on the left.
PS > "http://SPServer01", "http://SPServer02" -eq "http://SPServer02"
http://SPServer02
The -ne operator returns True
if the left value and the right value are not identical. If the left
side of the operation contains multiple values, the operator returns
ones that do not match the value on the right.
PS > "http://SPServer01" -ne "http://SPServer01"
False
PS > "http://SPServer01","http://SPServer02" -ne "http://SPServer01"
http://SPServer02
The -gt operator returns True if the left value is greater than the right value. The -ge operator returns True if the left value is greater than or equal to the right value.
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount
9
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -ge 9
True
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -gt 9
False
In this example, we use the Get-SPSiteAdministration cmdlet to retrieve the number of users of a site. In this case, there are nine users. We then use the –ge operator to check if the number of users is greater than or equal to nine, which returns True. Next, we check if the number of users is greater than nine, which returns False.
To see if the left value is less than the right value, use the -lt operator. The –le operator is for less-than or equal-to comparisons.
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -lt 9
False
PS > (Get-SPSiteAdministration -Identity http://SPServer01).UsersCount -le 9
True
The –like and –notlike operators are similar to the –eq and –ne operators, but instead of matching exact values, they allow wildcards to be used.
PS > "http://SPServer01" -like "http://SPServer01"
True
PS > "http://SPServer01/Site" -like "http://SPServer01*"
True
In the first example, we use the –like operator to compare two identical strings. Since the strings match, True
is returned. In the second example, we use a wildcard character when we
compare the values. The wildcard character matches any given character.
If the left side of the operation contains multiple values, the
operator returns matching values, rather than True or False, as shown in the next example.
PS > "http://SPServer01/internal/site1",
>> "http://SPServer01/external/site1" -like "*external*"
>>
http://SPServer01/external/site1
For case-sensitive evaluation, use the –clike operator, as demonstrated here:
PS > "http://SPServer01" -clike "http://SPServer01"
True
PS > "http://SPServer01" -clike "http://SPSERVER01"
False
The following examples demonstrate how to use the –notlike operator.
PS > "http://SPServer01" -notlike "http://SPServer01"
False
PS > "http://SPServer01/Site" -notlike "http://SPServer01"
True
PS > "http://SPServer01/Site" -notlike "http://SPServer01*"
False
If the left side of the operation contains multiple values, the operation returns values that do not match, as shown here:
PS > "http://SPServer01/internal/site1",
>> "http://SPServer01/external/site1" -notlike "*external*"
>>
http://SPServer01/internal/site1
The –match and –notmatch operators
try to match one or more of the set of string values on the left side
of the operation using regular expressions. Regular expressions are a
very powerful means of pattern matching (not specific to PowerShell or
any other technology), which allow you to create very complex and
effective comparisons. Here are some examples:
Match http://SPServer01/Site with http://SPServer01:
PS > "http://SPServer01/Site" -match "http://SPServer01"
True
Match http://SPServer01/Site with a regular expression that checks if the string starts with http://:
PS > "http://SPServer01/Site" -match "^http://"
True
Check if http://SPServer01/Site starts with http://, followed by any number of alphanumeric characters, followed by /, followed by any number of alphanumeric characters and ends with an e:
PS > "http://SPServer01/Site" -match "^http://\w*/\w*e$"
True
As you can see, it is possible to build up complex match patterns using regular expressions. Similar to the –clike operator, -cmatch performs case-sensitive matching.
You can also use multiple values on the left side and match against a value on the right side, as shown in this example:
PS > "http://SPServer01/Site",
>> "http://SPServer01",
>> "http://SPServer02" -match "http://SPServer01"
>>
http://SPServer01/Site
http://SPServer01
This example returns the left values that match the right value.
The -replace operator in Windows PowerShell
is used to find and replace substrings in a string value. It assumes
its input is a regular expression. You can use the -replace operator to search for and replace a specific pattern.
PS > "http://SPServer01/Site" -replace "^http","https"
https://SPServer01/Site
Here, we replace http with https. We use the ^ character to match the beginning of the original string.
Here is an example that uses a more complex replacement pattern:
PS > "http://SPServer01/Site" -Replace "/{2}\w*/","//SPServer02/"
http://SPServer02/Site
This example compares the string "http://SPServer01/Site"
to the specified pattern and replaces the part of the string that
starts with two / characters, contains any number of alphanumeric
characters, and ends with another /.
Note
Unlike the -replace operator, the Replace() method available through System.String does not accept regular expressions.