Home
POV-Ray
PowerShell pipeline
PowerShell scripts
PoSh punctuation
Contact
Sitemap
Links

 

PowerShell: Punctuation in the Shell

 ` (backtick)
escape character. It removes the special meaning of any character that follows it.
For example, a space is normally a separator, which is why cd c:\Program Files generates an error. Escaping the space, cd c:\Program` Files, forces the space to be treated as a literal.

() (parentheses)
define an order of execution. PowerShell will execute parenthetical commands first, from the innermost parentheses to the outermost.
This is a good way to run a command and have its output feed the parameter of another command:
Get-Service -computerName (Get-Content c:\computernames.txt)

[] (square brackets)

o    contain the index number when you want to refer to a single object within an array or collection: $services[2] gets the third object from $services (indexes are always zero-based).

o    contain a data type when you are casting a piece of data as a specific type.
For example, $myresult / 3 -as [int] casts the result as a whole number (integer), and [xml]$data = Get-Content data.xml will read the contents of Data.xml and attempt to parse it as a valid XML document.

{} (curly braces)

o    contain blocks of executable code or commands, called script blocks.
These are often fed to the parameters that expect a script block, or a filter block:
Get-Service | Where-Object { $_.Status -eq 'Started' }

o    contain the key-value pairs that make up a new hashtable. The opening brace is always preceded by an @ sign. Notice that in this example I’m using braces both to enclose the hashtable key-value pairs (of which there are two), and to enclose an expression script block, which is the value for the second key, “e:”
$hashtable = @{l='Label';e={expression}}

o    when a variable name contains spaces, braces must surround the name: ${My Variable}.

'' (single quotation marks)
contain string values. PowerShell does not look for escape character or variables, inside single quotes.

"" (double quotation marks)
contain string values. PowerShell looks for escape characters and the $ character inside double quotes. Escape characters are processed, and the characters following a $ symbol (up to the next whitespace) are taken as a variable name and the contents of that variable substituted.
For example, if the variable $one contains the value “World,” then $two = "Hello $one `n" will contain “Hello World” and a carriage return (`n is a carriage return).

$ (dollar sign)
precedes variable name: characters following a $ sign, till next white space, form a variable name.
This can be tricky when working with the cmdlets that manage variables.
Supposing that $one contains the value “two,” then
New- Variable -name $one -value 'Hello' will create a new variable named two, with the value “Hello”, because the dollar sign tells the shell that you want to use the contents of $one.
New-Variable -name one -value 'Hello' would create a new variable $one.

% (percent sign)
alias to the ForEach-Object cmdlet.

? (question mark)
alias to the Where-Object cmdlet.

> (right angle bracket)
sort of alias to the Out-File cmdlet.
It’s not technically a true alias, but it does provide for Cmd.exe-style file redirection: dir > files.txt.

+ - * / (math operators)
standard arithmetic operators.

- (dash)

o    precedes parameter names and operators, such as - computerName or -eq.

o    separates the verb and noun components of a cmdlet name, as in Get-Content.

o    subtraction arithmetic operator.

@ (at sign)

o    precedes curly braces, enclosing a hashtable

o    precedes parentheses, enclosing a comma-separated list of values that form an array:
$array = @(1,2,3,4). However, both the @ sign and the parentheses are optional, since the shell will normally treat any comma-separated list as an array anyway.

o    denotes a here-string, which is a block of literal string text.
A here string starts with @" or @' and ends with "@ or '@. The closing mark must be on the beginning of a new line. Run help about_quoting_rules for more information and examples.

o    splat operator. If you construct a hashtable where the keys match parameter names, and those keys’ values are the parameters’ values, then you can splat the hashtable to a cmdlet. http://bartdesmet.net/blogs/bart/archive/2008/03/24/windows-powershell-2-0-feature-focus-splat-split-and-join.aspx provides a good example of splatting.

& (ampersand)
invocation operator, instructing the shell to treat something as a command and to run it.
For example, $a = "Dir" places the string “Dir” into the variable $a; & $a will run the Dir command.

; (semicolon)
command separator, separates independent commands that are included on a single line:
Dir ; Get-Process will run Dir, and then Get- Process.
The results are sent to a single pipeline, but the results of Dir are not piped to Get-Process.

# (pound sign or hash mark):
 comment character. Any characters following #, till next carriage return, are ignored by the shell.

= (equal sign)
assignment operator, used to assign a value to a variable: $one = 1.
This is not used for quality comparisons; use -eq instead.

| (pipe)
conveys the output of one cmdlet to the input of another. The second cmdlet (the one receiving the output) uses pipeline parameter binding to determine which parameter or parameters will actually receive the piped-in objects.

\ or / (backslash or slash)
A forward slash is used as a division operator in mathematical expressions; either the forward or backslash can be used as a path separator in file paths: C:\Windows is the same as C:/Windows.

. (period)
indicates that you want to access a member, such as a property or method, or an object: $_.Status will access the Status property of whatever object is in the $_ placeholder.

, (comma)
item separator: outside of quotation marks, separates the items in a list or array: "One",2,"Three",4. Can be used to pass multiple static values to a parameter that can accept them:
Get-Process -computername Server1,Server2,Server3.

:~^ (colon, tilde, caret):
the only punctuation on a US keyboard that PowerShell doesn’t actively use for something are the colon (:), tilde (~), and caret (^), although those do get used in regular expressions.

 

PowerShell: Punctuation in the Help File

Punctuation within the help file takes on slightly different meanings:

[] (square brackets)

o    that surround any text are indicating that the text is optional. That might include an entire command ([-Name <string>]), or it might indicate that a parameter is positional and that the name is optional ([-Name] <string>). It can also indicate both: That a parameter is optional, and if used, can be used positionally ([[-Name] <string>]). It is always legal to use the parameter name, if you’re in any doubt.

o    Adjacent square brackets [] indicate that a parameter can accept multiple values (<string[]> instead of <string>).

<> (angle brackets)

surround data types and indicate what kind of value or object a parameter expects:

<string>, <int>, <process>, and so forth.

 

 

to Top of Page

FurBall Productions | furball@casema.nl