ZetCode

PowerShell 运算符和表达式

最后修改:2025 年 2 月 15 日

在本文中,我们将讨论 PowerShell 中的运算符和表达式。

运算符 是用于对值和变量执行操作的特殊字符或关键字。这些运算符可以大致分为几种类型,例如算术运算符、比较运算符、逻辑运算符和赋值运算符。像 +, -, *, 和 / 这样的算术运算符用于基本的数学运算。像 -eq, -ne, -gt, -lt, -ge, 和 -le 这样的比较运算符用于比较值。像 -and, -or, 和 -not 这样的逻辑运算符用于对布尔表达式执行逻辑运算。此外,像 = 和 += 这样的赋值运算符用于为变量赋值或修改其值。

表达式是值、变量和运算符的组合,可以对其进行求值以产生结果。这些表达式可以像单个值一样简单,也可以像多个运算符和操作数的组合一样复杂。例如,表达式 2 + 3 * 4 涉及加法和乘法运算符。PowerShell 遵循标准的运算顺序(也称为运算符优先级)来评估此类表达式。括号可用于在表达式中明确定义运算顺序,以确保所需的输出。表达式是 PowerShell 脚本和命令的构建块,使用户能够高效有效地执行各种任务。

算术运算符

PowerShell 支持各种算术运算符,例如加法 (+),减法 (-),乘法 (*),除法 (/),取模 (%) 和幂运算 (**)。

运算符 描述 示例
+ 加法 5 + 2
- 减法 5 - 2
* 乘法 5 * 2
/ 除法 5 / 2
% 取模(余数) 5 % 2

该表列出了算术运算符。

arithmetic.ps1
$x = 10
$y = 3

Write-Output "Addition: $($x + $y)"
Write-Output "Subtraction: $($x - $y)"
Write-Output "Multiplication: $($x \* $y)"
Write-Output "Division: $($x / $y)"
Write-Output "Modulus: $($x % $y)"
Write-Output "Exponentiation: $($x ** $y)"

在此示例中,我们声明了两个变量 $x 和 $y,并使用相应的运算符对它们执行算术运算。

PS C:\> .\arithmetic.ps1
Addition: 13
Subtraction: 7
Multiplication: 30
Division: 3.33333333333333
Modulus: 1
Exponentiation: 1000

比较运算符

比较运算符用于比较两个值或变量。PowerShell 支持各种比较运算符,例如 -eq(等于),-ne(不等于),-lt(小于),-le(小于或等于),-gt(大于)和 -ge(大于或等于)。

运算符 描述 示例
-eq 等于 5 -eq 2
-ne 不等于 5 -ne 2
-gt 大于 5 -gt 2
-lt 小于 5 -lt 2
-ge 大于或等于 5 -ge 2
-le 小于或等于 5 -le 2

该表列出了比较运算符。

comparison.ps1
$x = 10
$y = 3

Write-Output "Equal to: $($x -eq $y)"
Write-Output "Not equal to: $($x -ne $y)"
Write-Output "Less than: $($x -lt $y)"
Write-Output "Less than or equal to: $($x -le $y)"
Write-Output "Greater than: $($x -gt $y)"
Write-Output "Greater than or equal to: $($x -ge $y)"

在此示例中,我们声明了两个变量 $x 和 $y,并使用比较运算符对它们进行比较。

PS C:\> .\comparison.ps1
Equal to: False
Not equal to: True
Less than: False
Less than or equal to: False
Greater than: True
Greater than or equal to: True

逻辑运算符

逻辑运算符用于组合表达式。PowerShell 支持各种逻辑运算符,例如 -and(与),-or(或)和 -not(非)。

运算符 描述 示例
-and 逻辑与 $true -and $false
-or 逻辑或 $true -or $false
-not 逻辑非 -not $true

该表列出了逻辑运算符。

logical.ps1
$x = 10
$y = 3

Write-Output "And: $($x -gt $y -and $x -lt 20)"
Write-Output "Or: $($x -gt $y -or $x -gt 20)"
Write-Output "Not: $(-not $x -eq 0)"

在此示例中,我们使用逻辑运算符组合表达式。

PS C:\> .\logical.ps1
And: True
Or: True
Not: False

赋值运算符

赋值运算符用于为变量赋值。PowerShell 支持各种赋值运算符,例如 =(等于),+=(加法赋值),-=(减法赋值),\*=(乘法赋值),/=(除法赋值),%=(取模赋值)和 **=(幂赋值)。

运算符 描述 示例
= 赋值 $a = 5
+= 加法赋值 $a += 2
-= 减法赋值 $a -= 2
*= 乘法赋值 $a *= 2
/= 除法赋值 $a /= 2
%= 取模赋值 $a %= 2

该表列出了赋值运算符。

assignment.ps1
$x = 10
$x += 5
Write-Output "X: $x"

在此示例中,我们声明了一个变量 $x,并使用加法赋值运算符将其值增加 5。

PS C:\> .\assignment.ps1
X: 15

特殊运算符

PowerShell 中的特殊运算符提供了处理数据和执行超越基本算术、比较和逻辑运算的操作的独特功能。

以下是特殊运算符

运算符 描述 示例
.. 范围 1..5
-match 正则表达式匹配 "hello" -match "h.*o"
-replace 使用正则表达式替换 "hello" -replace "l", "y"
-contains 包含(数组) @(1,2,3) -contains 2

该表列出了一些特殊运算符。

special.ps1
$numbers = 1..5
Write-Output "Numbers: $numbers"

$text = "PowerShell is powerful"

if ($text -match "Power.*") {
    Write-Output "The text matches the pattern."
}

$replacedText = $text -replace "powerful", "versatile"
Write-Output "Replaced text: $replacedText"

$users = @("Alice", "Bob", "Charlie")
$userToCheck = "Alice"

if ($users -contains $userToCheck) {
    Write-Output "$userToCheck is in the list."
}

该示例演示了它们的用法。

$numbers = 1..5

范围运算符生成一个从 1 到 5 的数字序列,并将其存储在 $numbers 变量中。

$replacedText = $text -replace "powerful", "versatile"

-match 运算符检查 $text 字符串是否与正则表达式模式 Power.* 匹配,该模式查找以“Power”开头后跟任意字符的字符串。

if ($users -contains $userToCheck) {
    Write-Output "$userToCheck is in the list."
}

-contains 运算符检查 $users 数组是否包含指定的项 $userToCheck,在本例中为“Alice”。

来源

PowerShell 文档

在本文中,我们讨论了 PowerShell 中的运算符和表达式。

作者

我叫 Jan Bodnar,是一位充满热情的程序员,拥有丰富的编程经验。我自 2007 年以来一直撰写编程文章。迄今为止,我已撰写了 1,400 多篇文章和 8 本电子书。我在教学编程方面拥有超过十年的经验。

列出 所有 PowerShell 教程