0
0
PowerShellscripting~10 mins

String type and interpolation in PowerShell - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - String type and interpolation
Start
Define string variable
Choose string type
Double quotes
Supports interpolation
Output string with variables
End
This flow shows how defining strings with double or single quotes affects whether variables inside are replaced with their values (interpolated) or treated as plain text.
Execution Sample
PowerShell
$name = "Alice"
$greeting1 = "Hello, $name!"
$greeting2 = 'Hello, $name!'
Write-Output $greeting1
Write-Output $greeting2
This script defines a variable and shows how double-quoted strings replace $name with its value, while single-quoted strings do not.
Execution Table
StepActionVariable ValuesOutput
1Assign $name = "Alice"$name = Alice
2Assign $greeting1 = "Hello, $name!" (double quotes)$greeting1 = Hello, Alice!
3Assign $greeting2 = 'Hello, $name!' (single quotes)$greeting2 = Hello, $name!
4Write-Output $greeting1$greeting1 = Hello, Alice!Hello, Alice!
5Write-Output $greeting2$greeting2 = Hello, $name!Hello, $name!
💡 All commands executed; output shows interpolation only in double-quoted string.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$nameundefinedAliceAliceAliceAlice
$greeting1undefinedundefinedHello, Alice!Hello, Alice!Hello, Alice!
$greeting2undefinedundefinedundefinedHello, $name!Hello, $name!
Key Moments - 2 Insights
Why does $greeting1 show the name but $greeting2 shows $name literally?
Because $greeting1 uses double quotes which allow variable interpolation (see Step 2 and Step 4 in execution_table), while $greeting2 uses single quotes which treat the content as plain text (see Step 3 and Step 5).
What happens if you use single quotes but want to include a variable's value?
You must use double quotes or concatenate strings; single quotes do not replace variables with their values (see execution_table rows 3 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 4, what is the output of Write-Output $greeting1?
A$greeting1
BHello, $name!
CHello, Alice!
DError
💡 Hint
Check the Output column at Step 4 in execution_table.
At which step does $greeting2 get assigned a value that includes the literal text $name?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the Variable Values column for $greeting2 in execution_table.
If you change $greeting2 to use double quotes, what will be the output at Step 5?
AHello, Alice!
BHello, $name!
CError
D$greeting2
💡 Hint
Refer to how $greeting1 is assigned and output in Steps 2 and 4.
Concept Snapshot
String type and interpolation in PowerShell:
- Double quotes "" allow variables inside strings to be replaced with their values.
- Single quotes '' treat the string literally, no variable replacement.
- Use double quotes for dynamic strings with variables.
- Use single quotes for fixed text.
- Example: "$name" vs '$name'
Full Transcript
This lesson shows how PowerShell handles strings and variable interpolation. When you put a variable inside double quotes, PowerShell replaces it with the variable's value. For example, if $name is Alice, then "Hello, $name!" becomes "Hello, Alice!". But if you use single quotes, like 'Hello, $name!', PowerShell treats it as plain text and does not replace $name. The script assigns $name to Alice, then creates two greetings: one with double quotes and one with single quotes. When outputting, the double-quoted string shows the name, the single-quoted string shows the variable name literally. This helps you decide when to use each type of string in your scripts.