关于比较运算符

简短说明

介绍在 PowerShell 中比较值的运算符。

长说明

比较运算符允许指定用于比较值和查找与指定模式匹配的值的条件。 若要使用比较运算符,请指定要与分隔这些值的运算符进行比较的值。

PowerShell 包含以下比较运算符:

类型 运算符 说明
相等 -eq 等于
-ne 不等于
-gt 大于
-ge 大于或等于
-lt 小于
-le 小于或等于
Matching -like 字符串与通配符匹配时返回 true
pattern
-notlike 如果字符串不匹配,则返回 true
通配符模式
-match 字符串与正则表达式匹配时返回 true
模式;$matches包含匹配的字符串
-notmatch 如果字符串不匹配,则返回 true
正则表达式模式;$matches包含匹配
字符串
Containment -contains 当引用值包含时返回 true
在集合中
-notcontains 当引用值不时,返回 true
包含在集合中
-in 当 测试值包含在 中时,返回 true
collection
-notin 如果未包含测试值,则返回 true
在集合中
Replacement -取代 替换字符串模式
类型 -是 如果两个对象相同,则返回 true
类型
-isnot 如果对象不相同,则返回 true
类型

默认情况下,所有比较运算符不区分大小写。 若要使比较运算符区分大小写,请在运算符名称前面加上 c。 例如,区分大小写的版本 -eq-ceq。 若要显式区分大小写,请在 运算符前面加上 i。 例如,不区分大小写的 -eq 显式版本为 -ieq

当运算符的输入是标量值时,比较运算符将返回布尔值。 当输入是值的集合时,比较运算符将返回任何匹配值。 如果集合中没有匹配项,比较运算符将返回空数组。

PS> (1, 2 -eq 3).GetType().FullName
System.Object[]

包含运算符、In 运算符和类型运算符例外,它们始终返回 布尔 值。

注意

如果需要将值与 $null 进行比较,应放在 $null 比较的左侧。 与 Object[] 进行比较$null时,结果为 False,因为比较对象是数组。 将数组与 $null进行比较时,比较会筛选出数组中存储的任何 $null 值。 例如:

PS> $null -ne $null, "hello"
True
PS> $null, "hello" -ne $null
hello

相等运算符

相等运算符 (-eq-ne 当一个或多个输入值与指定模式相同时,) 返回 TRUE 或匹配值。 整个模式必须与整个值匹配。

例如:

-eq

说明:等于 。 包括相同的值。

例如:

PS> 2 -eq 2
True

PS> 2 -eq 3
False

PS> 1,2,3 -eq 2
2
PS> "abc" -eq "abc"
True

PS> "abc" -eq "abc", "def"
False

PS> "abc", "def" -eq "abc"
abc

-ne

说明:不等于 。 包括不同的值。

例如:

PS> "abc" -ne "def"
True

PS> "abc" -ne "abc"
False

PS> "abc" -ne "abc", "def"
True

PS> "abc", "def" -ne "abc"
def

-gt

说明:大于。

例如:

PS> 8 -gt 6
True

PS> 7, 8, 9 -gt 8
9

注意

这不应与 >(在许多其他编程语言中大于运算符)混淆。 在 PowerShell 中, > 用于重定向。 有关详细信息,请参阅 About_redirection

-ge

说明:大于或等于。

例如:

PS> 8 -ge 8
True

PS> 7, 8, 9 -ge 8
8
9

-lt

说明:小于。

例如:


PS> 8 -lt 6
False

PS> 7, 8, 9 -lt 8
7

-le

说明:小于或等于。

例如:

PS> 6 -le 8
True

PS> 7, 8, 9 -le 8
7
8

匹配运算符

类似运算符 (-like-notlike) 使用通配符表达式查找与指定模式匹配或不匹配的元素。

语法为:

<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>

匹配运算符 (-match-notmatch) 使用正则表达式查找与指定模式匹配或不匹配的元素。

当输入 (运算符的 $Matches 左侧参数) 为单个标量对象时,匹配运算符将填充自动变量。 当输入为标量时, -match-notmatch 运算符返回一个布尔值,并将自动变量的值 $Matches 设置为参数的匹配组件。

语法为:

<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>

-like

说明:使用通配符 (*) 进行匹配。

例如:

PS> "PowerShell" -like "*shell"
True

PS> "PowerShell", "Server" -like "*shell"
PowerShell

-notlike

说明:使用通配符 (*) 不匹配。

