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?
Use " " to Convert an Array Object to String in PowerShell Copy $address = "Where", "are", "you", "from?" You can check the data type using the GetType() method. When you encode the array variable with " " , it will be converted into a string.
Split() function. The . Split() function splits the input string into the multiple substrings based on the delimiters, and it returns the array, and the array contains each element of the input string. By default, the function splits the string based on the whitespace characters like space, tabs, and line-breaks.
To use the unary join operator, enclose the strings in parentheses, or store the strings in a variable, and then submit the variable to join.
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 '
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With