When working with PowerShell, you may often need to combine or concatenate strings. Concatenation is the process of combining multiple strings to create a single string. PowerShell provides several options for concatenating strings, each with advantages and use cases.
In this blog post, we will explore different PowerShell concatenate string techniques.
Recommended String Concatenation Methods
In PowerShell, different string concatenation methods are better suited for different scenarios. Below you’ll find a table which provides with a quick overview of when to use each approach.
| Scenario | Recommended Method |
|---|---|
| Simple variable interpolation | String expansion (“$var”) |
| Readable formatted output | Format operator (-f) |
| Joining arrays of strings | -join or [String]::Join() |
| Small one-off concatenations | + operator |
| High-performance loops or large text generation | System.Text.StringBuilder |
| Explicit .NET-style concatenation | [String]::Concat() |
Among the most common and readable approaches in PowerShell scripting are string expansion, the format operator (-f), and the -join operator.
When selecting a string concatenation method in PowerShell, you should consider the following factors:
- readability;
- performance;
- formatting requirements;
- array handling;
- scripting style and maintainability.
Some methods are better suited for simple interactive scripts, while others are more efficient for loops, logging, automation, and large-scale text processing.
String Expansion
String expansion is a straightforward and intuitive way to concatenate strings in PowerShell. It allows you to include variables and expressions within double-quoted strings. When PowerShell encounters a variable or expression enclosed in $(), it evaluates it and replaces it with its value in the resulting string.
Hereโs an example of string expansion in PowerShell:
$firstName = "John"
$lastName = "Doe"
$fullName = "$firstName $lastName"
$fullName
In this example, the variable $fullName will be assigned the concatenated value of $firstName and $lastName, resulting in โJohn Doeโ.
String expansion is a convenient option when you need to concatenate a few strings or include variables within a larger string.
When to use string expansion (best suited for):
- simple variable interpolation;
- readable console output;
- log messages;
- filenames and paths;
- quick admin scripts.
In most PowerShell scripts, string expansion is the most readable option for simple concatenation cases. Some more examples:
# Username
"$firstName.$lastName"
# User principal name or email address
"$firstName.$lastName@domain.com"
Using the String.Concat() Method
The String.Concat() method in PowerShell allows you to concatenate multiple strings by passing them as separate arguments to the method. This PowerShell concatenate string method combines all the input strings and returns a new string, that is, the concatenation of the inputs.
Hereโs an example of using the String.Concat() method:
$greeting = "Hello"
$name = "John"
$message = [String]::Concat($greeting, " ", $name)
In this example, the Concat() method combines the $greeting, a space character, and the $name variables, resulting in โHello Johnโ.

The String.Concat() method is useful when you have a known number of strings to concatenate and want to specify each string as an argument explicitly.
String Concatenation Operator (+)
PowerShell supports the + operator for string concatenation. This operator allows you to combine two or more strings into a single string. Note that this PowerShell concatenate string operator is a shorthand of the String.Concat() method.
Hereโs an example of using the string concatenation operator:
$firstName = "John"
$lastName = "Doe"
$fullName = $firstName + " " + $lastName
$fullName
In this example, the + operator concatenates the $firstName, a space character, and the $lastName variables, resulting in โJohn Doeโ.

The string concatenation operator is a concise and commonly used approach for concatenating strings in PowerShell.
When to use the + operator? The + operator works well for small, simple concatenation tasks. However, it is less ideal for large scripts because readability decreases as expressions grow longer. Furthermore, using + (or +=) for repeated concatenation inside loops can introduce unnecessary memory allocations and significantly reduce performance.
Be careful when using the + PowerShell concatenate string operator when concatenating strings and numbers because it is also a math operator (addition). When the left operand is numeric, PowerShell attempts numeric addition and tries to convert the right operand to a number. For example, run the below command in PowerShell:
$age = 30
$age + " years old"
This command returns an error because $age is a number value, and PowerShell interprets the + operator as an addition operation instead of string concatenation.