例如:

PS> "PowerShell" -notlike "*shell"
False

PS> "PowerShell", "Server" -notlike "*shell"
Server

-match

说明:使用正则表达式匹配字符串。 当输入为标量时,它将填充 $Matches 自动变量。

如果输入是集合, -match 则 和 -notmatch 运算符返回该集合的匹配成员,但 运算符不会填充变量 $Matches

例如,以下命令将字符串集合提交到 -match 运算符。 运算符 -match 返回集合中匹配的项。 它不会填充 $Matches 自动变量。

PS> "Sunday", "Monday", "Tuesday" -match "sun"
Sunday

PS> $Matches
PS>

相比之下,以下命令将单个字符串提交到 -match 运算符。 运算符 -match 返回布尔值并填充 $Matches 自动变量。 自动 $Matches 变量是 哈希表。 如果未使用分组或捕获,则只填充一个键。 键 0 表示匹配的所有文本。 有关使用正则表达式进行分组和捕获的详细信息,请参阅 about_Regular_Expressions

PS> "Sunday" -match "sun"
True

PS> $Matches

Name                           Value
----                           -----
0                              Sun

请务必注意, $Matches 哈希表将仅包含任何匹配模式的第一个匹配项。

PS> "Banana" -match "na"
True

PS> $Matches

Name                           Value
----                           -----
0                              na

重要

0整数。 可以使用任何 Hashtable 方法来访问存储的值。

PS> "Good Dog" -match "Dog"
True

PS> $Matches[0]
Dog

PS> $Matches.Item(0)
Dog

PS> $Matches.0
Dog

-notmatch 输入为标量且结果为 False 时,运算符将填充 $Matches 自动变量,即当它检测到匹配项时。

PS> "Sunday" -notmatch "rain"
True

PS> $matches
PS>

PS> "Sunday" -notmatch "day"
False

PS> $matches

Name                           Value
----                           -----
0                              day

-notmatch

说明:与字符串不匹配。 使用正则表达式。 当输入为标量时,它将填充 $Matches 自动变量。

例如:

PS> "Sunday" -notmatch "sun"
False

PS> $matches
Name Value
---- -----
0    sun

PS> "Sunday", "Monday" -notmatch "sun"
Monday

包含运算符

包含运算符 (-contains-notcontains) 类似于相等运算符。 但是,包含运算符始终返回布尔值,即使输入为集合也是如此。

此外,与相等运算符不同,包含运算符在检测到第一个匹配项后立即返回一个值。 相等运算符评估所有输入,然后返回集合中的所有匹配项。

-contains

说明:包含运算符。 指示引用值的集合是否包含单个测试值。 始终返回布尔值。 仅当测试值与至少一个引用值完全匹配时,才返回 TRUE。

当测试值为集合时,Contains 运算符使用引用相等性。 仅当其中一个引用值是测试值对象的同一实例时,它才返回 TRUE。

在非常大的集合中 -contains ,运算符返回结果的速度快于等于 运算符。

语法:

<Reference-values> -contains <Test-value>

示例:

PS> "abc", "def" -contains "def"
True

PS> "Windows", "PowerShell" -contains "Shell"
False  #Not an exact match

# Does the list of computers in $DomainServers include $ThisComputer?
PS> $DomainServers -contains $thisComputer
True

PS> "abc", "def", "ghi" -contains "abc", "def"
False

PS> $a = "abc", "def"
PS> "abc", "def", "ghi" -contains $a
False
PS> $a, "ghi" -contains $a
True

-notcontains

说明:包含运算符。 指示引用值的集合是否包含单个测试值。 始终返回布尔值。 如果测试值不是至少一个引用值的完全匹配项,则返回 TRUE。

当测试值为集合时,NotContains 运算符使用引用相等性。

语法:

<Reference-values> -notcontains <Test-value>

示例:

PS> "Windows", "PowerShell" -notcontains "Shell"
True  #Not an exact match

# Get cmdlet parameters, but exclude common parameters
function get-parms ($cmdlet)
{
    $Common = "Verbose", "Debug", "WarningAction", "WarningVariable",
      "ErrorAction", "ErrorVariable", "OutVariable", "OutBuffer"

    $allparms = (Get-Command $Cmdlet).parametersets |
      foreach {$_.Parameters} |
        foreach {$_.Name} | Sort-Object | Get-Unique

    $allparms | where {$Common -notcontains $_ }
}

