Msh Language Quick Start
MSH LANGUAGE QUICK START
Arithmetic Operators (also see Unary and String operators) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Array Comparison | ||||||||||||||||||||||||||||||||||||||||||||||||||||
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: if (1, 3, 5 –eq 2) … Other operators: -gt, -le, -ge | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Arrays | ||||||||||||||||||||||||||||||||||||||||||||||||||||
* Arrays are zero-based. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Assignment Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Associative Arrays (Hashtables) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Boolean Values | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
break (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The "break" commands exits a loop. Example: while (1) { $a = something if ($a –eq 1) break; } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Command Expansion Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comments | ||||||||||||||||||||||||||||||||||||||||||||||||||||
# This is a comment. $a = "#This is not a comment…" $a = "something" # …but this is. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Comparison Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
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. | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Escape Character and Escape Sequences | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The MSH escape character is the backwards apostrophe, or `. To make a character literal, precede it with `. To specify a ` use ``.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Execution Order | ||||||||||||||||||||||||||||||||||||||||||||||||||||
MSH attempts to resolve commands in the following order: aliases, functions, cmdlets, scripts, executables, 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 pipeline or collection) {} Example: $i = 1,2,3 foreach ($z in $i) {write-object $z} | ||||||||||||||||||||||||||||||||||||||||||||||||||||
functions (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
function MyFunction { write-object $args[0] } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Filters (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
filter MyFilter { $_.name } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if/elseif/else (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
if (condition) {…} elseif (condition) {…} else {…} On the command-line, the closing brace must be on the same line as elseif and else. This restriction does not exist for scripts | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Invoke Operator | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The & operator can be used to invoke the contents of an object. Example: $a = "get-process" &$a $a = { get-process | pick-head 2 } &$a | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Logical Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Method Calls | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Methods can be called on objects. Examples: $a = "This is a string" $a.ToUpper() $a.SubString(0,3) $a.SubString(0,($a.length/2)) $a.Substring(($a.length/2), ($a.length/3)) Static methods may be called as well: [string].format("{0} {1} {2}","one",2,(get-date)) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
MSH Variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Object Properties | ||||||||||||||||||||||||||||||||||||||||||||||||||||
An object’s properties can be referenced directly with the "." operator. $a = get-date $a.Date $a.TimeOfDay.Hours | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Operator Precedence | ||||||||||||||||||||||||||||||||||||||||||||||||||||
In MSH, operators are evaluated in the following precedence: () {}, \@ $, !, [ ], ., &, ++ --, Unary + -, * / %, Binary + -, Comparison Operators, -and –or, |, > >>, = | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Redirection | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The > and >> operators redirect command output to files. The > operator creates a new file or truncates and existing one, while the >> operator appends to an existing file. Example: 1,2,3 >foo.txt 5,6 >>foo.txt | ||||||||||||||||||||||||||||||||||||||||||||||||||||
return (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
The return command exits the current script or function and returns a value. Example: function foo { ... } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Script Blocks | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Commands and expressions can be stored in a script block object and executed later. Example: $block = {get-process; $a=1} &$block | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Scripts | ||||||||||||||||||||||||||||||||||||||||||||||||||||
MSH commands can be stored in and executed from script files. The file extension for MSH scripts is ".msh". Parameters can be passed to a script and a script can return a value. Example: $sum = MyAdder.msh 1 2 3 | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Strings and String Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
String constants: "this is a string, this $variable is expanded" ‘this is a string, this $variable is not expanded’
Examples: MSH> "test" + "this" testthis MSH> "{0:M}" -f $(get-date) June 02 MSH> $a = 1,2,3,4 MSH> $a 1 2 3 4 MSH> $OFS = ":" MSH> "$a" 1:2:3:4 MSH> "This is a test" -replace "is","IS" ThIS IS a test | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Switch | ||||||||||||||||||||||||||||||||||||||||||||||||||||
$a = 3 switch ($a) { 1 {"got one"} 2 {"got two"} 3 {"got three"} } $var = "word2" switch -regex ($var) { "word2" {"Multi-match Exact " + $_ } "word.*" {"Multi-match Exact1 " + $_ } default {"Multi-match Default " + $_; break} "w.*" {"Previous Break terminated the matching"} } $var = "word1","word2","word3" switch -regex ($var) { "word1" {"Multi-match Exact " + $_ ; continue} "word2" {"Multi-match Exact " + $_ ; continue} default {"Multi-match Default " + $_; continue} } | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Trap | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Execute a block of code in a terminating error condition. Example: function handler1 { write-host "Hi, I'm a trap handler" } function handler2 { write-host "Hi, I'm a trap handler2" } trap [System.Management.Automation.ExecutionFailedException] { handler2 ; continue } trap [System.Management.Automation.ExecutionBreakOnErrorException] { handler1 ; continue } get-content thisisabadfilename -errorp notifystop set-location thisisabadlocation | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Types & Casts | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Brackets around a string indicate a type object
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Unary Operators | ||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||
Variables | ||||||||||||||||||||||||||||||||||||||||||||||||||||
Format: $[scope:]name Examples: $a = 1 $global:a = 1 $local:a = 1 $env:path = "d:\windows" Scope may be either global, local or script | ||||||||||||||||||||||||||||||||||||||||||||||||||||
while (scripting) | ||||||||||||||||||||||||||||||||||||||||||||||||||||
[:label] while (condition) { … } |