The same happens even if you donโt use variables and directly specify the number like so:
30 + " years old."

So how can we avoid this error? You can explicitly specify the variableโs data type as a string like so:
[string]$age = 30
Or enclose the number in quotes.
"30"
Either way, the resulting object is of a string data type which you can verify by calling the GetType() method.
$age.GetType()
"30".GetType()

So when we apply this technique, weโll get the expected results without errors.
[string]$age = 30
$age + " years old."
"30" + " years old."

In summary, when you start a PowerShell concatenate string operation with a number (i.e., number + string + string), the + operator is interpreted as a mathematical addition operator.
But suppose you start the concatenation with a string value (i.e., string + number + string). In that case, every element is automatically considered a string, and the + operator is interpreted as a string concatenation operator.
String.Format() Method
The String.Format() PowerShell concatenate string method provides a flexible way to concatenate strings in PowerShell by allowing you to specify placeholders within a format string and supply corresponding values.
Hereโs an example of using the String.Format() method:
$firstName = "John"
$lastName = "Doe"
$fullName = [String]::Format("Full name: {0} {1}", $firstName, $lastName)
$fullname
In this example, the format string โFull name: {0} {1}โ contains placeholders {0} and {1} that are replaced with the values of $firstName and $lastName, respectively.

The String.Format() method is beneficial when creating a formatted string with placeholders for dynamic values.
For example, letโs create a hash table of fruits with their corresponding colors.
$fruits = [ordered]@{
Banana = 'yellow'
Avocado = 'green'
Apple = 'red'
Orange = 'orange'
} Using the String.Format() method and for loop, we can display these values in this format โ[fruit name] is [color name]โ
for ($i = 0 ; $i -lt $fruits.Count; $i++ ) {
[String]::Format("{0} is {1}", $fruits.Keys[$i], $fruits.Values[$i])
} 
String Format Operator (-f)
PowerShell provides the -f operator, also known as the format operator, which allows you to perform string formatting and concatenation concisely and readably.
Note. The -f operator is a shorthand of the String.Format() method.
Hereโs an example of using the format operator:
$firstName = "John"
$lastName = "Doe"
$fullName = "Full name: {0} {1}" -f $firstName, $lastName
In this example, the format operator -f is applied to the string โFull name: {0} {1}โ, and the values of $firstName and $lastName are used to replace the placeholders {0} and {1}.
The format operator is an elegant way to concatenate strings and perform formatting in PowerShell. Since it is a shorthand of the String.Format() method, it is also excellent for displaying dynamic values.
$fruits = [ordered]@{
Banana = 'yellow'
Avocado = 'green'
Apple = 'red'
Orange = 'orange'
}
for ($i = 0 ; $i -lt $fruits.Count; $i++ ) {
"{0} is {1}" -f $fruits.Keys[$i], $fruits.Values[$i]
} 
String.Join() Method
The String.Join() PowerShell concatenate string method allows you to concatenate an array of strings using a specified separator. It takes two arguments: the separator string and an array of strings to concatenate.
Hereโs an example of using the String.Join() method:
$colors = "Red", "Green", "Blue"
$colorList = [String]::Join(", ", $colors)
In this example, the Join() method concatenates the strings in the $colors array, separating them with a comma and a space, resulting in โRed, Green, Blueโ.

The String.Join() method is handy when you have an array of strings that you want to concatenate with a specific separator.
When to use String.Join() and -join? These approaches are recommended when you are working with arrays/collections of strings. Note that the -join operator is more idiomatic in PowerShell scripts, while [String]::Join() may be preferred in scripts that closely follow .NET coding patterns.
For another example, letโs get the first five services on the computer and join their DisplayName property values with the โ | โ (space pipe space) characters.
$services = Get-Service | Select-Object -First 5
[String]::Join(" | ", $services.DisplayName)

