PowerShell String Concatenation with Delimiters

Today, I will discuss with you a very important topic, “PowerShell string concatenation with delimiters”. If you’re working with arrays, processing data, or formatting output for reports, you might need to concatenate strings with delimiters in PowerShell.

As a PowerShell developer with over a decade of experience in various Fortune 500 organizations, I’ve learned that string manipulation is something you’ll do nearly every day in your scripting journey. Mainly, you should know how to join strings with a delimiter.

In this tutorial, I’ll walk you through every method available for concatenating strings with delimiters in PowerShell, complete with real-world examples.

The PowerShell -join Operator

The -join operator is the most straightforward and PowerShell-specific way to concatenate an array of strings with a delimiter. It’s built directly into the language and provides a clean, readable syntax.

PowerShell provides numerous ways to manipulate strings, making it a powerful tool for working with text data. The -join operator is one of those powerful features.

Here’s how to use it:

$states = @('Texas', 'California', 'Florida', 'New York')
$result = $states -join ', '
Write-Output $result

This produces: Texas, California, Florida, New York

You can see the exact output in the screenshot below:

powershell concatenate strings with delimiter

The -join operator takes the array of strings on the left and concatenates them using the delimiter specified on the right. It’s clean, intuitive, and very PowerShell-centric. I use this method most frequently in my day-to-day scripting.

Advanced Usage of -join

You can also use the -join operator without specifying a delimiter, which will concatenate the strings without any characters between them:

$phoneDigits = @('555', '123', '4567')
$phoneNumber = $phoneDigits -join ''
Write-Output $phoneNumber  # Outputs: 5551234567

When working with arrays that might contain null or empty values, -join handles them gracefully by simply skipping them:

$data = @('Washington', $null, 'Lincoln', '', 'Roosevelt')
$result = $data -join ' | '
Write-Output $result  # Outputs: Washington | Lincoln |  | Roosevelt

Notice how the null value is completely skipped, while the empty string is preserved as an empty space between delimiters.

Check out Concatenate String and DateTime in PowerShell

Using String.Join() Method

Another powerful approach is using the .NET String.Join() method. This method is particularly useful when you’re working in environments where .NET functionality is preferred or when you need to integrate with other .NET code.

PowerShell’s built-in Concat() function is another way to handle string concatenation, but String.Join() gives you more control with delimiters.

Here’s the basic syntax:

$cities = @('Chicago', 'Miami', 'Seattle', 'Boston')
$result = [string]::Join(' - ', $cities)
Write-Output $result

This produces: Chicago - Miami - Seattle - Boston

You can see the exact output in the screenshot below:

powershell concatenate strings with delimiter examples

I find this method particularly useful when I’m working with PowerShell in a larger .NET environment or when I’m developing scripts that might be converted to C# later.

String.Join() with Non-String Arrays

One advantage of String.Join() is its ability to work with arrays of objects that aren’t strings by implicitly converting them.

Here is the complete PowerShell script:

$numbers = @(42, 7, 99, 1776)
$result = [string]::Join(', ', $numbers)
Write-Output $result  # Outputs: 42, 7, 99, 1776

This implicit conversion can be a real time-saver when working with mixed data types.

Read Concatenate Strings and Floats in PowerShell

The Format Operator (-f)

The format operator (-f) gives you incredible control over how strings are formatted and concatenated. While it’s slightly more complex, it offers tremendous flexibility for more sophisticated output formatting.

In the context of PowerShell, string concatenation is essential for creating informative messages, dynamic file paths, or any other scenario where text assembly is required.

Here’s how to use the format operator to join strings with a delimiter:

$presidents = @('Washington', 'Jefferson', 'Lincoln', 'Roosevelt')
$result = "{0}, {1}, {2}, and {3}" -f $presidents
Write-Output $result

This produces: Washington, Jefferson, Lincoln, and Roosevelt

The format operator is particularly powerful when you need different delimiters between different elements, as shown in the example with the “and” before the last item. I use this approach often when creating natural-language output in reports.

Dynamic Formatting with -f

For more dynamic situations, you can generate the format string programmatically:

$employees = @('John', 'Sarah', 'Michael', 'Emily', 'David')
$formatString = 0..($employees.Count-2) | ForEach-Object { "{$_}, " }
$formatString += "{$($employees.Count-1)}"
$result = $formatString -f $employees
Write-Output $result  # Outputs: John, Sarah, Michael, Emily, David

