How to Concatenate Strings in PowerShell?

If you are working with strings in PowerShell, then you might want to concatenate strings. In this PowerShell tutorial, I will show you different methods to concatenate strings in PowerShell. We will see how to combine multiple strings into a single string using PowerShell.

To concatenate strings in PowerShell, you can use the + operator. For example, “John” + ” ” + “Doe” will yield “John Doe”. Another method is using the -f format operator, such as “{0} {1}” -f “John”, “Doe”. Also the Join-String cmdlet can be used for more complex concatenation tasks. Here’s a simple example:

$firstName = "John"
$lastName = "Doe"
$fullName = $firstName + " " + $lastName
Write-Host $fullName  # Outputs: John Doe

Concatenate Strings in PowerShell

To combine strings in PowerShell, we can use the below methods:

  • Using the + Operator
  • Using the -f Operator (Format Operator)
  • Using String Interpolation
  • Using the -join Operator
  • Using the String.Concat() Method
  • Using the StringBuilder Class

We will check all the above methods with examples.

1. Using the + Operator

The simplest way to concatenate strings in PowerShell is to use the + operator. This cmdlet is very simple and looks like the one below.

Here is a complete PowerShell script.

$city1 = "New York"
$city2 = "Los Angeles"
$city3 = "Chicago"

$combinedCities = $city1 + ", " + $city2 + ", " + $city3
Write-Output $combinedCities

Output:

New York, Los Angeles, Chicago

You can see the output in the screenshot below after I executed the PowerShell script using VS code.

Concatenate Strings in PowerShell

Read Concatenate Strings Inside Loops in PowerShell

2. Using the -f Operator (Format Operator)

The -f operator in PowerShell allows for more complex string formatting. It is similar to string interpolation in other languages.

Here is a complete PowerShell script.

$city1 = "Houston"
$city2 = "Phoenix"
$city3 = "Philadelphia"

$combinedCities = "{0}, {1}, {2}" -f $city1, $city2, $city3
Write-Output $combinedCities

Output:

Houston, Phoenix, Philadelphia

You can see the output in the screenshot below; I executed the script.

How to Concatenate Strings in PowerShell

3. Using String Interpolation

String interpolation embeds variable values directly within a string in PowerShell. In PowerShell, you can use double quotes (") to achieve this.

Here is a complete script to merge strings in PowerShell.

$city1 = "San Antonio"
$city2 = "San Diego"
$city3 = "Dallas"

$combinedCities = "$city1, $city2, $city3"
Write-Output $combinedCities

Output:

San Antonio, San Diego, Dallas

You can see the output in the screenshot below; I executed the PowerShell script.

merge strings in powershell, concatenate 2 strings in powershell

4. Using the -join Operator

The -join operator can be used to concatenate an array of strings with a specified separator in PowerShell. This is particularly useful when dealing with lists.

$cities = @("San Jose", "Austin", "Jacksonville")

$combinedCities = -join($cities, ", ")
Write-Output $combinedCities

Output:

San Jose, Austin, Jacksonville

5. Using the String.Concat() Method

The String.Concat method from the .NET framework can also be used to concatenate strings in PowerShell.

Here is a complete PowerShell script.

$city1 = "San Francisco"
$city2 = "Columbus"
$city3 = "Fort Worth"

$combinedCities = [String]::Concat($city1, ", ", $city2, ", ", $city3)
Write-Output $combinedCities

Output:

San Francisco, Columbus, Fort Worth

You can see the output in the screenshot below:

concatenate two strings in powershell

6. Using the StringBuilder Class

If you need to concatenate a large number of strings efficiently, then you can use the StringBuilder class in PowerShell.

Below is a complete script for concatenating strings in PowerShell using the StringBuilder Class.

Add-Type -AssemblyName "System.Text"
$stringBuilder = New-Object System.Text.StringBuilder

$cities = @("Indianapolis", "Charlotte", "Seattle")

foreach ($city in $cities) {
    [void]$stringBuilder.Append("$city, ")
}

# Remove the trailing comma and space
if ($stringBuilder.Length -gt 0) {
    $stringBuilder.Length -= 2
}

$combinedCities = $stringBuilder.ToString()
Write-Output $combinedCities

Output:

Indianapolis, Charlotte, Seattle

Conclusion

I hope this tutorial helps you concatenate 2 strings in PowerShell. In it, I have explained 6 different methods for concatenating strings in PowerShell.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.