比较运算符

PowerShell 比较运算符由一个前导短划线(-)后跟一个名称(equalequalgtgreater 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)