Describes how PowerShell parses commands.
When you enter a command at the command prompt, PowerShell breaks the command text into a series of segments called tokens and then determines how to interpret each token.
For example, if you type:
PowerShell breaks the command into two tokens,
interprets each token independently using one of two major parsing modes:
expression mode and argument mode.
As PowerShell parses command input it tries to resolve the command names to
cmdlets or native executables. If a command name does not have an exact
match, PowerShell prepends
Get- to the command as a default verb. For
example, PowerShell parses
Get-Process. It's not
recommended to use this feature for the following reasons:
- It's inefficient. This causes PowerShell to search multiple times.
- External programs with the same name are resolved first, so you may not execute intended cmdlet.
Get-Commanddon't recognize verb-less names.
Expression mode is intended for combining expressions, required for value manipulation in a scripting language. Expressions are representations of values in PowerShell syntax, and can be simple or composite, for example:
Literal expressions are direct representations of their values:
Variable expressions carry the value of the variable they reference:
Operators combine other expressions for evaluation:
- 12, -not $Quiet, 3 + 7, $input.Length -gt 1
- Character string literals must be contained in quotation marks.
- Numbers are treated as numerical values rather than as a series of characters (unless escaped).
- Operators, including unary operators like
-notand binary operators like
-gt, are interpreted as operators and apply their respective operations on their arguments (operands).
- Attribute and conversion expressions are parsed as expressions and applied
to subordinate expressions, e.g.
- Variable references are evaluated to their values but splatting (i.e. pasting prefilled parameter sets) is forbidden and causes a parser error.
- Anything else will be treated as a command to be invoked.
When parsing, PowerShell first looks to interpret input as an expression. But when a command invocation is encountered, parsing continues in argument mode.
Argument mode is designed for parsing arguments and parameters for commands in a shell environment. All input is treated as an expandable string unless it uses one of the following syntaxes:
- Dollar sign (
$) begins a variable reference (only if it is followed by a valid variable name, otherwise it is interpreted as part of the expandable string).
- Quotation marks (
") begin string values
- Parentheses (
)) demarcate new expressions.
- Subexpression operator (
)) demarcates embedded expressions.
- Braces (
}) demarcate new script blocks.
- Initial at sign (
@) begins expression syntaxes such as splatting (
@args), arrays (
@(1,2,3)) and hash tables (
- Commas (
,) introduce lists passed as arrays, except when the command to be called is a native application, in which case they are interpreted as part of the expandable string. Initial, consecutive or trailing commas are not supported.
Values of embedded expressions are converted to strings.
The following table provides several examples of values processed in expression
mode and argument mode and the evaluation of those values. We assume that the
value of the variable
Every token can be interpreted as some kind of object type, such as Boolean or string. PowerShell attempts to determine the object type from the expression. The object type depends on the type of parameter a command expects and on whether PowerShell knows how to convert the argument to the correct type. The following table shows several examples of the types assigned to values returned by the expressions.
||argument||':A B' (string)|
The stop-parsing symbol (
--%), introduced in PowerShell 3.0, directs
PowerShell to refrain from interpreting input as PowerShell commands or
When calling an executable program in PowerShell, place the stop-parsing symbol before the program arguments. This technique is much easier than using escape characters to prevent misinterpretation.
When it encounters a stop-parsing symbol, PowerShell treats the remaining
characters in the line as a literal. The only interpretation it performs is to
substitute values for environment variables that use standard Windows notation,
The stop-parsing symbol is effective only until the next newline or pipeline
character. You cannot use a continuation character (
`) to extend its
effect or use a command delimiter (
;) to terminate its effect.
For example, the following command calls the Icacls program.
icacls X:\VMS /grant Dom\HVAdmin:(CI)(OI)F
To run this command in PowerShell 2.0, you must use escape characters to prevent PowerShell from misinterpreting the parentheses.
icacls X:\VMS /grant Dom\HVAdmin:`(CI`)`(OI`)F
Beginning in PowerShell 3.0, you can use the stop-parsing symbol.
icacls X:\VMS --% /grant Dom\HVAdmin:(CI)(OI)F
PowerShell sends the following command string to the Icacls program:
X:\VMS /grant Dom\HVAdmin:(CI)(OI)F
The stop-parsing symbol only intended for use on Windows platforms.