PowerShell~语法与运算符

基本语法

变量:$开头

$a = "Hello"

条件语句:if else

if ($price -eq 2) 
{
 Write-Host "偶数"
}
else{
 Write-Host "基数" 
}
     

循环语句:for

 for($i=0;$i -lt 3;$i++) 
{ Write
-Host "`$i=$i" }

比较运算符

  示例 结果
-eq 等于。包含一个相同的值。 1 -eq 1
"abc” -eq “abc”
1,2,3 -eq 2
1,2,3,1,2,3,1,2,3 –eq 2
"abc”,"def” -eq “abc
true
true
2
2,2,2
abc
-ne 不等于。包含一个不同的值。 1 -ne 1
1,2,3 –ne 1
"abc”,"def” –ne “abc”
false
2,3
def
-gt (>)大于 1 -gt 1 false
-ge (>=)大于或等于 1 -ge 1 true
-lt (<)小于 1 -lt 1 false
-le (<=)小于或等于 1 -le 1 true
-like 使用通配符 (*) 匹配。 “abc” -like “*b*” true
-notlike 使用通配符 (*) 不匹配。 “abc” -notlike “*b*” false
-match 当运算符的输入(左侧参数)是一个单标量对象时,-match 和 -notmatch 运算符填充 $Matches 自动变量。当输入是标量时,-match 和 notmatch 运算符返回布尔值,并将 $Matches 自动变量 的值设置为参数的匹配项目。 如果输入是一个集合,则 -match 和 -notmatch 运算符返回该集合中的匹配成员,但运算符不会填 充 $Matches 变量。 123 -match “^[\d]+$”




"abc” -match “^[\w]+$”




"abc”,123 –match “^[\d]+"$”
true
($matches为123)



true
($matches为abc)


123
($matches为空)
-notmatch 正则表达式比较 123 -notmatch “^[\d]+$” false
-contains 包含运算符。包含一个相同的完整值(而不是值的一部分)。始终返回布尔值。 "abc”,"def” -contains “abc”
"abc”,"def” -contains “ab”
"abc”,"def” -contains “ab*”
true
false
false
-notcontains 包含运算符。不包含一个相同值。始终返回布尔值。 "abc”,"def” -notcontains “abc” false
-replace 替换,支持正则表达式 “abc“ -replace “b”,"-"
"a1b2c3” -replace “\d”,"-"
a-c
a-b-c-

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

逻辑运算符

 

  说明 示例 结果
-and 逻辑与。
仅当两条语句都为 TRUE 时才为 TRUE。
$true -and $false
$true -and $true
(1 -eq 1) -and (1 -eq 2)
false
true
false
-or 逻辑或。
当其中一条语句为 TRUE或两条语句都为 TRUE 时为 TRUE。
$true -or $false
(1 -eq 1) -or (1 -eq 2)
true
true
-xor 逻辑异或。
仅当一条语句为 TRUE而另一条语句为 FALSE 时才为 TRUE。
$true -xor $false
$true -xor $true
true
false
-not
!
逻辑非。对后面的语句取反。 -not $true
!$true
false
false
posted @ 2017-06-07 13:36  张占岭  阅读(2390)  评论(0编辑  收藏  举报