📜  Powershell-操作员

📅  最后修改于: 2020-11-19 07:59:27             🧑  作者: Mango


PowerShell提供了一组丰富的运算符来操作变量。我们可以将所有PowerShell运算符分为以下几类:

  • 算术运算符
  • 赋值运算符
  • 比较运算符
  • 逻辑运算符
  • 重定向运算符
  • 溢出和联接运算符
  • 类型运算符
  • 一元运算符

算术运算符

算术运算运算符在数学表达式中的使用方式与在代数中使用的方式相同。下表列出了算术运算运算符-

假设整数变量A持有10,变量B持有20,则-

显示范例

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

比较运算符

以下是PowerShell语言支持的赋值运算符-

假设整数变量A持有10,变量B持有20,则-

显示范例

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

赋值运算符

以下是PowerShell语言支持的赋值运算符-

显示范例

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

逻辑运算符

下表列出了逻辑运算符-

假设布尔变量A成立,变量B成立,则-

显示范例

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

杂项运算符

以下是PowerShell语言支持的各种重要运算符-

显示范例

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