When PowerShell parses an input, it starts in expression mode. When a parsed token refers to a
command invocation, parsing mode switches to argument mode.
When being in argument mode, there are a set of tokens that are evaluated in expression mode.
Argument mode
In argument mode, the following characters and syntaxes have a special meaning:
$variable | Only if name of variable exists |
'verbatim' and "expandable" strings | |
(…) | Parenthesis enclose an expression (which are evaluated in expression mode again) |
$( … ) | The subexpression operator |
{ … } | A script block |
@ | An initial at-sign starts splatting, arrays or hash tables |
, | commas create arrays |
A token that is not recognized as one of the listed above is assumed to be an
expandable string (that is: strings with double quotes rather than single quotes, see also
this StackOverflow answer)
Example of a consequence of the differing parsing modes
For example, in the following example, the token
[System.Text.Encoding]
is not recognized as a type reference to
System.Text.Ecoding
, rather, the second argument to the constructor of
System.IO.StreamWriter
is passed the string
"[System.Text.Encoding]::UTF8"
.
$outStream = new-object System.IO.StreamWriter some.file, [System.Text.Encoding]::UTF8
In expression mode, [System.Text.Encoding]
would have been recognized as a type reference.