|
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
$g = Get-WmiObject Win32_Process
$g[0].Name # instead of $g[0].Properties[“Name”]
| Arithmetic Binary Operators
| [table] | +
| addition, concatenation
| -
| Subtraction
| *
| multiplication, string repetition
| /
| Division
| %
| Modulus
|
[/td][/tr][tr][td=1,1,288]Array Operations
[/td][/tr][tr][td=1,1,288]Does this array have a <metricconverter w:st="on" productid="3 in">3 in</metricconverter> it
1,2,3,5,3,2 –contains 3
Return all elements equal to 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
[/td][/tr][tr][td=1,1,288]Arrays
[/td][/tr][tr][td=1,1,288]“a”,“b”,”c”
| array of strings
| 1,2,3
| array of integers
| @()
| empty array
| @(2)
| array of 1 element
| 1,(2,3),4
| array within array
| ,”hi”
| Array of one element
| $a[5]
| sixth element of array*
| $a[2][3]
| fourth element or the third
|
| element of an array
| $a[2..20]
| Return elements 3 thru 21
| · Arrays are zero based.
[/td][/tr][tr][td=1,1,288]Assignment Operators
[/td][/tr][tr][td=1,1,288]=, +=, -=, *=, /=, %=
[/td][/tr][tr][td=1,1,288]Associative Arrays (Hashtables)
[/td][/tr][tr][td=1,1,288]$hash = @{ }
| Create empty hashtable
| $h =@{foo=1;bar=2}
| Create and initialize a hashtable
| $hash.key1 = 1
| Assign 1 to key “key1”
| $hash.key1
| Returns value of key1
| $hash["key1"]
| Returns value of key1
|
[/td][/tr][tr][td=1,1,288]Boolean Values and Operators
[/td][/tr][tr][td=1,1,288]TRUE
| FALSE
| $TRUE
| $FALSE
| Any string of length > 0 except the word “false”
| Empty string or the string “false”
| Any number !=0
| Any number = 0
| Array of length > 1
| Array of length 0
| Array of length 1 whose element is TRUE
| Array of length 1 whose element is FALSE
| A reference to any object
| Null
|
[/td][/tr][tr][td=1,1,288]Break (Scripting)
[/td][/tr][tr][td=1,1,288]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;
}
[/td][/tr][tr][td=1,1,288]Command Expansion Operators
[/td][/tr][tr][td=1,1,288]$( )
| Returns null
| $(1,2,3)
| Returns an array containing1,2,3.
| $(Get-Alias a*)
| Returns evaluation of the expression
| @(Get-Alias;Get-Process)
| Executes the two commands and returns the results in an array
|
[/td][/tr][tr][td=1,1,288]Comments
[/td][/tr][tr][td=1,1,288]# 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
[/td][/tr][tr][td=1,1,288]Comparison Operators
[/td][/tr][tr][td=1,1,288]-eq
| Equal
| -ne
| Not equal
| -gt –ge
| Greater than, greater than or equal to
| -lt –le
| Less than, less than or equal to
|
“i” or “c” may be prepended to get case-insensitive or case-sensitive operations (for example, –ceq )
[/td][/tr][tr][td=1,1,288]Continue (Scripting)
[/td][/tr][tr][td=1,1,288]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.
[/td][/tr][tr][td=1,1,288]Dot Sourcing
[/td][/tr][tr][td=1,1,288]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
[/td][/tr][tr][td=1,1,288]Escape Sequences
[/td][/tr][tr][td=1,1,288]The Windows PowerShell escape character is the backwards apostrophe, or `. To make a character literal, precede it with `. To specify a ` use ``.
Special escape sequences
| `0
| (null)
| `a
| (alert)
| `b
| (backspace)
| `f
| (form feed)
| `n
| (new line)
| `r
| (carriage return)
| `t
| (tab)
| `v
| (vertical quote)
|
[/td][/tr][tr][td=1,1,288]Execution Order
[/td][/tr][tr][td=1,1,288]Windows PowerShell attempts to resolve commands in the following order: aliases, functions, cmdlets, scripts, executables, and normal files.
[/td][/tr][tr][td=1,1,288]For (Scripting)
[/td][/tr][tr][td=1,1,288][:label] for ([initializer]; [condition]; [iterator]) {}
Example:
for ($i = 0; $i –lt 5; $i++) {Write-Object $i}
[/td][/tr][tr][td=1,1,288]Foreach (Scripting)
[/td][/tr][tr][td=1,1,288][: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”}}
[/td][/tr][tr][td=1,1,288]Functions (Scripting)
[/td][/tr][tr][td=1,1,288]function MyFunction {
write-object $args[0]
}
function test ([string]$label=”default label”,[int]$start=0)
{ BEGIN {$x=$start} PROCESS {“$label: $_”’; $x++}
END{“$x total”}
}
[/td][/tr][tr][td=1,1,288]Filters (Scripting)
[/td][/tr][tr][td]padding-right: 5.4pt; border-top: #ece9d8; padding-left: 5.4pt; padding-bottom: 0cm; width: 216pt; padding |
|