PowerShell
String Concatenation
Variables
Scripting
Coding Guide

How do I concatenate strings and variables in PowerShell?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

PowerShell gives you several valid ways to combine text and variables, but they are not all equally readable. For ordinary command output, interpolation inside double quotes is usually the best choice. For arrays, template-style formatting, or tricky variable boundaries, other forms are clearer and safer.

Start with double-quoted interpolation

The most common pattern is to place variables directly inside a double-quoted string.

powershell
$name = "Morgan"
$message = "Hello, $name"
Write-Output $message

PowerShell expands variables only in double-quoted strings. If you use single quotes, the variable name stays literal:

powershell
$name = "Morgan"
Write-Output 'Hello, $name'

That difference is the first thing to remember. If interpolation should happen, use double quotes.

Use subexpressions for properties and commands

Simple variables expand cleanly, but expressions need $() so PowerShell knows exactly what to evaluate.

powershell
1$user = [pscustomobject]@{
2    Name = "Morgan"
3    LoginCount = 7
4}
5
6$message = "User $($user.Name) has logged in $($user.LoginCount) times."
7Write-Output $message

The same rule applies when you call a command inside a string:

powershell
$message = "Today is $(Get-Date -Format 'yyyy-MM-dd')"
Write-Output $message

Without the subexpression, parsing often stops too early or produces unexpected output.

Use braces when text touches the variable name

PowerShell sometimes needs help knowing where the variable name ends. Braces solve that problem:

powershell
$envName = "prod"
$path = "logs/${envName}_app.log"
Write-Output $path

This is especially useful when a variable sits next to letters, underscores, or punctuation that could be interpreted as part of the name.

Use -f when the string is really a template

If you want fixed placeholders or need to control order explicitly, use the format operator.

powershell
1$name = "Morgan"
2$count = 7
3$message = "User {0} has {1} logins" -f $name, $count
4Write-Output $message

This style is often easier to maintain when the same output format appears in several places or when values must be reordered for localization or reporting.

It is also handy for numbers and dates:

powershell
$total = 12.3456
$line = "Total: {0:N2}" -f $total
Write-Output $line

Use -join for arrays instead of repeated concatenation

If you are building a string from many values, -join is usually better than chaining +.

powershell
$parts = "PowerShell", "makes", "pipelines", "pleasant"
$sentence = $parts -join " "
Write-Output $sentence

Repeated + works, but it becomes harder to read as the number of fragments grows. -join makes it obvious that the input is a collection, not a handful of separate variables.

Pick the style that matches the job

A practical rule set looks like this:

  • use double quotes for ordinary interpolation
  • use $() for expressions and property access
  • use ${} when variable boundaries would otherwise be ambiguous
  • use -f for template-style formatting
  • use -join when the source data is already an array

That rule set keeps most scripts readable without forcing one technique everywhere.

For multi-line output, a here-string is often cleaner than lots of + operators:

powershell
1$name = "Morgan"
2$report = @"
3User: $name
4Generated: $(Get-Date -Format 'u')
5"@
6
7Write-Output $report

Common Pitfalls

The most common mistake is using single quotes and expecting variable expansion. Single-quoted strings are literal.

Another common problem is forgetting $() around property access or command output inside an interpolated string.

People also run variable names directly into surrounding text and get partial expansion or the wrong variable name. ${} fixes that.

Finally, avoid building large strings with repeated + when the real input is an array or a reusable template. -join and -f are usually clearer.

Summary

  • Double-quoted strings are the default way to combine text and variables in PowerShell.
  • Use $() for expressions and ${} for ambiguous variable boundaries.
  • Use -f when you want template-style placeholders.
  • Use -join when combining many string elements from an array.
  • Choose the form that makes the output intent obvious to the next reader.

Course illustration
Course illustration