about_Comparison_Operators

应用到: Windows PowerShell 2.0, Windows PowerShell 3.0, Windows PowerShell 4.0, Windows PowerShell 5.0

在此处插入介绍。

主题

about_Comparison_Operators

简短说明

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

详细说明

比较运算符可使你指定用于比较值并查找与指定模式匹配的值的条件。若要使用比较运算符,请指定要比较的值以及分隔这些值的运算符。

Windows PowerShell 包括以下比较运算符:

-eq
      -ne
      -gt
      -ge   
      -lt
      -le
      -Like
      -NotLike
      -Match
      -NotMatch
      -Contains
      -NotContains
      -In
      -NotIn
      -Replace

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

当对运算符的输入为标量值时,比较运算符将返回布尔值。当输入为值的集合时,比较运算符将返回任何匹配的值。如果集合中没有匹配项,比较运算符不返回任何内容。

例外情况是包含运算符(-Contains、-NotContains),In 运算符(-In、-NotIn)和类型运算符(-Is、-IsNot),这些运算符始终返回布尔值。

Windows PowerShell 支持以下比较运算符。

-eq

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

示例:

        PS C:\> "abc" -eq "abc"
        True

        PS C:\> "abc" -eq "abc", "def"
        False

        PS C:\> "abc", "def" -eq "abc"
        abc

-ne

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

示例:

        PS C:\> "abc" -ne "def"
        True

        PS C:\> "abc" -ne "abc"
        False

        PS C:\> "abc" -ne "abc", "def"
        True

        PS C:\> "abc", "def" -ne "abc"
        def

-gt

说明:大于。

示例:

          PS C:\> 8 -gt 6
          True

          PS C:\> 7, 8, 9 -gt 8
          9

-ge

说明:大于或等于。

示例:

          PS C:\> 8 -ge 8
          True         

          PS C:\> 7, 8, 9 -ge 8
          8
          9

-lt

说明:小于。

示例:

          PS C:\> 8 -lt 6
          False

          PS C:\> 7, 8, 9 -lt 8
          7

-le

说明:小于或等于。

示例:

          PS C:\> 6 -le 8
          True

          PS C:\> 7, 8, 9 -le 8
          7
          8

-Like

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

示例:

          PS C:\> "Windows PowerShell" -like "*shell"
          True

          PS C:\> "Windows PowerShell", "Server" -like "*shell"
          Windows PowerShell

-NotLike

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

示例:

          PS C:\> "Windows PowerShell" -NotLike "*shell"
          False

          PS C:\> "Windows PowerShell", "Server" -NotLike "*shell"
          Server   

-Match

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

示例:

           PS C:\> "Sunday" -Match "sun" 
          True 

          PS C:\> $matches 
          Name Value 
          ---- ----- 
          0    Sun
 
          PS C:\> "Sunday", "Monday" -Match "sun" 
          Sunday

-NotMatch

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

示例:

          PS C:\> "Sunday" -NotMatch "sun"
          False

          PS C:\> $matches 
          Name Value 
          ---- ----- 
          0    sun

          PS C:\> "Sunday", "Monday" -NotMatch "sun" 
          Monday

-Contains

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

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

语法:

<Reference-values> -Contains <Test-value>

示例:

          PS C:\> "abc", "def" -Contains "def"
          True

          PS C:\> "Windows", "PowerShell" -Contains "Shell"
          False  #Not an exact match


          # Does the list of computers in $domainServers
          # include $thisComputer?
          # -------------------------------------------
          PS C:\> $domainServers -Contains $thisComputer
          True

          PS C:\> "abc", "def", "ghi" -Contains "abc", "def"
          False

          PS C:\> $a = "abc", "def"
          PS C:\> "abc", "def", "ghi" -Contains $a
          False
          PS C:\> $a, "ghi" -Contains $a
          True

-NotContains

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

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

语法:

<Reference-values> -NotContains <Test-value>

示例:

PS C:\> "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 C:\> $ApprovedVerbs = Get-Verb | foreach {$_.verb}
          PS C:\> $myVerbs = Get-Command -Module MyModule | foreach {$_.verb}

          PS C:\> $myVerbs | where {$ApprovedVerbs -NotContains $_}      
          ForEach
          Sort
          Tee
          Where

-In

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

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

