Chapter 1. PowerShell Language and Environment
Commands and Expressions
PowerShell breaks any line that you enter into its individual units (tokens), and then interprets each token in one of two ways: as a command or as an expression. The difference is subtle: expressions support logic and flow control statements (such as if, foreach, and throw), whereas commands do not.
You will often want to control the way that PowerShell interprets your statements, so Table 1-1 lists the available options.
| Statement | Explanation |
|---|---|
Precedence control: |
Forces the evaluation of a command or expression, similar to the way that parentheses are used to force the order of evaluation in a mathematical expression. For example: PS > 5 * (1 + 2) 15 PS > (dir).Count 227 |
Expression subparse: |
Forces the evaluation of a command or expression, similar to the way that parentheses are used to force the order of evaluation in a mathematical expression. However, a subparse is as powerful as a subprogram and is required only when the subprogram contains logic or flow control statements. This statement is also used to expand dynamic information inside a string. For example: PS > "The answer is (2+2)"
The answer is (2+2)
PS > "The answer is $(2+2)"
The answer is 4
PS > $value = 10
PS > $result = $(
if($value -gt 0) { $true }
else { $false })
PS > $result
True |
List evaluation: |
Forces an expression to be evaluated as a list. If it is already a list, ... |
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access