Skip to content

PowerShell String Interpolation & Quoting Guide (For Linux Users 😅)

The behavior of quotes in PowerShell is nearly identical to Bash.

Quote TypeBehaviorBash EquivDescription
Single 'Literal'No expansion. What you type is exactly what you get.
Double "Interpolated"Variables ($var) and escape sequences (`n) are expanded.
Terminal window
$name = "Tux"
# Literal (Single Quotes)
Write-Output 'Hello $name'
# Output: Hello $name
# Interpolated (Double Quotes)
Write-Output "Hello $name"
# Output: Hello Tux

This is the most common “gotcha.”

  • Bash uses the backslash \ to escape characters.
  • PowerShell uses the backslash \ for file paths (C:\Windows).
  • PowerShell uses the Backtick ` (grave accent) as the escape character.
FeatureBash SyntaxPowerShell Syntax
Escape a double quote\"`"
Escape a variable ($)\$`$
New Line\n`n
Tab\t`t

If you need to output a string that contains double quotes (e.g., for a JSON string or a file path with spaces):

Terminal window
$path = "C:\Program Files\App"
# BAD (Parse Error):
# $cmd = "Executable is "$path""
# GOOD (Escaped):
$cmd = "Executable is `"$path`""
# Output: Executable is "C:\Program Files\App"

Standard variables expand just like in Bash.

Terminal window
$file = "log.txt"
Write-Output "Reading $file..."
# Output: Reading log.txt...

PowerShell passes Objects, not text. If you try to expand an object property inside a string directly, it often fails or prints the Class Name instead of the value.

Terminal window
$service = Get-Service wuauserv
# WRONG
Write-Output "The status is $service.Status"
# Output: The status is System.ServiceProcess.ServiceController.Status

Why? PowerShell sees $service, expands it to its string representation, and then treats .Status as literal text.

To interpret an expression (like accessing a property) inside a string, wrap it in $(). This is similar to command substitution in Bash ($()), but for variables.

Terminal window
# CORRECT
Write-Output "The status is $($service.Status)"
# Output: The status is Running

Just like in Bash, if a variable is immediately followed by text that could be interpreted as part of the variable name, use braces {}.

Terminal window
$base = "backup"
# Ambiguous (might look for a variable named $base_2025)
# Write-Output "$base_2025.zip"
# Explicit (Correct)
Write-Output "${base}_2025.zip"
# Output: backup_2025.zip

Escaping quotes with backticks (`) can get messy and hard to read. PowerShell offers a format operator -f (similar to Python’s .format() or C’s printf), which is often cleaner.

Syntax: 'Template String {0} {1}' -f $Var1, $Var2

It allows you to use Single Quotes for the template string (meaning you don’t have to escape double quotes inside it).

Terminal window
$exe = "C:\Program Files\App\bin.exe"
$flag = "C:\Configs\app.conf"
# The "Backtick Way" (Hard to read)
$cmd = "`"$exe`" --config=`"$flag`""
# The "-f Way" (Clean)
$cmd = '"{0}" --config="{1}"' -f $exe, $flag

Note: Since the template string uses single quotes '...', the double quotes " inside it are treated literally and do not need escaping.

6. Real World Use Case: The “Unquoted Service Path”

Section titled “6. Real World Use Case: The “Unquoted Service Path””

This is the specific error scenario discussed in our conversation.

The Problem: Windows interprets spaces in a path as a separator between an executable and its arguments unless the path is wrapped in quotes.

Terminal window
# Dangerous Path (Spaces)
$path = "C:\Program Files\My Service\svc.exe"
# Naive Interpolation
$binPath = "$path --run"
# Result: C:\Program Files\My Service\svc.exe --run

Windows tries to run C:\Program.exe with arguments Files\My.... It crashes.

The Solution: You must ensure the final string contains literal quotes around the path.

Terminal window
# Method 1: Escaping
$binPath = "`"$path`" --run"
# Method 2: Formatting (Preferred)
$binPath = '"{0}" --run' -f $path

Result: "C:\Program Files\My Service\svc.exe" --run

RequirementBashPowerShell
Variable"$var""$var"
Escape CharBackslash \Backtick `
Escape Quote\"`"
Object PropertyN/A"$($obj.Prop)"
Variable Boundary"${var}text""${var}text"
Format Stringprintf'String {0}' -f $val