# Find unapproved verbs in the functions in my module
PS> $ApprovedVerbs = Get-Verb | foreach {$_.verb}
PS> $myVerbs = Get-Command -Module MyModule | foreach {$_.verb}
PS> $myVerbs | where {$ApprovedVerbs -notcontains $_}
ForEach
Sort
Tee
Where

-in

说明:In 运算符。 指示测试值是否出现在引用值的集合中。 始终返回布尔值。 仅当测试值与至少一个引用值完全匹配时,才返回 TRUE。

当测试值为集合时,In 运算符使用引用相等性。 仅当其中一个引用值是测试值对象的同一实例时,它才返回 TRUE。

-in 运算符是在 PowerShell 3.0 中引入的。

语法:

<Test-value> -in <Reference-values>

示例:

PS> "def" -in "abc", "def"
True

PS> "Shell" -in "Windows", "PowerShell"
False  #Not an exact match

PS> "Windows" -in "Windows", "PowerShell"
True  #An exact match

PS> "Windows", "PowerShell" -in "Windows", "PowerShell", "ServerManager"
False  #Using reference equality

PS> $a = "Windows", "PowerShell"
PS> $a -in $a, "ServerManager"
True  #Using reference equality

# Does the list of computers in $DomainServers include $ThisComputer?
PS> $thisComputer -in  $domainServers
True

-notin

说明:指示测试值是否显示在引用值集合中。 始终返回布尔值。 如果测试值与至少一个引用值不完全匹配,则返回 TRUE。

当测试值为集合时,In 运算符使用引用相等性。 仅当其中一个引用值是测试值对象的同一实例时,它才返回 TRUE。

-notin 运算符是在 PowerShell 3.0 中引入的。

语法:

<Test-value> -notin <Reference-values>

示例:

PS> "def" -notin "abc", "def"
False

PS> "ghi" -notin "abc", "def"
True

PS> "Shell" -notin "Windows", "PowerShell"
True  #Not an exact match

PS> "Windows" -notin "Windows", "PowerShell"
False  #An exact match

# Find unapproved verbs in the functions in my module
PS> $ApprovedVerbs = Get-Verb | foreach {$_.verb}
PS> $MyVerbs = Get-Command -Module MyModule | foreach {$_.verb}

PS> $MyVerbs | where {$_ -notin $ApprovedVerbs}
ForEach
Sort
Tee
Where

替换运算符

运算符 -replace 使用正则表达式将值的全部或部分替换为指定的值。 可以使用 -replace 运算符执行许多管理任务,例如重命名文件。 例如,以下命令将所有 .txt 文件的文件扩展名更改为.log:

Get-ChildItem *.txt | Rename-Item -NewName { $_.name -replace '\.txt$','.log' }

运算符的 -replace 语法如下所示,其中 <original> 占位符表示要替换的字符, <substitute> 占位符表示将替换它们的字符:

<input> <operator> <original>, <substitute>

默认情况下, -replace 运算符不区分大小写。 若要使其区分大小写,请使用 -creplace。 若要使其显式不区分大小写,请使用 -ireplace

请开考虑以下示例:

PS> "book" -replace "B", "C"
Cook
"book" -ireplace "B", "C"
Cook
"book" -creplace "B", "C"
book

还可以使用正则表达式通过捕获组和替换来动态替换文本。 有关详细信息,请参阅 about_Regular_Expressions

ScriptBlock 替换

从 PowerShell 6 开始,可以对替换文本使用 ScriptBlock 参数。 ScriptBlock 将针对在输入字符串中找到的每个匹配项执行。

ScriptBlock 中,使用 $_ 自动变量引用当前的 System.Text.RegularExpressions.Match 对象。 Match 对象使你能够访问要替换的当前输入文本以及其他有用信息。

此示例将三个小数的每个序列替换为等效字符。 ScriptBlock 针对需要替换的每组三位小数运行。

PS> "072101108108111" -replace "\d{3}", {[char][int]$_.Value}
Hello

类型比较

类型比较运算符 (-is-isnot) 用于确定对象是否为特定类型。

-是

语法:

<object> -is <type reference>

例如:

PS> $a = 1
PS> $b = "1"
PS> $a -is [int]
True
PS> $a -is $b.GetType()
False

-isnot

语法:

<object> -isnot <type reference>

例如:

PS> $a = 1
PS> $b = "1"
PS> $a -isnot $b.GetType()
True
PS> $b -isnot [int]
True

另请参阅