<<

The Complete Guide to PowerShell Punctuation  Does not include special characters in globs (about_Wildcards) or regular expressions (about_Regular_Expressions) as those are separate “languages”.  Green items are placeholders indicating where you insert either a single word/character or, with an ellipsis, a complex expression.

Symbol What it is Explanation Symbol What it is Explanation line break Allowed between statements, within strings, after these <#... Multi-line Everything between the opening and closing tokens— carriage separators [ | , ; = ] and—as of V3—these [ . :: ]. comment may span multiple lines—is a comment. return Also allowed after opening tokens [ { [ ( ' " ] . #> Not allowed most anywhere else. call operator Forces the next thing to be interpreted as a command statement Optional if you always use line breaks after statements; & even if it looks like a string. So while either Get- ; required to put multiple statements on one line, e.g. semicolon separator ampersand ChildItem or & Get-ChildItem do the same thing, $a = 25; -Output $a "Program Files\stuff.exe" just echoes the string name variable prefix $ followed by letters, numbers, or underscores specifies a literal, while & "Program Files\stuff.exe" will $ variable name, e.g. $width. Letters and numbers are not execute it. dollar sign limited to ASCII; some 18,000+ chars are eligible. (a) line As the last character on a line, lets you continue on the variable prefix To embed any other characters in a variable name enclose ` continuation next line where PowerShell would not normally allow a ${...} it in braces, e.g ${save-items}. See about_Variables. back tick; line break. Make sure it is really last—no trailing spaces! grave accent See about_Escape_Characters accessor Special case: ${drive-qualified path} lets you, e.g., ${path} store to (${C:tmp.txt}=1,2,3) or retrieve from (b) literal Precede a dollar sign to avoid interpreting the following character characters as a variable name; precede a quote mark ($data=${C:tmp.txt}) a file. See Provider Paths. inside a string to embed that quote in the string instead of (a) grouping Wrap any single statement (or single command-stream ending the string. See about_Escape_Characters (...) expression connected by pipes) to override default precedence rules. (c) special Followed by one of a set of pre-defined characters, allows See the subexpression operator for multiple $() character inserting special characters, e.g. `t = tab, `r = carriage commands. return, `b = backspace. See about_Special_Characters Group the front: access a property from the result of an operation, e.g. (get- -name win*).name '...' literal string String with no interpolation; typically used for single-line Group at the end: pass the result of an operation as an single quote strings but can be used for multi-line as well. argument: write-output (1,2,3 -join '*') "..." interpolated String with interpolation of variables, sub-expressions, (b) grouping Override operator precedence: double quote string escapes, and special characters (e.g. `t). See operator e.g. 8 + 4 / 2 vs. (8 + 4)/2 about_Escape_Characters and about_Special_Characters (c) . Unlike when calling native PowerShell functions, calling @' literal A multi-line string with no interpolation; differs from a function arg .NET functions require parentheses: ... here-string normal string in that you can embed single quotes within container $hashTable.ContainsKey($x) '@ the string without doubling or escaping. $(...) (a) sub- Wrap multiple statements, where the output of each interpolated A multi-line string with interpolation; differs from a normal expression contributes to the total output: $($x=1;$y=2;$x;$y) @"... here-string string in that you can embed double quotes within the (b) sub- Interpolate simple variables in a double-quoted string with string without doubling or escaping. expression just $, but complex expressions must be wrapped in a "@ inside a string subexpression. Ex: $p = ps | select –first 1 then command Pipe output of one command to input of next, "proc name is $($p.name)" | pipe connector e.g. ps | select ProcessName array sub- Same as a sub-expression, except this returns an array divert to file / Redirects & overwrites (if file exists) stdout stream to a file @(...) expression even with zero or one objects. Many cmdlets return a > overwrite (e.g. ps > process_list.txt). See about_Redirection array collection of a certain , say X. If two or more, it is greater than returned as an array of X whereas if you only get one It’s a “greater than” symbol but it doesn’t do comparisons: object then it is just an X. Wrapping the call with this for algebraic operators use -gt or -lt, e.g. ($x -lt $y). operator forces it to always be an array, e.g. $a = @(ps divert to file / Redirects & overwrites (if file exists) numbered stream (2 | where name -like 'foo') See about_Arrays n> overwrite thru 5) or all streams (use *) to a file e.g. ps 4> hash initializer Defines a hash table with the format process_list.txt @{...} @{ name1=value1; name2=value2; ...}. Example: divert to file / Redirects & appends stdout stream to a file, e.g. hash $h = @{abc='hello'; color='green'}. You can >> append ps >> process_list.txt. See about_Redirection then access values by their keys, e.g. $h['color'] or divert to file / Redirects & appends numbered stream (2 thru 5) or all . See about_Hash_Tables $h.color n>> append streams (use *) to a file, e.g. ps *>> out.txt {...} script block Essentially an anonymous function. Ex: output redirect Redirects an output stream (2 thru 5) to stdout stream, $sb = {param($color="red"); "color=$color"} n>&1 to stdout effectively merging that stream with stdout. Ex: to merge braces then . See about_Script_Blocks & $sb 'blue' errors with stdout: Do-SomethingErrorProne 2>&1 (a) array indexer returns the 5th element of the array. [...] $data[4] $data assignment Assign a value to a variable, e.g. $stuff = 25 or brackets (b) hash indexer $hash['blue'] returns the value associated with key = operator $procs = ps | select -first 5. Use -eq or -ne for 'blue' in the hash (though you could also use $hash.blue) equals equality operators: ("ab" -eq $x) or ($amt -eq 100). (c) static type Use to call a static methods, e.g. [Regex]::Escape($x) ! Logical not Negates the statement or value that follows. Equivalent to (d) type cast Cast to a type just like C# ([int]"5.2") but in PS you can exclamation the -not operator. if ( !$canceled ) ... also cast the variable itself ([xml]$x=''). Also (a) add Adds numbers, e.g. ($val + 25). applies for function args: function f([int]$i) {...} + plus (b) concatenate Concatenates strings, arrays, hash tables, e.g. ('hi'+'!'). (e) array type Cast to an array type—use with no content inside: (c) nested class Typically best practice says not to have public nested designator function f([int[]] $values) {...}. access classes but when needed you need a plus to access, e.g. pipeline object This special variable holds the current pipeline object (now [Net.WebRequestMethods+Ftp] See Plus (+) in .NET $_ with a more friendly as well, $PSItem), Class Names e.g. ps | where { $_.name -like 'win*' } add & store Common shorthand identical to that in C#: $x += 5 is splatting prefix Allows passing a collection of values stored in a hash table += shorthand for $x = $x + 5. Can also be used for @name or in an array as parameters to a cmdlet. Particularly compound concatenation as described under plus and concatenation splat useful to forward arguments passed in to another call with assignment direct to a path: ${c:output.txt) += 'one','two' or . See about_Splatting @Args @PsBoundParameters (a) negate Negate a number (-$val). alias for Instead of you Get-Stuff | Where-Object { ... } - (b) subtract Subtract one number from another ($v2 - 25.1). ? Where-Object can write the oft-used cmdlet with the terse alias: hyphen question Get-Stuff | ? { ... } (c) operator Prefixes lots of operators: logical (-and, -or, -not), mark prefix comparision (-eq, -ne, -gt, -lt, -le, -ge), %{...} Alias for Instead of 1..5| ForEach-Object { $_ * 2 } you bitwise (-bAND, -bOR, -bXOR, -bNOT), and more. ForEach-Object can write the oft-used cmdlet as: 1..5| % { $_ * 2 } (d) verb/noun Separates the verb from the noun in every cmdlet, e.g. (a) alias for Special case of above for a single property of pipeline separator Get-Process. % ForEach-Object input: ls| % name is equivalent to ls| % { $_.name} percent subtract & Common shorthand identical to that in C#: $x -= 5 is (b) modulo Returns the remainder of a division e.g. (7 % 2) returns 1. -= store shorthand for $x = $x - 5. modulo & store Common shorthand identical to that in C#: $x %= 5 is (a) multiply Multiply numbers, e.g. ($val * 3.14). %= shorthand for $x = $x % 5. * (b) replicate Replicate arrays, e.g. ('a','b' * 2). (a) drive Just like conventional Windows drives ( , etc.) you asterisk C:\ Common shorthand identical to that in C#: is : designator can use dir alias: to see the contents of the alias drive multiply & $x *= 5 *= store shorthand for $x = $x * 5. Can also be used for or $:path to see the $path variable on the env drive. replication as described under asterisk and replication (b) variable An undecorated variable, e.g. $stuff implicitly specifies direct to a path: ${c:output.txt) *= 3 scope specifier the current scope. But you can also reference divide Divide numbers, e.g. ($val / 3.14). $script:stuff or $global:stuff to specify a / different scope. See about_Scopes virgule divide & store Common shorthand identical to that in C#: $x /= 5 is static member Specify a static .NET method, e.g. [String]::Join(...) /= :: shorthand for $x = $x / 5. accessor or [System.IO.Path]::GetTempFileName(), or a increment Auto-increment a variable: increment then return value double colon static property [System.Windows.Forms.Keys]::Alt ++ (++$v) or return value then increment ($v++). or [int]::MaxValue. decrement Auto-decrement a variable: decrement then return value array builder Specify an array to feed a pipeline, e.g. 1,3,5,7 | -- (++$v) or return value then decrement ($v++). , ForEach-Object { $_ * 2 } or specify an array comma argument, ps -name winword,spoolsv stop parsing Inserted in the midst of a statement, PowerShell treats any --% or verbatim arguments after it as literals except for DOS-style (a) separator in E.g. System.IO.FileInfo just as in C# parameter environment variables (e.g, %PATH%). See about_Parsing . class path period; Get the last token in the previous line. (b) property / Specify property of simple object $myArray.Length or $$ method complex one (ps | ? Name -like "win*").name or $^ Get the first token in the previous line. dereference method $hashTable.ContainsKey($x) Execution status of the last operation ($true or $false); (c) dot-source Load a PowerShell file into the current scope $? contrast with $LastExitCode that reports the code operator (e.g. . myScript.ps1) rather than into a subshell. of the last Windows-based program executed. .. range operator Initialize an array (e.g. $a = 1..10) or return an array double dot slice ($a[3..6]). References (a) comment Everything through the end of the line is a comment. about_Automatic_Variables, about_Preference_Variables, about_Operators, # about_Environment_Variables, about_Quoting_Rules, When to Quote in PowerShell, octothorp (b) history On the command-line, you can type # to recall the recall last command for editing. Also, #string recalls the Copyright © 2015 Michael Sorens  2015.10.23  Version 1.0.3 last command containing string; subsequent tabs continue through the history stack. (Since V2) Published on Simple-Talk.com at http://bit.ly/1JJtp4v