This advanced technique allows you to handle arrays of any size dynamically.

Check out How to Concatenate String with Space in PowerShell

Using Join-String Cmdlet (PowerShell 7+)

If you’re using PowerShell 7 or later, you have access to the new Join-String cmdlet, which provides a pipeline-friendly way to concatenate strings with delimiters.

Here’s how to use it:

$states = @('California', 'Texas', 'Florida', 'New York')
$result = $states | Join-String -Separator ', '
Write-Output $result

This produces: California, Texas, Florida, New York

As someone who works extensively with pipeline operations in PowerShell, I find this method particularly elegant when it’s part of a larger pipeline of operations.

Advanced Join-String Features

The Join-String cmdlet has some powerful additional features, like the ability to add prefixes and suffixes:

$capitals = @('Austin', 'Sacramento', 'Tallahassee', 'Albany')
$result = $capitals | Join-String -Separator ' | ' -OutputPrefix "[" -OutputSuffix "]"
Write-Output $result  # Outputs: [Austin | Sacramento | Tallahassee | Albany]

You can also transform each element before joining using the -Property or -FormatString parameters:

$states = @('Texas', 'California', 'Florida')
$result = $states | Join-String -Separator ', ' -Property { $_.ToUpper() }
Write-Output $result  # Outputs: TEXAS, CALIFORNIA, FLORIDA

Check out Concatenate String and Variable in PowerShell

Performance Considerations

As a senior PowerShell developer, you should also consider the performance.

When working with very large arrays or in performance-critical applications, the method you choose can make a significant difference. Based on my testing and real-world experience:

MethodSmall ArraysLarge ArraysMemory UsageCode Readability
-join★★★★★★★★★☆★★★★★★★★★★
String.Join()★★★★☆★★★★★★★★★☆★★★★☆
Format (-f)★★★☆☆★★☆☆☆★★★☆☆★★★☆☆
Join-String★★★☆☆★★★☆☆★★★☆☆★★★★★

Benchmark tests show addition outperforms Concat() by 50%+ for only two strings, but for joining multiple strings with delimiters, the -join operator and String.Join() method are generally the most efficient.

Read Concatenate String with NewLine in PowerShell

Real-World Examples

Now, let me show you some real-world examples of string concatenation with delimiters in PowerShell.

Generate a CSV Line

Here is an example of generating a CSV line.

$userData = @('John Smith', 'Atlanta', '555-123-4567', 'jsmith@example.com')
$csvLine = $userData -join ','
Write-Output $csvLine  # Outputs: John Smith,Atlanta,555-123-4567,jsmith@example.com

Build a SQL IN Clause

$customerIds = @(1001, 1045, 2189, 3056)
$sqlInClause = "SELECT * FROM Customers WHERE CustomerID IN ('$($customerIds -join "','")'"
Write-Output $sqlInClause
# Outputs: SELECT * FROM Customers WHERE CustomerID IN ('1001','1045','2189','3056')

PowerShell String Concatenation with Delimiters with Complex Objects

Now, here is another example of PowerShell string concatenation with delimiters with PowerShell complex objects.

Here is how to extract and join specific properties from complex objects in PowerShell:

$employees = @(
    [PSCustomObject]@{FirstName = 'John'; LastName = 'Smith'; Department = 'IT'},
    [PSCustomObject]@{FirstName = 'Mary'; LastName = 'Johnson'; Department = 'HR'},
    [PSCustomObject]@{FirstName = 'Robert'; LastName = 'Williams'; Department = 'Finance'}
)

$fullNames = $employees | ForEach-Object { "$($_.FirstName) $($_.LastName)" } | Join-String -Separator ', '
Write-Output $fullNames  # Outputs: John Smith, Mary Johnson, Robert Williams

Conclusion

String concatenation with delimiters is easy using PowerShell, using various methods.

The -join operator is my go-to method for most situations due to its simplicity and performance, but each approach has its place depending on your specific requirements and coding style.

I hope this tutorial helps you master string concatenation with delimiters in PowerShell. These techniques will serve you well whether you’re generating reports, building dynamic queries, or processing data from various sources.

You may also like the following tutorials:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

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