Dennis43589y
Dennis43589y

Reputation: 229

In powershell how to output an ArrayList to CSV

I am trying to export an ArrayList object to a CSV file. The object is a list of 3 element arrays.

I have been trying something like the following however I just get information about the object (number of elements, length, etc).

$CsvArrayList | Export-Csv "./Output.csv"

Is it possible to output the values contained in an array list into csv format? Ideally one line per array and one element per cell.

Upvotes: 3

Views: 13504

Answers (1)

mjsqu
mjsqu

Reputation: 5452

You could create the CSV manually using -join, however this may be slow if there are lots of arrays:

$CSVArrayList = new-Object System.Collections.ArrayList

[void]$CSVArrayList.Add(@('1','2','3'))
[void]$CSVArrayList.Add(@('4','5','6'))

Set-Content "./Output.csv" -Value $null

Foreach ($arr in $CSVArrayList) {
      $arr -join ',' | Add-Content "./Output.csv"
}

Upvotes: 1

Related Questions