POWERSHELL / CHAPTER 01
Values, interpolation, and expressions
BeginnerRead PowerShell 7 values without treating everything as text.
By the end of this chapter
- Distinguish expandable and literal strings
- Use subexpressions for property interpolation
- Keep arithmetic separate from formatted output
The idea
These lessons target PowerShell 7. Variables hold objects, so a number and a string containing digits are different inputs even when the console renders them similarly. Commands and expressions have different parsing contexts; use parentheses when an argument needs an expression result. Single-quoted strings are literal, while double-quoted strings expand variables and supported subexpressions.
Inside an expandable string, use a subexpression when displaying a property or calculation, such as $($item.Name). Simple variable expansion alone does not evaluate an entire property access. Keep the computed value separate from its final display when later commands need to filter, compare, or sum it. Object values preserve more useful structure than a line of text assembled too early.
$item = [pscustomobject]@{ Name = "Panel"; Count = 3 }
"$($item.Name): $($item.Count * 2)"
'$item.Name'Panel: 6 $item.Name
Subexpressions evaluate the property and calculation before interpolation. The single-quoted line remains literal text, including the dollar sign.
Try it yourself
Create an object with a name and count, then format a message without replacing the original object.
Use your own editor for this exercise. Runnable challenges on CodeQuest are available for JavaScript, Python, and SQL.
CHECK YOUR UNDERSTANDING