Eric Eric - 3 months ago 54
PowerShell Question

Powershell Array to comma separated string with Quotes

I have an array that I need to output to a comma separated string but I also need quotes "". Here is what I have.

$myArray = "file1.csv","file2.csv"
$a = ($myArray -join ",")
$a


The output for
$a

ends up

file1.csv,file2.csv


My desired output is

"file1.csv","file2.csv"


How can I accomplish this?

Answer

Here you go:

[array]$myArray = '"file1.csv"','"file2.csv"'
[string]$a = $null

$a = $myArray -join ","

$a

Output:

"file1.csv","file2.csv"

You just have to get a way to escape the ". So, you can do it by putting around it '.

Comments