PowerShell provides a rich set of operators to manipulate variables. We can divide all the PowerShell operators into the following groups −
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 −
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 |
Following are the assignment operators supported by PowerShell language −
Assume integer variable A holds 10 and variable B holds 20, then −
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 |
Following are the assignment operators supported by PowerShell language −
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 following table lists the logical operators −
Assume Boolean variables A holds true and variable B holds false, then −
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 |
Following are various important operators supported by PowerShell language −
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 |