PowerShell比较运算符由前导破折号(-)和名称(eqfor equal,gtforgreater than等)组成。
名称之前可以带有特殊字符以修改运算符的行为:
i # Case-Insensitive Explicit (-ieq) c # Case-Sensitive Explicit (-ceq)
如果未指定,则不区分大小写是默认值,(“ a” -eq“ A”)与(“ a” -ieq“ A”)相同。
简单比较运算符:
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)