Powershell - Operators


PowerShell provides a rich set of operators to manipulate variables. We can divide all the PowerShell operators into the following groups −

  • Arithmetic Operators
  • Assignment Operators
  • Comparison Operators
  • Logical Operators
  • Redirectional Operators
  • Spilt and Join Operators
  • Type Operators
  • Unary Operators

The Arithmetic Operators

Arithmetic operators are used in mathematical expressions in the same way that they are used in algebra. The following table lists the arithmetic operators −

Assume integer variable A holds 10 and variable B holds 20, then −

Show Examples

Operator Description Example
+ (Addition) Adds values on either side of the operator. A + B will give 30
- (Subtraction) Subtracts right-hand operand from left-hand operand. A - B will give -10
* (Multiplication) Multiplies values on either side of the operator. A * B will give 200
/ (Division) Divides left-hand operand by right-hand operand. B / A will give 2
% (Modulus) Divides left-hand operand by right-hand operand and returns remainder. B % A will give 0

The Comparison Operators

Following are the assignment operators supported by PowerShell language −

Assume integer variable A holds 10 and variable B holds 20, then −

Show Examples

Operator Description Example
eq (equals) Compares two values to be equal or not. A -eq B will give false
ne (not equals) Compares two values to be not equal. A -ne B will give true
gt (greater than) Compares first value to be greater than second one. B -gt A will give true
ge (greater than or equals to) Compares first value to be greater than or equals to second one. B -ge A will give true
lt (less than) Compares first value to be less than second one. B -lt A will give false
le (less than or equals to) Compares first value to be less than or equals to second one. B -le A will give false

The Assignment Operators

Following are the assignment operators supported by PowerShell language −

Show Examples

Operator Description Example
= Simple assignment operator. Assigns values from right side operands to left side operand. C = A + B will assign value of A + B into C
+= Add AND assignment operator. It adds right operand to the left operand and assign the result to left operand. C += A is equivalent to C = C + A
-= Subtract AND assignment operator. It subtracts right operand from the left operand and assign the result to left operand. C -= A is equivalent to C = C - A

The Logical Operators

The following table lists the logical operators −

Assume Boolean variables A holds true and variable B holds false, then −

Show Examples

Operator Description Example
AND (logical and) Called Logical AND operator. If both the operands are non-zero, then the condition becomes true. (A -AND B) is false
OR (logical or) Called Logical OR Operator. If any of the two operands are non-zero, then the condition becomes true. (A -OR B) is true
NOT (logical not) Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true then Logical NOT operator will make false. -NOT(A -AND B) is true

Miscellaneous Operators

Following are various important operators supported by PowerShell language −

Show Examples

Operator Description Example
> (Redirectional Opeator) Redirectional operator. Assigns output to be printed into the redirected file/output device. dir > test.log will print the directory listing in test.log file
Advertisements