1.算术运算符+-*/%(和.NET的不太一样,不仅在数字上面,也可以放在String,数组上面执行)
+: 2+4->6; "hi"+"there"->"hithere"
* 2*3->6 ; "2"*3->"222"
加法运算符号:
符合“左手系规则”:即 a+b不一定等于b+a
The “left-hand” rule for arithmetic operators: The type of the left-hand operand
determines the type of the overall operation. This is an important
rule to remember.
比如:2+"123" - >125; 而"123"+2->"1232"
现在离圣诞节还有多少时间:$now=[datetime]([string](([datetime]::now).Year)+"-12-25")-[datetime]::now
2.分配操作符:=,-=,+=,*=等
3.比较运算符:默认为大小写不敏感("abc" - eq "Abc" -> True);如果要敏感的话,则添加c前缀("abc" -ceq "Abc"- > False)
Operator Description Example Result
-eq Equals(=) 5 –eq 5 $true
-ne Not equals(!=) 5 –ne 5 $false
-gt Greater than(>) 5 –gt 3 $true
-ge Greater than or equal (>=) 5 –ge 3 $true
-lt Less than(<) 5 –lt 3 $false
-le Less than or equals(<=) 5 –le 3 $false
-contains The collection on the left side contains the value specified on the right side(包括). 1,2,3 –contains 2 $true
-notcontains The collection on the left side does not contain the value on the right side(不包括). 1,2,3 –notcontains 2 $false
不同类型的变量比较时,同样也遵守“左手系规则”
比如: "001" -eq 1 ->False; 1 -eq "001" ->True
数组比较,依次返回符合条件的记录
If the left operand is an array or collection, then the comparison operation will return the elements of that collection which match the right
operand.
比如:1,2,3,2,4 -eq 2 返回
2
2
4.通配符*,?的使用,还有强大的正则表达式
5.逻辑运算符号:-and, -or
6.类型转换:-is -isnot -as :
[int] "abc" -eq $null,出现Exception; 而"abc" -as [int] -eq $null返回True;
1.判断语句if/elseif/else
if ($x -gt 100)
{
"It's greater than one hundred"
}
elseif ($x -gt 50)
{
"It's greater than 50"
} else
{
"It's not very big."
}
elseif 是一个关键字,没空格;括号()是必须的,即使只有一条语句
if ($x –gt 100) "It's greater than one hundred" // 编译错误的
2.循环语句while/for/foreach
$val = 0
while($val -ne 3)
{
$val++
write-host "The number is $val"
}
for ($i=0; $i -lt 5; $i++) { $i }
$l = 0; foreach ($f in dir *.txt) { $l += $f.length }
3.switch
PS (5) > switch (2) {1 {"One"} 2 {"two"; break} 2 {"another 2"}}
two