Windows PowerShell uses variables to store values, and we have a large set of operators we can use to modify or compare those variables.
PowerShell Assignment Operators
Assignment Operators allow us to simply create variables and populate them with values in one command:
$a = 5 $b = 10 $c = $a + $b
When we call those variables, we can see the values they contain:
We can add to a variable using the Add AND operator:
$a = 100 $a += 200
Likewise we can subtract from a variable using the Subtract AND operator:
$a -= 50
PowerShell Arithmetic Operators
Arithmetic Operators let us apply mathematical expressions to our variables. For example, let’s assign $a to be equal to 5.
$a = 5
We can use the addition operator to add three:
$a + 3
Bear in mind that in this case, the variable itself isn’t changed. If we call the variable, we can see that it’s still equal to 5.
$a
If we want to change the value of $a
to be equal to 5 + 3
, then we need to identify it first:
$a = $a + 3
Or, as we saw earlier, we can use the ‘+=
‘ assignment operator to add to the variable.
$a = 5 $a += 3 $a
The other mathematical operators are available to us as well.
Subtraction:
$a = 5 $a - 2
Multiplication:
$a = 5 $a * 2
Division:
$a = 10 $a / 2
Modulus, in which we divide the first figure by the second and return only the remainder. In this example, 11 goes into 40 three times, with seven remaining.
$a = 40 $a % 11
Incidentally, the modulus operator is a handy way to determine if a number is odd or even. Let’s create a simple function that takes a number as an input. The function will divide the number by two, and if the modulus value is zero, then the number divided evenly and is therefore an even number. Anything else must be an odd number.
function OddEven ($number) { if ( $number % 2 -eq 0 ) { Write-Output "$number is an even number." } else { Write-Output "$number is an odd number." } }
OddEven 5 "5 is an odd number." OddEven 16 "16 is an even number."
Powershell Comparison Operators
We have a variety of Comparison Operators to compare the values of variables with each other. The results of the comparison operators will always be the Boolean values True
or False
Let’s assign a couple of variables:
$a = 5 $b = 10
And now let’s see how the Comparison Operators work:
-eq
or ‘Equal To’. Is the first variable Equal To the second variable?
$a -eq $b
-ne
or ‘Not Equal To’. Is the first variable Not Equal To the second variable?
$a -ne $b
-gt
or ‘Greater Than’. Is the first variable Greater Than the second variable?
$a -gt $b
-ge
or ‘Greater Than or Equal To’. Is the first variable Greater Than or Equal To the second variable?
$a -ge $b
-lt
or ‘Less Than’. Is the first variable Less Than the second variable?
$a -lt $b
-le
or ‘Less Than or Equal To’. Is the first variable Less Than or Equal To the second variable?
$a -le $b
One potential mistake made by PowerShell novices is confusing Comparison Operators with Assignment Operators. Here’s an example in a script that is often seen:
$score = 10 if ( $score = 20 ) { Write-Output "You've scored twenty points. You Win!" } else { Write-Output "Keep trying. You need twenty points to win." }
As we can see, we began with the variable $score
equal to ten. Then we meant to test if $score
was equal to twenty, and if so, declare the user a winner. But we mixed the Comparison Operator ‘-eq
‘ with the Assignment Operator ‘=
‘. What occurred was that the $score
variable was instead changed from ten to twenty. All the ‘if
‘ command did was check to see if the assignment was True
, which it is. Then the script declared the user a winner, even though the requirement was for the variable to have a value of twenty, not ten.
Here’s the debugged script using the proper Comparison Operator:
$score = 10 if ( $score -eq 20 ) { Write-Output "You've scored twenty points. You Win!" } else { Write-Output "Keep trying. You need twenty points to win." }
So far, we’ve been looking at Comparison Operators with integers, but we can use the same for strings:
$a = "apple" $b = "apple" $a -eq $b
$a -ne $b
PowerShell also provides Comparison Operators which have a Matching type and are typically used with strings. Let’s create a string variable:
$p = "PowerShell"
We can use the -like
and -notlike
Comparison Operators to search the string for patterns. The pattern must be identical, unless we use wildcards:
$p -like "Shell" False $p -like "Power*" True $p -like "?owerShell" True $p -notlike "Power" True
Unlike integers, when we work with strings, case can be a factor. The string "Automation"
is not exactly the same as "automation"
, but by default PowerShell considers string comparisons to be case-insensitive:
$a = "Automation" $b = "automation" $a -eq $b True
For our purposes, that may be sufficient, or maybe not. If we need the case to match, then we can add a 'c'
after the hyphen such as -ceq
, to indicate case-sensitivity. This time if we compare $a
to $b
using -ceq
, we see the result is False
.
$a -ceq $b False
PowerShell Logical Operators
Using PowerShell Logical Operators, we can connect statements and expressions. The most common logical expressions are -and
, -or
, and -not
.
With -and
, all the expressions must evaluate to be true:
$a = 5 $b = 10 $c = 100
($a -lt $b) -and ($b -lt $c) True
With -or
, only one of the expressions must evaluate to be true:
($b -lt $c) -or ($c -eq 0) True
With -not
, the statement that follows is negated:
10 -eq (5 * 2) True -not (10 -eq (5 * 2)) False
For more information about PowerShell Operators, see the official documentation provided by Microsoft.
Bear in mind that following that link will redirect you to the documentation for the latest version of PowerShell, currently 7.2 as of this writing. If your version of PowerShell is older than that, you may wish to click the Version drop-down box to find the documentation relevant to your version. To find your version of PowerShell, enter the following into your command line:
$PSVersionTable.PSVersion