0
0
PowerShellscripting~5 mins

String interpolation (double quotes) in PowerShell

Choose your learning style9 modes available
Introduction
String interpolation lets you put variables inside double quotes so you can build messages easily.
When you want to show a message with a variable value inside.
When you need to create a file path that includes folder names stored in variables.
When you want to combine text and numbers in a single string for output.
When you want to make your script messages clearer and easier to read.
Syntax
PowerShell
"Hello, $name!"
Use double quotes " " to enable interpolation.
Variables inside double quotes start with $ and get replaced by their values.
Examples
Prints 'Hello, Alice!' by replacing $name with its value.
PowerShell
$name = "Alice"
"Hello, $name!"
Shows how numbers can be included inside strings.
PowerShell
$count = 5
"You have $count new messages."
Builds a file path using a variable inside the string.
PowerShell
$folder = "Documents"
"C:\Users\$folder\file.txt"
Sample Program
This script creates a message using two variables inside a double-quoted string and prints it.
PowerShell
$user = "Bob"
$age = 30
$message = "User $user is $age years old."
Write-Output $message
OutputSuccess
Important Notes
Single quotes ' ' do NOT replace variables; use double quotes " " for interpolation.
If you want to include a literal $ sign, use backtick ` like this: "Price is `$5".
Summary
Double quotes allow variables to be replaced with their values inside strings.
Use $ before variable names inside double quotes to insert their values.
This makes building messages and paths easier and clearer.