比较操作
类型 | 操作符 | 说明 |
---|---|---|
判等 | -eq | 等于 |
-ne | 不等于 | |
-gt | 大于 | |
-ge | 大于等于 | |
-lt | 小于 | |
-le | 小于等于 | |
匹配 | -like | 匹配上为真(支持通配符表达式) |
-notlike | 与上相反 | |
-match | 匹配上为真(支持正则表达式) | |
-notmatch | 与上相反 | |
替换 | -replace | 替换(支持正则表达式) |
包含 | -contains | 包含某个值的集合 |
-notcontains | 与上相反 | |
-in | 值在集合中 | |
-notin | 与上相反 | |
类型 | -is | 对象是同一类型 |
-isnot | 与上相反 |
共有特性
默认比较操作不区分大小写。在-后加c使得区分大小写。如-ceq,‘a’ -ceq 'A’为False。为了不区分大小写,在-后添加一个i。如-ieq,‘a’ -ieq 'A’为True。
当输入单个值,返回一个布尔值;当输入一个集合,返回集合中匹配的元素。特别的:包含和类型操作总返回布尔值;-replace返回替换的结果;若左边的表达式不是集合,-match和-notmatch会给自动变量$Matches赋值。
详细说明
-eq和-ne
#左侧是一个元素
2 -eq 2 # Output: True
2 -eq 3 # Output: False
"abc" -eq "abc" # Output: True
"abc" -eq "abc", "def" # Output: False!!!!
"abc" -ne "def" # Output: True
"abc" -ne "abc" # Output: False
"abc" -ne "abc", "def" # Output: True
#左侧是一个集合时,-eq返回相等的元素。-ne则会过滤他们
1,2,3 -eq 2 # Output: 2
"abc", "def" -eq "abc" # Output: abc
"abc", "def" -ne "abc" # Output: def
"zzz", "def", "zzz" -eq "zzz" #Output:zzz zzz (两个zzz会换行)
#判断集合$a是否为空
$a = 1, 2, $null, 4, $null, 6 #$a包含空元素
$null -ne $a #Output: True。说明$a非空
$a -ne $null # Output: 1, 2, 4, 6。即输入非空元素
-gt, -ge, -lt, 和 -le
#左右都是一个值。结果返回布尔值
8 -gt 6 # Output: True
8 -ge 8 # Output: True
6 -lt 8 # Output: True
8 -le 8 # Output: True
#左边是一个集合,返回左边满足逻辑的元素
$a=5, 6, 7, 8, 9
$a -gt 7 # Output: 8,9
$a -le 7 #Output:5.6.7
#其他
[DateTime]'2001-11-12' -lt [DateTime]'2020-08-01' # True。时间比较
'a' -lt 'z' # True; 'a' comes before 'z'
'macOS' -ilt 'MacOS' # False
'MacOS' -ilt 'macOS' # False。不区分大小写
'macOS' -clt 'MacOS' # True; 'm'在'M'前出现
匹配操作
匹配操作符(-like, -notlike, -match, and -notmatch)按照指定模式,寻找匹配(不匹配)的元素。-like和 -notlike支持通配符表达式(包含*,?,[])。 -match,和-notmatch则支持正则表达式。当输入的是一个串时,返回布尔值;当输入集合时,返回匹配的对象。
语法:
<string[]> -like <wildcard-expression>
<string[]> -notlike <wildcard-expression>
<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>
-like和-notlike
实例:
"PowerShell" -like "*shell" # Output: True
"PowerShell" -notlike "*shell" # Output: False
"PowerShell" -like "Power?hell" # Output: True
"PowerShell" -notlike "Power?hell" # Output: False
"PowerShell" -like "Power[p-w]hell" # Output: True
"PowerShell" -notlike "Power[p-w]hell" # Output: False
"PowerShell", "Server" -like "*shell" # Output: PowerShell
"PowerShell", "Server" -notlike "*shell" # Output: Server
-match和-notmatch
#注意不同:默认是不区分大小写。-like需要从第一个字符开始,能够匹配上
"PowerShell" -match 'shell' # Output: True
"PowerShell" -like 'shell' # Output: False
#正则表达式匹配
"PowerShell" -match '^Power\w+' # Output: True
'bag' -notmatch 'b[iou]g' # Output: True
#若输入的是一个集合,则返回匹配的成员。且$Matches变量为$null
"Rhell", "Chell", "Mel", "Smell", "Shell" -match "hell"
# Output: Rhell, Chell, Shell
#-match和-notmatch支持正则的分组匹配。
$string = 'The last logged on user was CONTOSO\jsmith'
$string -match 'was (?<domain>.+)\\(?<user>.+)'
$Matches
Write-Output "`nDomain name:"
$Matches.domain
Write-Output "`nUser name:"
$Matches.user
$Matches[0] #存放匹配的所有结果
结果:
True
Name Value
---- -----
domain CONTOSO
user jsmith
0 was CONTOSO\jsmith
Domain name:
CONTOSO
User name:
jsmith
was CONTOSO\jsmith
Replace操作
语法:
<input> -replace <regular-expression>, <substitute>
实例1:
Get-ChildItem *.txt | Rename-Item -NewName { $_.name -replace '\.txt$','.log' } #将.txt文件重命名为.log文件
#默认的-replace不区分大小写。-creplace则区分大小写。
实例2:分组匹配
#正则表达式可以使用分组匹配的模式,匹配的结果在替换部分又可以使用
$SearchExp = '^(?<DomainName>[\w-.]+)\\(?<Username>[\w-.]+)$'
#匹配两组,分别命名为DomainName,和Username
$ReplaceExp = '${Username}@${DomainName}' #在替换部分,使用匹配到的结果
'Contoso.local\John.Doe' -replace $SearchExp,$ReplaceExp
结果:
John.Doe@Contoso.local
#在双引号中,以$开头的串是变量,会替换成变量的值。在正则表达式搜索串中,$表示行末。在正则表达式替换串中,$表示匹配上的组,但要确保正则表达式放在单引号内。
$1 = 'Goodbye'
'Hello World' -replace '(\w+) \w+', "$1 Universe" #双引号内,这里是变量
# Output: Goodbye Universe
'Hello World' -replace '(\w+) \w+', '$1 Universe'#单引号内,是匹配的对象的引用
# Output: Hello Universe
#输入一个集合是,替换会作用到每个元素
"B1","B2","B3","B4","B5" -replace "B", 'a'
a1
a2
a3
a4
a5
#循环替换
"072101108108111" -replace "\d{3}", {return [char][int]$_.Value}
#每次匹配三个数字,将他们强制转换为字符。
结果:
Hello
包含操作
即使输入的是集合,包含操作符(-contains, -notcontains, -in, 和 -notin)也返回布尔值。除此之外,它们类似判等操作。首次匹配成功后,就会停止匹配(无论是否匹配到了每个元素)。
语法:
<Collection> -contains <Test-object>
<Collection> -notcontains <Test-object>
<Test-object> -in <Collection>
<Test-object> -notin <Collection>
-contains和-notcontains
区分一个集合中是是否包含特定的元素。前面是集合,后面是匹配对象。
"abc", "def" -contains "def" # Output: True,右边有一个匹配上左边即可
"abc", "def" -notcontains "def" # Output: False
"Windows", "PowerShell" -contains "Shell" # Output: False
"Windows", "PowerShell" -notcontains "Shell" # Output: True
"abc", "def", "ghi" -contains "abc", "def" # Output: False,左边的一个元素可以可以匹配到右边的模式
"abc", "def", "ghi" -notcontains "abc", "def" # Output: True
#更复杂的例子
$a = "abc", "def"
"abc", "def", "ghi" -contains $a # Output: False
$a, "ghi" -contains $a # Output: True!!!!
-in和-notin
和-contains,-notcontains的匹配对象和集合的位置刚好相反。前面是匹配对象,后面是集合。
"def" -in "abc", "def" # Output: True
"def" -notin "abc", "def" # Output: False
"Shell" -in "Windows", "PowerShell" # Output: False
"Shell" -notin "Windows", "PowerShell" # Output: True
"abc", "def" -in "abc", "def", "ghi" # Output: False
"abc", "def" -notin "abc", "def", "ghi" # Output: True
#复杂例子
$a = "abc", "def"
$a -in "abc", "def", "ghi" # Output: False
$a -in $a, "ghi" # Output: True
Type操作
type操作(-is 和 -isnot)用来判等一个对象是否是一个指定的类型。
<object> -is <type-reference>
<object> -isnot <type-reference>
实例
$a = 1
$b = "1"
$a -is [int] # Output: True
$a -is $b.GetType() # Output: False
$b -isnot [int] # Output: True
$a -isnot $b.GetType() # Output: True