Double quotation marks create expandable strings: PowerShell replaces variable references with their values when it first parses the string. Single quotation marks keep those references literal. section 16.3 section 26.1.2
Build a message from a variable
Set $computername to 'SRV-02', then assign "The computer name is $computername" to $phrase. PowerShell displays “The computer name is SRV-02”. Changing $computername later does not update $phrase, because replacement happened when the string was parsed. section 16.3
Use double quotation marks for these practical string-building tasks:
- Escape a dollar sign with the backtick so it remains visible instead of starting variable replacement. section 16.3
- Insert a command result with a subexpression such as $($processes[0].name); PowerShell evaluates the expression and places its result in the string. section 16.5
- Represent special characters such as a newline or tab with backtick sequences including `n and `t. section 16.3 section 26.2.3
PS > $computername = 'SRV-02' PS > $phrase = "The computer name is $computername" PS > $phrase The computer name is SRV-02
See variable interpolation produce a complete sentence, then compare it with the literal single-quoted form in b1 and the escaped form in b4. section 16.3
