Windows PowerShell Language Quick Reference
Native Support for Different Type Systems | ||||||||||||||||||||
Windows PowerShell adapts WMI, XML, ASDI, <city w:st="on"><place w:st="on">ADO</place></city>, and COM objects to provide a common syntax to access their properties and methods. Example
| ||||||||||||||||||||
Arithmetic Binary Operators | ||||||||||||||||||||
| ||||||||||||||||||||
Array Operations | ||||||||||||||||||||
Does this array have a <metricconverter w:st="on" productid="3 in">3 in</metricconverter> it 1,2,3,5,3,2 –contains 3
1,2,3,5,3,2 –eq 3
Return all elements less than 3: 1,2,3,5,3,2 –lt 3
Test if 2 exists in collection: if (1, 3, 5 –contains 2) … Other operators: -gt, -le, -ge, -ne
| ||||||||||||||||||||
Arrays | ||||||||||||||||||||
· Arrays are zero based.
| ||||||||||||||||||||
Assignment Operators | ||||||||||||||||||||
=, +=, -=, *=, /=, %=
| ||||||||||||||||||||
Associative Arrays (Hashtables) | ||||||||||||||||||||
| ||||||||||||||||||||
Boolean Values and Operators | ||||||||||||||||||||
| ||||||||||||||||||||
Break (Scripting) | ||||||||||||||||||||
The break commands exits a loop. It can take an optional LABEL to break to Example: while (1) { $a = something if ($a –eq 1) break; }
| ||||||||||||||||||||
Command Expansion Operators | ||||||||||||||||||||
| ||||||||||||||||||||
Comments | ||||||||||||||||||||
# This is a comment because # is the first char of a token $a = “#This is not a comment…” $a = “something” # …but this is. Write-Host Hello#world
| ||||||||||||||||||||
Comparison Operators | ||||||||||||||||||||
“i” or “c” may be prepended to get case-insensitive or case-sensitive operations (for example, –ceq ) | ||||||||||||||||||||
Continue (Scripting) | ||||||||||||||||||||
The continue statement continues the next iteration of a loop without breaking out of it. Example: while (1) { $a = something if ($a –eq 1) (continue) # This line is not reached unless $a == 1 } # This line is never reached. | ||||||||||||||||||||
Dot Sourcing | ||||||||||||||||||||
Dot sourcing allows running functions, script blocks, and scripts in the current scope rather than a local one. Example: . MyFunction
If MyFunction sets a variable, it is set in the current scope rather than the function’s local scope. $a = {$x = Get-Process | Select –First 2} . $a #Evaluates the script block in the current scope
| ||||||||||||||||||||
Escape Sequences | ||||||||||||||||||||
The Windows PowerShell escape character is the backwards apostrophe, or `. To make a character literal, precede it with `. To specify a ` use ``.
| ||||||||||||||||||||
Execution Order | ||||||||||||||||||||
Windows PowerShell attempts to resolve commands in the following order: aliases, functions, cmdlets, scripts, executables, and normal files.
| ||||||||||||||||||||
For (Scripting) | ||||||||||||||||||||
[:label] for ([initializer]; [condition]; [iterator]) {}
Example:
for ($i = 0; $i –lt 5; $i++) {Write-Object $i}
| ||||||||||||||||||||
Foreach (Scripting) | ||||||||||||||||||||
[:label] foreach (identifier in collection) {} Expression | foreach {} Expression | foreach {BEGIN{} PROCESS{} END{}}
Examples:
$i = 1,2,3 foreach ($z in $i) {Write-Object $z} Get-Process |foreach {BEGIN{$x=1} PROCESS{$X++} END{“$X Processes”}}
| ||||||||||||||||||||
Functions (Scripting) | ||||||||||||||||||||
function MyFunction { write-object $args[0] }
function test ([string]$label=”default label”,[int]$start=0) { BEGIN {$x=$start} PROCESS {“$label: $_”’; $x++} END{“$x total”} } | ||||||||||||||||||||
Filters (Scripting) | ||||||||||||||||||||