PowerShell Concatenate Strings
In PowerShell, concatenate two strings with the + operator, insert values into double-quoted strings, and turn an array into one delimited string with -join. The best choice depends on whether you are combining a few known values, building a readable message, or joining a collection such as names, paths, or IDs.
What PowerShell Means by Concatenation
String concatenation creates one text value from separate pieces. PowerShell can convert many values to text automatically, which makes simple concatenation convenient but can also hide mistakes.
For example, + adds numeric values when both sides are numbers, but it combines text when one side is a string.
2 + 3
That produces a numeric result. In contrast, this produces text:
'Build-' + 3
Use quotes deliberately:
- Single quotes create literal text. Variables are not expanded.
- Double quotes create expandable strings. PowerShell replaces variables and subexpressions.
- The
+operator joins explicit pieces of text. - The
-joinoperator combines every item in an array or collection.
How to Concatenate Strings in PowerShell
Choose the method that matches the kind of string you are building.
| Need | Best Method | Example Shape |
|---|---|---|
| Combine a few explicit values | + |
$first + ' ' + $last |
| Put variables into a readable message | Double-quoted string | "Hello, $name" |
| Include a property, command result, or expression | Subexpression | "Status: $($service.Status)" |
| Reuse a fixed text template | Format operator | '{0}-{1}' -f $name, $id |
Use the Plus Operator for Explicit Parts
Use + when the separate parts are obvious and you want each part visible in the expression.
$firstName = 'Ada'
$lastName = 'Lovelace'
$fullName = $firstName + ' ' + $lastName
$fullName
The value of $fullName is:
Ada Lovelace
The spaces, dashes, slashes, and other separators are not added automatically. Include them as quoted text.
$environment = 'production'
$serverName = 'api-01'
$label = $environment + '/' + $serverName
If PowerShell could treat both operands as numbers, make the text intent explicit.
$releaseName = 'release-' + [string]42
Use Double Quotes for Variable Concatenation
Double-quoted strings are usually the clearest option when you are building a message, filename, label, or log line.
$environment = 'staging'
$message = "Deploying to $environment"
Single quotes do not expand variables:
$environment = 'staging'
$literalMessage = 'Deploying to $environment'
The value of $literalMessage contains the characters $environment, not staging.
Use braces when text immediately after a variable name could be interpreted as part of that name.
$name = 'report'
$fileName = "${name}-final.txt"
Without braces, a string such as "$nameSuffix" looks for a variable named $nameSuffix, not $name followed by the literal word Suffix.
Use Subexpressions for Properties and Calculations
A simple variable expands directly in double quotes, but an object property or expression needs $().
$service = [pscustomobject]@{
Name = 'web'
Status = 'Running'
}
$statusMessage = "Service $($service.Name) is $($service.Status)"
Use the same pattern for calculations and command output.
$items = 'api', 'worker', 'database'
$summary = "Configured services: $($items.Count)"
$() tells PowerShell to evaluate the content first, then insert its text representation into the string.
Use the Format Operator for Reusable Templates
The format operator, -f, is useful when the template is separate from its values or when the same template appears in several places.
$application = 'billing'
$environment = 'test'
$tag = '{0}-{1}' -f $application, $environment
The placeholders start at {0}, then {1}, {2}, and so on. Escape literal braces by doubling them.
$name = 'api'
$jsonFragment = '{{ "name": "{0}" }}' -f $name
This is often easier to read than a long chain of + operators when a string contains many fixed punctuation characters.
Join an Array Into One String
Choose -join when you are joining a collection; choose interpolation or + when you are constructing one message.
Use the binary -join operator to place a separator between array items.
$hosts = 'api-01', 'api-02', 'db-01'
$hostList = $hosts -join ', '
$hostList
The result is:
api-01, api-02, db-01
To join without a separator, use the unary form.
$parts = 'build', '-', 'latest'
$value = -join $parts
The unary -join expression combines the three array items into build-latest.
Join a Property From Many Objects
Collections often contain objects rather than plain strings. Extract the property you want, then join it.
$serviceNames = Get-Service | Select-Object -ExpandProperty Name
$serviceList = $serviceNames -join '; '
$serviceList
This avoids joining PowerShell’s formatted object display, which is intended for the console rather than for scripts.
If you need to create a string from values gathered in a loop, collect values into an array and apply -join after the loop whenever possible. It is usually clearer than manually managing a separator in every iteration.
Understand Empty, Null, and False Values
The -join operator keeps each array position. A $null value and an empty string contribute no visible text, but the separator still appears. Boolean $false is converted to the text False.
$values = 'first', $null, '', $false, 'last'
$joined = $values -join ','
$joined
That behavior matters for CSV-like output, where an empty position can be meaningful. Filter values before joining only when removing them is intentional.
$values = 'first', $null, '', $false, 'last'
$nonBlankValues = $values | Where-Object {
-not [string]::IsNullOrEmpty([string]$_)
}
$joined = $nonBlankValues -join ','
This example removes $null and empty strings, while $false remains because it converts to False for the blank-value test.
Use .NET String.Join When Needed
-join is the normal PowerShell choice, but [string]::Join() can be useful when code already uses .NET APIs.
$colors = 'red', 'green', 'blue'
$colorList = [string]::Join(', ', [string[]]$colors)
For ordinary scripts, -join is shorter and more recognizable to PowerShell users.
Be Careful With Custom Join-String Functions
Some PowerShell profiles define a custom function named Join-String. That custom function differs from the -join operator and has its own filtering behavior.
Check what your session resolves before depending on it.
Get-Command -Name Join-String -All
In the custom Join-String implementation from the University of Vermont, the default separator is a comma and blank-like values remain represented.
$array = 3.14, 'Puppy', $false, '', 'Green', $null, 'foo'
$array | Join-String
With that custom function loaded, the result is:
3.14,Puppy,False,,Green,,foo
Its -collapse option removes $false, empty-string, and $null entries.
$array | Join-String -collapse
That produces:
3.14,Puppy,Green,foo
You can supply a separator after -collapse.
$array | Join-String -collapse ' - '
That produces:
3.14 - Puppy - Green - foo
The same custom function can receive the array as an argument and return a string for later use.
$y = Join-String $array -collapse
$y.GetType().Name
The type name is String. Prefer -join for portable scripts that only need to join values; use a custom Join-String function only when you know it is installed and its filtering behavior is what you want.
Append Text in Loops Without Replacing It
Use += to append to an existing string. A normal assignment with = replaces the previous value.
$line = 'start'
$line = '-next'
$line
The result is only -next because the second assignment overwrote the original value.
For a small loop, append carefully and add separators only between items.
$names = 'api', 'worker', 'db'
$line = ''
foreach ($name in $names) {
if ($line) {
$line += ', '
}
$line += $name
}
$line
For a known array, this is simpler:
$names = 'api', 'worker', 'db'
$line = $names -join ', '
For large or frequent appends, use System.Text.StringBuilder. PowerShell strings are immutable, so repeated += operations repeatedly create replacement strings.
$parts = 'log-', 'archive-', 'current'
$builder = [System.Text.StringBuilder]::new()
foreach ($part in $parts) {
[void]$builder.Append($part)
}
$result = $builder.ToString()
$result
Append() returns the builder object, so [void] prevents that intermediate object from being written to the pipeline. Call ToString() when you need the completed text.
Avoid Variable, Property, and Path Errors
String concatenation is not always the right tool. Use PowerShell commands that understand the data type when they exist.
Build File Paths With Join-Path
Do not manually concatenate folder names and path separators when you need a filesystem path.
$reportPath = Join-Path -Path $HOME -ChildPath 'report.txt'
$reportPath
Join-Path handles the path separator appropriate to the PowerShell provider. It also makes the distinction clear: you are building a path, not arbitrary display text.
Pass Arguments Instead of Building a Command String
Avoid combining executable names and arguments into one string for later execution. Keep the command and its arguments separate so PowerShell can pass values correctly, including paths with spaces.
& git -C $repoPath status --short
Use concatenation to create data such as a branch name, filename, or message. Use argument passing to run commands.
Escape a Literal Dollar Sign in Expandable Text
A dollar sign starts a variable reference inside double quotes. Escape it with a backtick when you need the literal character.
$example = "The variable name is `$HOME"
$example
Single quotes are another good choice when no expansion is needed.
$example = 'The variable name is $HOME'
$example
Confirm the Result Is a String
When output looks wrong, inspect the value before passing it to another command. Brackets make leading or trailing whitespace visible.
$result = 'api' + '-' + '01'
"[$result]"
Check the object type if a command expects a string but receives an array or another object.
$result.GetType().FullName
For a concatenation result, the type should be System.String.
Use this short checklist when debugging:
- Verify that double quotes are used where variable expansion is expected.
- Add
${variable}when literal letters immediately follow a variable. - Use
$()for properties, methods, calculations, and command output. - Use
-joinfor arrays rather than+inside a loop. - Use
Join-Pathfor filesystem paths instead of manually adding slashes.
Frequently Asked Questions
Can You Concatenate Two Strings in PowerShell?
Yes. Use + when you want to combine two explicit strings.
$result = 'hello' + ' world'
The value of $result is hello world. Add any required separator, such as a space or dash, as its own quoted string.
How Do You Concatenate a Variable With Text in PowerShell?
Use a double-quoted string for simple variables.
$name = 'report'
$file = "$name.txt"
Use braces when the following text could look like part of the variable name.
$file = "${name}Final.txt"
How Do You Concatenate a List Into One String?
Use -join with the delimiter you want.
$items = 'one', 'two', 'three'
$result = $items -join ' | '
This creates one System.String containing one | two | three. Use an empty delimiter with -join $items when the pieces should run together.
Why Does PowerShell Print the Variable Name Instead of Its Value?
Single-quoted strings treat $variable as literal text. Use double quotes when you want PowerShell to expand a variable.
$name = 'Ada'
$correct = "Hello, $name"
For properties such as $user.Name, use a subexpression such as "Hello, $($user.Name)".
Does += Append Text to a String?
Yes. $value += 'text' appends text to the current string value, while $value = 'text' replaces the current value.
$value = 'build'
$value += '-latest'
Use -join instead when you already have all values in an array, and use StringBuilder when repeatedly appending many pieces in a long-running loop.
