比較運算子
PowerShell 比較運算子由一個前導短劃線(-
)後跟一個名稱(equal
為 equal
,gt
為 greater than
等等)組成。
名稱前面可以加上特殊字元來修改運算子的行為:
i # Case-Insensitive Explicit (-ieq)
c # Case-Sensitive Explicit (-ceq)
Case-Insensitive 是預設值(如果未指定),(a
-eqA
)與(a
-ieqA
)相同。
簡單比較運算子:
2 -eq 2 # Equal to (==)
2 -ne 4 # Not equal to (!=)
5 -gt 2 # Greater-than (>)
5 -ge 5 # Greater-than or equal to (>=)
5 -lt 10 # Less-than (<)
5 -le 5 # Less-than or equal to (<=)
字串比較運算子
"MyString" -like "*String" # Match using the wildcard character (*)
"MyString" -notlike "Other*" # Does not match using the wildcard character (*)
"MyString" -match "$String^" # Matches a string using regular expressions
"MyString" -notmatch "$Other^" # Does not match a string using regular expressions
集合比較運算子:
"abc", "def" -contains "def" # Returns true when the value (right) is present
# in the array (left)
"abc", "def" -notcontains "123" # Returns true when the value (right) is not present
# in the array (left)
"def" -in "abc", "def" # Returns true when the value (left) is present
# in the array (right)
"123" -notin "abc", "def" # Returns true when the value (left) is not present
# in the array (right)