You can achieve the same with a one-liner. This time, letโs join them with a new line character (`n)
[String]::Join("`n", $(Get-Service | Select-Object -First 3).DisplayName) 
Using the -join Operator
PowerShell also provides the -join operator, which allows you to concatenate an array of strings without specifying a separator explicitly. The -join operator joins the elements of an array into a single string without any delimiter.
Hereโs an example of using the join operator:
$colors = "Red", "Green", "Blue"
-join $colors
In this example, the -join operator concatenates the strings in the $colors array without any separator, resulting in โRedGreenBlueโ.

The join operator is useful when you want to concatenate strings in an array without any separator between them. Like, when creating filenames with a date.
$name = "backup_"
$now = Get-Date -Format "yyyy_MM_dd"
$ext = ".txt"
-join @($name, $now, $ext)

The -join operator provides functionality similar to String.Join(), which means you can also specify a delimiter. To do so, you must move the -join operator after the array and followed by the delimiter.
$colors = "Red", "Green", "Blue"
$colors -join ' > '

String concatenation in pipeline scenarios
String formatting is often used together with pipeline objects in PowerShell automation scripts. For example:
Get-Service |
Select-Object -First 3 |
ForEach-Object {
"{0} - {1}" -f $_.DisplayName, $_.Status
}
Performance Considerations
For most admin scripts, you won’t feel the performance difference between string concatenation methods. However, in loops or large-scale text processing operations, some approaches are significantly more efficient than others.
Here are our general recommendations:
- Use string expansion for readability.
- Use the format operator (-f) for formatted output.
- Use -join when combining arrays.
- You should avoid repeated + concatenation inside large loops.
.NET strings are immutable objects; Each concatenation operation creates a new string object in memory. Because of this, repeated use of the + operator inside loops may increase memory allocations significantly and reduce performance. - Use System.Text.StringBuilder for high-performance text generation cases.
Using StringBuilder for Large-Scale String Operations
When you need to build large strings inside loops or automation scripts, you should use System.Text.StringBuilder:
$stringBuilder = [System.Text.StringBuilder]::new()
1..5 | ForEach-Object {
[void]$stringBuilder.AppendLine("Processing item $_")
}
$result = $stringBuilder.ToString()
$result
Unlike repeated string concatenation with the + operator, StringBuilder reduces unnecessary memory allocations. It improves performance when generating:
- log files;
- reports;
- CSV/HTML content;
- large text output;
- dynamically generated scripts.
Conclusion
Concatenating strings in PowerShell is a common task, and knowing the various methods available can help you choose the most suitable approach for your specific requirements.
In this blog post, we explored different options for concatenating strings in PowerShell, including string expansion, the String.Concat() method, the string concatenation operator, the String.Format() method, the format operator, the String.Join() method and the join operator.
Each method has its strengths and use cases, so itโs essential to understand their differences and choose the most appropriate one for your PowerShell scripting needs.
For most modern PowerShell scripts:
- use string expansion for simple interpolation;
- use -f for formatted output;
- use -join for arrays and collections;
- avoid excessive + concatenation in loops;
- use StringBuilder for performance-critical scenarios.
What is the best way to concatenate strings in PowerShell?
For most modern PowerShell scripts:
- use string expansion for simple variable interpolation;
- use -f for formatted output;
- use -join for arrays and collections;
- use StringBuilder for large-scale or performance-critical operations.
String expansion and -f are generally considered the most readable approaches.
When should you use StringBuilder in PowerShell?
System.Text.StringBuilder is recommended for:
- loops with repeated concatenation;
- log generation;
- report creation;
- large text output;
- dynamically generated scripts.
It improves performance because .NET strings are immutable objects, and repeated + concatenation creates many temporary string objects in memory.
Which PowerShell string concatenation method is the most readable?
For readability:
- use string expansion for simple interpolation;
- use -f for formatted output;
- use -join for arrays.
Avoid long chains of + operators in larger scripts because they become harder to maintain and troubleshoot.
