# PowerShell Operators

An operator is a symbol that specifies an action to perform in an expression, for example, the ‘+’ symbol in the expression ‘1 + 2’ would add the two numbers together to produce a result. This type of operator is known as an arithmetic operator, however, there are also other groups of operators within PowerShell that will be explained below.

## Arithmetic Operators

Arithmetic operators allow for mathematical calculations to be carried out within a PowerShell script.

Operator Description
Subtracts one value from another.
* Multiplies two values together.
/ Divides one value by another.
% Calculates the remainder after one value is divided by another.

The following examples show each of these operators in action.

```# Variables to use in expressions.
\$a = 3
\$b = 8
\$c = 0

\$c = \$a + \$b   # c will equal 11.
\$c = \$b - \$a   # c will equal 5.
\$c = \$a * \$b   # c will equal 24.
\$c = \$b / \$a   # c will equal 2.66666666666667.
\$c = \$b % \$a   # c will equal 2.```

## Assignment Operators

Assignment operators in PowerShell are used to assign values to a variable, the most basic of which is ‘=’. Here, the ‘=’ symbol means ‘gets set to’ rather than ‘equals’, so in the expression ‘x = y’, ‘x’ gets set to ‘y’. The arithmetic operator symbols can be combined with ‘=’ to form the assignment operators.

Operator Description
= Assigns the value of the right side operand to the left side operand.
+= Adds the value from the right side operand to the left side operand and assigns the value to the left side operand.
-= Subtracts the value of the right side operand from the left side operand and assigns the value to the left side operand.
*= Multiplies the value from the right side operand with the left side operand and assigns the value to the left side operand.
/= Divides the left side operand by the value of the right side operand and assigns the value to the left side operand.
%= Takes the resulting modulus of the operands on both sides and assigns it to the left hand operand.

The examples below show these assignment operators in action.

```\$a = 10   # a gets set to 10.

\$b = 2
\$b += 5   # b gets set to 7.

\$c = 10
\$c -= 4   # c gets set to 6.

\$d = 2
\$d *= 2   # d gets set to 4.

\$e = 20
\$e /= 5   # e gets set to 4.0.

\$f = 5
\$f %= 2   # f gets set to 1.```

## Relational Operators

Relational operators, or comparison operators, as they are sometimes known, compare two values and are used, for example, in ‘if’ statements, which are described in the next section on Decision Making. The result of the comparison is either True or False.

Operator Description
-eq Evaluates to True if the two values are equal.
-ne Evaluates to True if the two values are not equal.
-gt Evaluates to True if the first value is greater than the second.
-lt Evaluates to True if the first value is less than the second.
-ge Evaluates to True if the first value is greater than or equal to the second.
-le Evaluates to True if the first value is less than or equal to the second.

It should be noted that these operators can be used to compare string values as well as numbers. Where two strings are being compared, by default, it is a case-insensitive comparison, so for example, ‘Test’ and ‘test’ are equal. In order to explicitly specify a case-insensitive comparison an ‘i’ needs to be included before the letters of the operator, so the equals operator becomes ‘-ieq’. If a case-sensitive comparison is required then a ‘c’ needs to be included before the letters of the operator, for example, ‘-ceq’, for the equals operator.

## Logical Operators

Logical operators are used where it is required to test more than one condition at the same time, for example, in an ‘if’ statement, which is discussed in the next section on Decision Making.

Operator Description
-and Logical AND operator where operands on both sides must be True to evaluate to True.
-or Logical OR operator where only one of the two operands must be True to evaluate to True.
-not Logical NOT operator that can be used in conjunction with ‘AND’ or ‘OR’ to reverse the resulting operand. In the case of ‘AND’, if both conditions evaluate to True, then with the use of the ‘NOT’ operator the result would be False.
! Logical NOT operator, the same as ‘-not’ above.
-xor Logical exclusion OR operator that evaluates to True if one and only one of the operands evaluates to True or if one and only one of the operands evaluates to False.