Windows PowerShell 3.0 中引入了 In 运算符。

语法:

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

示例:

PS C:\> "def" -in "abc", "def"
          True

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

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

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

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

          # Does the list of computers in $domainServers
          # include $thisComputer?
          # -------------------------------------------
          PS C:\> $thisComputer -in  $domainServers
          True

-NotIn

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

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

Windows PowerShell 3.0 中引入了 NotIn 运算符。

语法:

<Test-value> -NotIn <Reference-values> 

示例:

PS C:\> "def" -NotIn "abc", "def"
          False

          PS C:\> "ghi" -NotIn "abc", "def"
          True

          PS C:\> "Shell" -NotIn "Windows", "PowerShell"
          True  #Not an exact match

          PS C:\> "Windows" -NotIn "Windows", "PowerShell"
          False  #An exact match



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

          PS C:\> $myVerbs | where {$_ -NotIn $ApprovedVerbs}      
          ForEach
          Sort
          Tee
          Where

-Replace

说明:Replace 运算符。更改某个值的指定元素

示例:

PS C:\> "Get-Process" -Replace "Get", "Stop"
          Stop-Process

          # Change all .GIF file name extension to .JPG
          PS C:\> dir *.gif | foreach {$_ -Replace ".gif", ".jpg"} 

相等运算符

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

C:PS> 2 -eq 2
          True

          C:PS> 2 -eq 3
          False

          C:PS> 1,2,3 -eq 2
          2

          C:PS> "PowerShell" -eq "Shell"
          False

          C:PS> "Windows", "PowerShell" -eq "Shell"
          C:PS> 

          PS C:\> "abc", "def", "123" -eq "def"
          def
 
          PS C:\> "abc", "def", "123" -ne "def"
          abc
          123

包含运算符

包含运算符(-Contains 和 -NotContains)类似于相等运算符。但是,即使当输入是集合时,包含运算符也始终返回布尔值。

此外,与相等运算符不同,包含运算符在检测到第一个匹配项后立即返回值。相等运算符评估所有输入,然后返回该集合中的所有匹配项。以下示例显示了 -Contains 运算符的效果:

C:PS> 1,2,3 -contains 2
          True

          C:PS> "PowerShell" -contains "Shell"
          False

          C:PS> "Windows", "PowerShell" -contains "Shell"
          False

          PS C:\> "abc", "def", "123" -contains "def"
          True

          PS C:\> "true", "blue", "six" -contains "true"
          True

以下示例显示了包含运算符与等于运算符有何不同。包含运算符在发现第一个匹配时返回 TRUE 值。

PS C:\> 1,2,3,4,5,4,3,2,1 -eq 2
          2
          2

          PS C:\> 1,2,3,4,5,4,3,2,1 -contains 2
          True

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

匹配运算符

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

语法是:

<string[]> -Match <regular-expression>
          <string[]> -NotMatch <regular-expression>

以下示例显示了某些 -Match 运算符的用法:

PS C:\> "Windows", "PowerShell" -Match ".shell"
          PowerShell

          PS C:\> (Get-Command Get-Member -Syntax) -Match "-view"
          True

          PS C:\> (Get-Command Get-Member -Syntax) -NotMatch "-path"
          True

          PS C:\> (Get-Content Servers.txt) -Match "^Server\d\d"
          Server01
          Server02

匹配运算符仅在字符串中搜索。它们无法在整数数组或其他对象中进行搜索。

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

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

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

PS C:\> "Sunday", "Monday", "Tuesday" -Match "sun"
          Sunday

          PS C:\> $matches
          PS C:\>

相反,以下命令将单个字符串提交到 -Match 运算符。-Match 运算符返回布尔值并填充 $Matches 自动变量。

PS C:\> "Sunday" -Match "sun"
          True

          PS C:\> $matches

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

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

PS C:\> "Sunday" -NotMatch "rain"
          True

          PS C:\> $matches
          PS C:\>
          
          PS C:\> "Sunday" -NotMatch "day"
          False

          PS C:\> $matches
          PS C:\>

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

REPLACE 运算符

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

Get-ChildItem | Rename-Item -NewName { $_ -Replace '.gif$','.jpg$' }

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

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

默认情况下,-Replace 运算符不区分大小写。若要使其区分大小写,请使用 -cReplace。若要使其显式不区分大小写,请使用 -iReplace。请考虑以下示例:

