共用方式為


關於比較運算符

簡短描述

描述比較 PowerShell 中值的運算子。

完整描述

比較運算子可讓您指定比較值的條件,以及尋找符合指定模式的值。 若要使用比較運算符,請指定您想要與分隔這些值的運算符比較的值。

PowerShell 包含下列比較運算子:

類型 運算子 說明
等式 -eq equals
-ne 不等於
-gt 大於
-ge 大於或等於
-lt 小於
-le 小於或等於
Matching -like 當字串符合通配符時傳回 true
模式
-notlike 當字串不相符時,傳回 true
通配符模式
-match 當字串符合 regex 時傳回 true
模式;$matches包含相符的字串
-notmatch 當字串不相符時,傳回 true
regex 模式;$matches包含相符專案
字串
Containment -contains 當包含參考值時,傳回 true
集合中的
-notcontains 當參考值不是時,傳回 true
包含在集合中
-in 當測試值包含在 中時傳回 true
collection
-notin 當測試值未包含時,傳回 true
集合中的
取代 -取代 取代字串模式
類型 -是 如果兩個物件都相同,則傳回 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 運算子 (-like-notlike) 使用通配符運算式尋找符合或不符合指定模式的專案。

語法是:

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

比對 -match 運算子 (和 -notmatch) 使用正則表達式尋找符合或不符合指定模式的專案。

當輸入 (左側自變數) 為單一純量物件時,比對運算符會 $Matches 填入自動變數。 當輸入為純量時,和 -notmatch 運算符會-match傳回布爾值,並將自動變數的值$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 自動變數。

如果輸入是集合,則 和 -notmatch 運算符會-match傳回該集合的相符成員,但運算元不會填入$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 鍵是 Integer。 您可以使用任何 Hashtable 方法來存取儲存的值。

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

PS> $Matches[0]
Dog

PS> $Matches.Item(0)
Dog

PS> $Matches.0
Dog

運算子 -notmatch 會在 $Matches 輸入為純量時填入自動變數,而結果為 False,也就是當它偵測到相符專案時。

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

另請參閱