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。
In 運算子是在 Windows PowerShell 3.0 中引進的。
語法:
<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。
NotIn 運算子是在 Windows PowerShell 3.0 中引進的。
語法:
<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
-NotMatch 運算子會在輸入為純量且結果為 False 時 (也就是偵測到相符項目時),填入 $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 支援標準位元運算子,包括位元 AND (-bAnd)、內含和互斥位元 OR 運算子 (-bOr 和 -bXor),以及位元 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" 是右運算元的值。會將一個零插入個位數的位置。
當左運算元是整數 (32 位元) 值時,右運算元較低的 5 個位元會決定左運算元要移位多少位元。
當左運算元是長數值 (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) 會在最左邊的位置插入一個零。
當左運算元是整數 (32 位元) 值時,右運算元較低的 5 個位元會決定左運算元要移位多少位元。
當左運算元是長數值 (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