0
0
PowerShellscripting~5 mins

String type and interpolation in PowerShell

Choose your learning style9 modes available
Introduction
Strings hold text in scripts. Interpolation lets you insert values inside text easily.
When you want to show a message with variable values.
When building file paths that include folder names stored in variables.
When creating commands that need dynamic parts.
When formatting output for reports or logs.
Syntax
PowerShell
Double-quoted strings allow interpolation:
"Hello, $name!"
Single-quoted strings do not:
'Hello, $name!'
Use double quotes "" to insert variables inside strings.
Single quotes '' treat everything as plain text, no variable replacement.
Examples
Inserts the value of $name into the string.
PowerShell
$name = "Alice"
"Hello, $name!"
Shows how numbers can be inserted too.
PowerShell
$count = 5
"You have $count new messages."
Single quotes show the text exactly, no variable replaced.
PowerShell
'Hello, $name!'
Builds a file path using a variable.
PowerShell
$folder = "Documents"
"C:\Users\$folder\file.txt"
Sample Program
This script creates a message using variables inside a string and prints it.
PowerShell
$user = "Bob"
$files = 3
$message = "User $user has $files files in the folder."
Write-Output $message
OutputSuccess
Important Notes
To include a literal $ sign in a double-quoted string, use backtick: ``$``.
You can use ${variable} to clearly mark variable boundaries inside strings.
Remember that single quotes are best when you want the string exactly as typed.
Summary
Strings hold text and can include variables with interpolation.
Use double quotes for strings with variables inside.
Single quotes keep the string plain without replacing variables.