3

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.

1
  • 2
    You need to force your inner arrays to enumerate before sending to the CSV command. $csvarraylist | foreach-object {$_} | export-CSV output.csv -notype Commented Jan 27, 2020 at 3:32

1 Answer 1

1

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"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.