How to Concatenate String with Space in PowerShell (4 Simple Methods)

In my years of PowerShell scripting, I’ve often needed to combine strings while ensuring proper spacing between them.

Whether you’re building filenames, creating log messages, or generating reports, you will require string concatenation in PowerShell.

In this tutorial, we will see various methods to concatenate strings with spaces in PowerShell with examples.

Let’s get started with some practical examples that you can apply to your own scripts right away.

Method 1 – Using the Plus (+) Operator

The plus operator is the most straightforward way to concatenate strings in PowerShell.

Here’s how to use it to combine strings with spaces:

$firstName = "John"
$lastName = "Smith"

# Concatenation with explicit space
$fullName = $firstName + " " + $lastName

Write-Host $fullName
# Output: John Smith

When working with the plus operator, you need to explicitly add the space as a string literal (" "). This gives you precise control over spacing, but can become cumbersome with multiple strings.

Here is the output in the screenshot below:

powershell concatenate string with space

I often use this method for simple concatenations where readability is more important than efficiency.

Check out Concatenate String and Variable in PowerShell

Method 2 – Using String Interpolation

String interpolation (also called variable expansion) is my preferred method for most scenarios as it’s more readable and efficient. Here is an example.

$firstName = "John"
$lastName = "Smith"

# Basic string interpolation
$fullName = "$firstName $lastName"

Write-Host $fullName
# Output: John Smith

This approach is cleaner because you can embed variables directly within the string. The spaces between variables are preserved exactly as written in the double quotes.

Here is the exact output in the screenshot below:

concatenate string with space in powershell

For more complex expressions, you can use subexpression syntax:

$order = 10
$item = "coffee"
$price = 4.99

# Using subexpressions for formatting
$message = "Your order of $order ${item}s costs $($order * $price) dollars."

Write-Host $message
# Output: Your order of 10 coffees costs 49.9 dollars.

The $() syntax allows you to include expressions and formatting inside your strings, which is incredibly powerful.

Read Concatenate String with NewLine in PowerShell

Method 3 – Using the -join Operator

The -join operator in PowerShell is specifically designed for concatenation and offers great flexibility. Here is an example: you can use the -join operator to concatenate a string with a space.

$parts = @("PowerShell", "is", "awesome")

# Join with spaces
$sentence = $parts -join " "

Write-Host $sentence
# Output: PowerShell is awesome

You can see the exact output in the screenshot below:

How to Concatenate Strings with Spaces in PowerShell

You can join array elements with any delimiter:

$path = @("C:", "Users", "JohnSmith", "Documents") -join "\"
Write-Host $path
# Output: C:\Users\JohnSmith\Documents

I find -join particularly useful when working with arrays or when I need to join a large number of strings with the same delimiter.

Check out Concatenate Strings Inside Loops in PowerShell

Method 4 – Using StringBuilder for Performance

When concatenating many strings in a loop, using the StringBuilder class from .NET can significantly improve performance:

$sb = [System.Text.StringBuilder]::new()

$names = @("Alice", "Bob", "Charlie", "David")

foreach ($name in $names) {
    # Append with space
    [void]$sb.Append($name)
    [void]$sb.Append(" ")
}

# Convert to string and trim the trailing space
$result = $sb.ToString().TrimEnd()

Write-Host $result
# Output: Alice Bob Charlie David

For a more controlled approach with spaces:

$sb = [System.Text.StringBuilder]::new()
$names = @("Alice", "Bob", "Charlie", "David")

for ($i = 0; $i -lt $names.Count; $i++) {
    [void]$sb.Append($names[$i])

    # Add space only if not the last element
    if ($i -lt $names.Count - 1) {
        [void]$sb.Append(" ")
    }
}

$result = $sb.ToString()
Write-Host $result
# Output: Alice Bob Charlie David

I typically use StringBuilder when concatenating strings inside loops that iterate thousands of times, as it prevents the memory fragmentation that can occur with repeated string concatenation.

Read Concatenate String and Integer in PowerShell

Comparing the Methods: When to Use Each Approach

Here’s a quick reference table to help you choose the right method for your specific scenario:

MethodBest ForPerformanceReadability
+ OperatorSimple concatenationsLow for many operationsGood
String InterpolationMost everyday scenariosGoodExcellent
-join OperatorArrays and collectionsExcellentVery Good
StringBuilderLarge loops and performance-critical codeExcellentFair

Special Cases

Here are some special cases that you might face while concatenating a string with a space.

Working with Different Data Types

PowerShell automatically converts many data types to strings during concatenation:

$quantity = 5
$product = "chairs"
$price = 49.99

# Mixing strings and numbers
$orderSummary = "Order: $quantity $product at $" + $price + " each"

Write-Host $orderSummary
# Output: Order: 5 chairs at $49.99 each

Formatting Numbers and Dates

For more control over number and date formatting, use the -f operator:

$total = 1234.56789
$date = Get-Date

# Format specifiers with the -f operator
$receipt = "Purchase total: {0:C2} on {1:MM/dd/yyyy}" -f $total, $date

Write-Host $receipt
# Output example: Purchase total: $1,234.57 on 06/04/2023

Multi-line String Concatenation

For readability in complex scripts, you can break concatenation across lines:

$address = $streetNumber + " " + 
           $streetName + ", " + 
           $city + ", " + 
           $state + " " + 
           $zipCode

# Or with string interpolation (my preference)
$address = "$streetNumber $streetName, 
           $city, 
           $state $zipCode"

I hope this tutorial, you learned how to concatenate strings with spaces in PowerShell. Each method has its advantages, and choosing the right one depends on your specific scenario.

If you have any questions or suggestions, please feel free to share them in the comments below.

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.