3

I am new to PowerShell and trying to get a list of VM names and their associated IP Addresses from within Hyper-V.

I am getting the information fine on the screen but when I try to export to csv all I get for the IP Addresses is System.Collections.Generic.List`1[System.String] on each line.

There are suggestions about "joins" or "ConvertTo-CSV" but I don't understand the syntax for these.

Can anyone help?

This is the syntax I am using...

Get-VM | Select -ExpandProperty VirtualNetworkAdapters | select name, IPV4Addresses | Export-Csv -Path "c:\Temp\VMIPs.csv"
mklement0
  • 382,024
  • 64
  • 607
  • 775
G Beach
  • 115
  • 1
  • 3
  • 10
  • if you save the output of the 2nd `Select-Object` to a $Var and do `$Var.GetType()` on it, what you you see? what about `$Var.IPV4Addresses.Get-Type()`? – Lee_Dailey Mar 11 '19 at 16:18
  • 2
    Did you know `Export-Csv` has a very useful switch called [NoTypeInformation](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/export-csv?view=powershell-6) ? – Theo Mar 11 '19 at 18:38

2 Answers2

2

If an object you export as CSV with Export-Csv or ConvertTo-Csv has property values that contain a collection (array) of values, these values are stringified via their .ToString() method, which results in an unhelpful representation, as in the case of your array-valued .IPV4Addresses property.

To demonstrate this with the ConvertTo-Csv cmdlet (which works analogously to Export-Csv, but returns the CSV data instead of saving it to a file):

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | ConvertTo-Csv
"col1","col2"
"1","System.Object[]"

That is, the array 2, 3 stored in the .col2 property was unhelpfully stringified as System.Object[], which is what you get when you call .ToString() on a regular PowerShell array; other .NET collection types - such as [System.Collections.Generic.List[string]] in your case - stringify analogously; that is, by their type name.


Assuming you want to represent all values of an array-valued property in a single CSV column, to fix this problem you must decide on a meaningful string representation for the collection as a whole and implement it using Select-Object with a calculated property:

E.g., you can use the -join operator to create a space-separated list of the elements:

PS> [pscustomobject] @{ col1 = 1; col2 = 2, 3 } | 
      Select-Object col1, @{ n='col2'; e={ $_.col2 -join ' ' } } |
        ConvertTo-Csv
"col1","col2"
"1","2 3"

Note how array 2, 3 was turned into string '2 3'.

mklement0
  • 382,024
  • 64
  • 607
  • 775
0

OtherObjectPipedStuff | Select-object name,IPV4Addresses | export-csv PP.csv -NoTypeinformation

Ernst
  • 1,125
  • 1
  • 7
  • 4
  • There's no meaningful difference between the OP's own attempt and your command (except the addition of `-NoTypeInformation`, which is _incidental_ to the problem at hand). Also, please [format your post properly](https://stackoverflow.com/help/formatting). – mklement0 Nov 12 '22 at 17:12