Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In powershell how to output an ArrayList to CSV

Tags:

powershell

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.

like image 901
Dennis43589y Avatar asked May 21 '26 11:05

Dennis43589y


1 Answers

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"
}
like image 110
mjsqu Avatar answered May 23 '26 22:05

mjsqu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!