How to Join an Array with a Comma in PowerShell?

I will explain how to join an array with a comma in PowerShell in this PowerShell tutorial. This is a common task when you need to convert an array into a string to display it in a readable format, or to prepare it for export to a CSV file or another system that requires comma-separated values.

To join an array with a comma in PowerShell, use the -join operator. For example, $joinedString = $array -join ‘,’ will concatenate all elements of $array into a single string, separated by commas. This is a simple and effective way to create a comma-separated list from an array.

How to Join an Array with a Comma in PowerShell

There are different methods to join an array with a comma in PowerShell. Let us check every method with examples.

Basic Join with -join Operator

The simplest way to join an array with a comma is to use the -join operator in PowerShell. This operator takes all the elements of an array and concatenates them into a single string, with each element separated by whatever character or string you specify.

Here’s an example:

# Define an array
$array = 'apple', 'banana', 'cherry'

# Join the array elements with a comma
$string = $array -join ','

# Display the result
$string

Output:

apple,banana,cherry

In this example, we defined an array $array with three fruit names. We then used the -join operator to concatenate the elements, separating them with a comma. The resulting string is stored in the $string variable.

You can see the output in the screenshot below after I executed the script using Visual Studio Code.

Join an Array with a Comma in PowerShell

Adding Quotes to Each Element

Sometimes, you might want to have each element in the resulting string quoted. This is particularly useful if your elements contain spaces or other characters that could cause issues in CSV files or command-line arguments.

Here’s how to do it:

# Define an array
$array = 'file1.csv', 'file2.csv', 'file3.csv'

# Join the array elements with a comma, adding quotes to each element
$string = $array -join '","'

# Add the initial and final quotes
$string = '"' + $string + '"'

# Display the result
$string

Output:

"file1.csv","file2.csv","file3.csv"

In this example, each element of the array is quoted individually, and then the whole string is enclosed in quotes.

Using ForEach-Object and Join-String

For more complex scenarios or when you need more control over the formatting, you can use ForEach-Object along with the Join-String cmdlet in PowerShell. This method is a bit more verbose but can be very flexible.

# Define an array
$array = 'apple', 'banana', 'cherry'

# Use ForEach-Object to process each element
$string = $array | ForEach-Object { '"' + $_ + '"' } | Join-String -Separator ','

# Display the result
$string

Output:

"apple","banana","cherry"

Here, $_ represents the current object in the pipeline, which, in this case, is each element of the array. We add quotes around each element and then use Join-String to combine them with a comma separator.

Using a Custom Function

If you need to join arrays with commas in PowerShell frequently, you might want to create a custom function to simplify the process. Here’s a simple function that you can add to your PowerShell profile or scripts:

function Join-ArrayWithComma {
    param (
        [String[]]$Array
    )
    $Array -join ','
}

# Define an array
$array = 'apple', 'banana', 'cherry'

# Use the custom function to join the array
$string = Join-ArrayWithComma -Array $array

# Display the result
$string

Output:

apple,banana,cherry

This custom function Join-ArrayWithComma takes an array as input and joins its elements with a comma. You can then call this function whenever you need to perform this operation.

Conclusion

Joining an array with a comma in PowerShell is easy and can be accomplished in several ways like by using -join operator, add quotes to each element, utilize ForEach-Object with Join-String, or create a custom function etc.

In this PowerShell tutorial, I have explained how to join an array with a comma in PowerShell using various methods.

You may also like: