1

I am trying to export a list in object into CSV, but my output is:

Hd M.d.o. Montagem Eixo Traseiro,"System.Collections.ArrayList"

The object is:

Class Departamento

{
    [String]$nome
    [Object]$pessoas

}

So, the first is a string and the second is a ArrayList(Object). I want to know how to export my arrayList, that has the name of all persons of my department. Thanks.

1 Answers1

2

You should convert your object [Object]$pessoas to a string or us XML or JSON format instead of CSV. Because CSV can't display the object as you wish (CSV has only "one level").
If you would like to use JSON:

#Export
$obj | ConvertTo-Json -Depth 99 | Out-File -FilePath C:\Temp\test.json #-Depth 99 <-- Needed for the ArrayList(Object)

#Import
Get-Content -Path C:\Temp\test.json | ConvertFrom-Json

If you have to use a csv format you could do it like that (convert the list with -join to a string):

$obj | Select-Object -Property nome, @{n = 'pessoas'; e = {$_.pessoas -join ';'}} | Export-Csv -Path C:\Temp\test.csv
Patrick
  • 2,128
  • 16
  • 24