1.Variable declaration
所有的变量都必须以$开始
Example:
[int]$valA = 2; [int]$valB = 3; [int]$valC = 0; $valC = $valA + $valB; write-host("The sum is: " + $valC);
2.Arrays
Example:
$myArray = 1,2,3,4,5,6,7,8,9,10; [int] $sum = 0; foreach ($val in $myArray) { write-host ("Index with value:$val"); $sum = $sum + $val; } Write-host(“The sum is: $sum”);
3.Associative Arrays 类似于C++ map, key,value pair.
## Declaration syntax: # $<arrayname> = @{<keyName = Value>;…} $countries = @{'88' = 'Bangladesh'; '44' = 'United Kingdom'; '11' = 'United States'; '1' = 'Canada'}; $countries
4.Loops, 支持for, foreach, while三种循环
for ($i=0; $i<10;$i++) {Write-Host $i} $i = 1 for(;;) { Write-Host $i }
$letterArray = "a","b","c","d" foreach ($letter in $letterArray) { Write-Host $letter }
while($val -ne 3) { $val++ Write-Host $val }
各种操作符:
逻辑操作符, 与 或 非
Operator |
Description |
Example |
Results |
-and |
logical and |
(A -eq A) -and (A -eq B) |
false |
-or |
logical or |
(A -eq B) -or (A -eq A) |
true |
-not |
logical not |
(1 -eq 1) -and -not (2 -gt 2) |
true |
! |
logical not |
(1 -eq 1) -and !(2 -gt 2) |
true |
比较操作符:
-eq |
Equal (case insensitive) |
-ne |
Not equal (case insensitive) |
-ge |
Greater than or equal (case insensitive) |
-gt |
Greater than (case insensitive) |
-lt |
Less than (case insensitive) |
-le |
Less than or equal (case insensitive) |
-like |
Wildcard comparison (case insensitive) |
-notlike |
Wildcard comparison (case insensitive) |
-match |
Regular expression comparison (case insensitive) |
-notmatch |
Regular expression comparison (case insensitive) |
-replace |
Replace operator (case insensitive) |
-contains |
Containment operator (case insensitive) |
-notcontains |
Containment operator (case insensitive) |
-ieq |
Case insensitive equal |
-ine |
Case insensitive not equal |
-ige |
Case insensitive greater than or equal |
-igt |
Case insensitive greater than |
-ile |
Case insensitive less than or equal |
-ilt |
Case insensitive less than |
-ilike |
Case insensitive equal |
-inotlike |
Case insensitive equal |
-imatch |
Case insensitive regular expression comparison |
-inotmatch |
Case insensitive regular expression comparison |
-ireplace |
Case insensitive replace operator |
-icontains |
Case insensitive containment operator |
-inotcontains |
Case insensitive containment operator |
-ceq |
Equal (case sensitive) |
-cne |
Not equal (case sensitive) |
-cge |
Greater than or equal (case sensitive) |
-cgt |
Greater than (case sensitive) |
-clt |
Less than (case sensitive) |
-cle |
Less than or equal (case sensitive) |
-clike |
Wildcard comparison (case sensitive) |
-cnotlike |
Wildcard comparison (case sensitive) |
-cmatch |
Regular expression comparison (case sensitive) |
-cnotmatch |
Regular expression comparison (case sensitive) |
-creplace |
Replace operator (case sensitive) |
-ccontains |
Containment operator (case sensitive) |
-cnotcontains |
Containment operator (case sensitive) |
-is |
Is of a type |
-isnot |
Is not of a type |
-as |
Is a type, no error if conversion fails |
if (($val1 -eq $val2) -and ! ($val1 = "Some value")){ Write-Host("One condition satisfied."); }else{ Write-Host("Condition not satisfied."); }
Function: