Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 898
Eric Avatar asked Sep 01 '16 16:09

Eric


People also ask

How do I convert an array to a string in PowerShell?

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.

How do you split a string in PowerShell?

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.

How do you use the join function in PowerShell?

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.


1 Answers

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 '.

like image 173
Syphirint Avatar answered Sep 28 '22 16:09

Syphirint