PS C:\> "book" -Replace "B", "C"
          Cook
          PS C:\> "book" -iReplace "B", "C" 
          Cook
          PS C:\> "book" -cReplace "B", "C"
          book
 
         PS C:\> '<command:parameter required="false" variableLength="true" globbing="false"'`
                 | foreach {$_ -replace 'globbing="false"', 'globbing="true"'}
         <command:parameter required="false" variableLength="true" globbing="true"

按位运算符

Windows PowerShell 支持标准按位运算符,包括 bitwise-AND (-bAnd)、非独占和独占 bitwise-OR 运算符(-bOr 和 -bXor)和位 bitwise-NOT (-bNot)。

从 Windows PowerShell 2.0 开始,所有按位运算符都使用 64 位整数。

从 Windows PowerShell 3.0 开始,引入了 -shr(右移)和 -shl(左移)以支持 Windows PowerShell 中的按位运算。

Windows PowerShell 支持以下按位运算符。

      Operator  Description               Example  
      --------  ----------------------    -------------------
      -bAnd     Bitwise AND               PS C:\> 10 -band 3
                                          2
 
      -bOr      Bitwise OR (inclusive)    PS C:\> 10 -bor 3
                                          11    

      -bXor     Bitwise OR (exclusive)    PS C:\> 10 -bxor 3
                                          9

      -bNot     Bitwise NOT               PS C:\> -bNot 10
                                          -11

      -shl      Shift-left                PS C:\> 100 -shl 2
                                          400
 
      -shr      Shift-right               PS C:\> 100 -shr 1
                                          50

按位运算符对值的二进制格式有效。例如,数字 10 的位结构为 00001010(基于 1 个字节),而数字 3 的位结构为 00000011。当使用按位运算符来比较 10 和 3 时,将比较每个字节中的各个位。

在按位 AND 运算中,仅当输入位都为 1 时,生成的位才设置为 1。

1010      (10)
          0011      ( 3)
          --------------  bAND
          0010      ( 2)

在按位 OR(非独占)运算中,当其中一个或两个输入位都为 1 时,生成的位才设置为 1。仅当两个输入位都设置为 0 时,生成的位才设置为 0。

1010      (10)
          0011      ( 3)
          --------------  bOR (inclusive)
          1011      (11)

在按位 OR(独占)运算中,仅当一个输入位为 1 时,生成的位才设置为 1。

1010      (10)
          0011      ( 3)
          --------------  bXOR (exclusive)
          1001      ( 9)

按位 NOT 运算符是生成值的二进制补集的一元运算符。位 1 设置为 0,位 0 设置为 1。

例如,0 的二进制补集为 -1,它是最大的无符号整数 (0xffffffff),而 -1 的二进制补集为 0。

PS C:\> -bNOT 10
          -11


          0000 0000 0000 1010  (10)
          ------------------------- bNOT
          1111 1111 1111 0101  (-11, xfffffff5) 

在按位左移运算中,所有位都向左移动“n”个位置,其中“n”是右操作数的值。在一个位置中插入零。

当左操作数是 Integer(32 位)值时,右操作数的较低的 5 位将决定移动多少位左操作数。

当左操作数是 Long(64 位)值时,右操作数的较低的 6 位将决定移动多少位左操作数。

PS C:\> 21 -shl 1
        42

          00010101  (21)
          00101010  (42)

        PS C:\> 21 -shl 2
        84

          00010101  (21)
          00101010  (42)
          01010100  (84)

在按位右移运算中,所有位都向右移动“n”个位置,其中“n”由右操作数指定。在向右移动正数值或无符号值时,右移运算符 (-shr) 在最左边的位置插入一个零。

当左操作数是 Integer(32 位)值时,右操作数的较低的 5 位将决定移动多少位左操作数。

当左操作数是 Long(64 位)值时,右操作数的较低的 6 位将决定移动多少位左操作数。

        PS C:\> 21 -shr 1
        10

          00010101  (21)
          00001010  (10)

        PS C:\> 21 -shr 2
        5

          00010101  (21)
          00001010  (10)
          00000101  ( 5)

另请参阅

about_Operators

about_Regular_Expressions

about_Wildcards

Compare-Object

Foreach-Object

Where-Object