Just posting it here so I have a place to refer to than Google every time.
Got an array of elements.
1 |
$array = 1,2,3,4,5 |
I want to join these with double quotes around each. That is, I want "1","2","3",...
Option 1
1 |
'"' + $($array -join '","') + '"' |
This joins the array elements with “,” and then adds a ” to the beginning of the first element and end of the last element.
Option 2
1 |
'"{0}"' -f ($array -join '","') |
Same idea as above, but I add the ” a different way to the beginning and end. Neater looking method.
If you want to add curly braces within the '"{0}"'
add them twice. Example: '"{{ {0} }}"'
Option 3
1 |
$array -replace '^.*$', '"$&"' -join "," |
This one adds “s to each array element, then joins them with commas. :)
Something else
I like this last method coz I can easily change it to add newlines too.
1 |
$array -replace '^.*$', '"$&"' -join ",`n" |
Can do with others too, but since I am using single quotes I’ll have to change that. For example:
1 |
'"' + $($array -join """,`n""") + '"' |
Too many double quotes in there. :) I have to add it twice to escape it within the outer double quotes.