I do this all the time. Turn a list of items or an array of strings into a comma-separated string. Or delimited by another character, a space, or even just smashed together.

So how do you turn a list or array of strings into a delimited string? $ArrayOfStrings -join "," or "One", "Two" -join ","

Use different delimiter or no delimiter

You don’t have to use a comma; you can use anything or nothing as a delimiter. The delimiter can be more than one character, such as a comma plus a space. I’ve even found occasional use to join strings with one or more words.

Let’s set up a simple string array for the following examples.

$ArrayOfStrings = @(
    "Let's",
    "join",
    "strings",
    "together"
)

Sometimes you just want to join with a space which turns this example into a sentence. This outputs “Let’s join strings together”:

$ArrayOfStrings -join " "

If you need to tab-separate values, use the backtick escape for tab:

$ArrayOfStrings -join "`t"

You may sometimes want to just concatenate the strings with no separator. This assigns “Let’sjoinstringstogether” to $MyVariable, to show you a slightly different use case:

$MyVariable = $ArrayOfStrings -join ""

This example is a bit silly, but I know I’ve found a real use for longer “delimiters” before. This makes “Let’s <== long separator ==> join <== long separator ==> strings <== long separator ==> together”:

$ArrayOfStrings -join " <== long separator ==> "

Turn a list into one string

I often have a list of items I want to turn into a comma-separated string, and I find using PowerShell is the quick an easy way. I just copy-paste the list into a here string which is a multi-line string beginning with @" and ending with "@ at the start of a line. Here is a friendly-to-read example. I use backtick escaping to split the here string on newlines, but I sometimes then need the .Trim() method depending on the source of the list to strip out extra newlines.

$MyList = @"
alpha
beta
gamma
delta
"@

$MyCleanedUpList = $MyList.Split("`n") | ForEach-Object {
    $PSItem.Trim()
}

$MyCleanedUpList -join ","

But that’s my fancy to-share version. Usually I’m quickly hacking something like this:

@"
alpha
beta
gamma
delta
"@.Split("`n") -join ","

Or this if there are newlines in the output. Just copy your list in place of mine.

(@"
alpha
beta
gamma
delta
"@.Split("`n") | %{ $_.Trim() }) -join ","

Sometimes I copy from a bullet list and the dots get pasted as text. I sometimes find it faster to use -replace , .TrimStart(), or .SubString() to edit the garbage out. I’ll give three examples using this data. I had an html email that did this weird dot and indent thing when I copy/pasted:

$BulletTextList = @"
·         alpha
·         beta
·         gamma
·         delta
"@

Here I’m -replaceing (with nothing since I left off the second parameter) before I .Trim() so I don’t have to get the white space right. I copied/pasted the unicode bullet from the pasted text since it’s not a keyboard character.

($BulletTextList.Split("`n") | %{ ($_ -replace '·').Trim() }) -join ","

If you prefer just chopping a specified number of characters, use .SubString().

($BulletTextList.Split("`n") | %{ $_.Substring(10) }) -join ","

If you dislike the regex of -replace you can use one of the trims like .TrimStart() with the list of characters you want to trim. In this case I’m putting the unicode bullet point (copied and pasted from the list to make sure I have the right one) and a space as the characters to remove from the beginning.

($BulletTextList.Split("`n") | %{ $_.TrimStart('· ') }) -join ","

Join lines from a file

Joining a list from a file I find easier than my copy/paste hacks above. Since Get-Content returns a stream or array of strings, one per line of the file, it’s really easy to join, and I never have extra newlines like I do sometimes when copying and pasting.

Assume the file “in.txt” has these contents and is in the current working directory:

first
second
third
fourth

Yes, it’s this easy.

(Get-Content in.txt) -join ","

If you want to make it harder or think this is easier to read:

$InContents = Get-Content in.txt
$InContents -join ","

Sometimes you have a header line in the file you want to skip. Use Select-Object or its alias select:

($InContents | select -Skip 1) -join ","

Put quotes around each string and then comma-separate them

The -join operator wasn’t really intended for this, but with a little coercion I find it easier to use than other formatting options. Let’s start with a list of phrases with spaces that we want to turn into a CSV.

$PhraseList = @(
  "one fish",
  "two fish",
  "red fish",
  "blue fish"
)

We can join those just as well, but it will be “one fish,two fish,red fish,blue fish” which many CSV readers will dislike because there are no quotes. So let’s join with quote-comma-quote and then tack on a quote at the front and back.

'"' + ($PhraseList -join '","' ) + '"'

Note that in PowerShell you can put single quotes on the outside and use unescaped double quotes on the inside, but you can also reverse that and put double quotes on the outside and single quotes on the inside. But if you need both single and double quotes inside, use double quotes on the outside and backtick-escape inner instances of double quotes, because single outer quotes will prevent you from backtick-escaping at all.

That’s not necessary in this case, but here’s what that looks like:

"`"" + ($PhraseList -join "`",`"" ) + "`""

If you found this helpful, you might also like my article on commenting in PowerShell.