PowerCLI

 View Only
  • 1.  PowerCLI - export to CSV

    Posted Jun 14, 2012 10:03 PM

    Hi everyone,

    I'm trying to run a powercli command and export the results to a csv. I run the command below, but when I open the CSV all output is added into 1 Excel column, but there are 4 columns of data. Is there a way I can get it into excel, but each of the columns (name, powerstate, num cpus, memory) are in 4 Excel columns rather than 1?

    get-vm | where-object {$_.powerstate -eq "poweredoff"} > c:\temp\poweredoffvms.csv

    Thanks,

    Scott



  • 2.  RE: PowerCLI - export to CSV
    Best Answer

    Posted Jun 14, 2012 10:52 PM

    If its possible check bellow line

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Export-Csv -Path c:\poweredoff-vms.csv -NoTypeInformation

    And if you want just few items you can filter them by using "Select"

    Get-VM | Where-Object {$_.PowerState -eq "PoweredOff"} | Select Name,PowerState,NumCPU,MemoryMB | Export-Csv -Path c:\poweredoff-vms.csv -NoTypeInformation

    For exporting csv file you can use "Export-Csv"



  • 3.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 02:23 PM

    Thanks for your help. That works perfect.

    Is there a reason why when I use ">" to send the output to a file, it looks the same as it does in the powercli interface, however when I use export-csv, it exports tons of columns of data? Just curious.

    Thanks,

    Scott



  • 4.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 03:24 PM

    Yes, the Export-Csv cmdlet reads the objects you place on the pipeline and converts them to the correct CSV format.

    With the redirect (>), all is converted to 1 long string, and Excel considers this as 1 cell per line.



  • 5.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 03:36 PM

    You can use ">>" to wrap each line you throw.



  • 6.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 03:39 PM

    I think '>>' is used to append the data to an existing file.

    And '>' will overwrite the file if it exists.



  • 7.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 03:52 PM

    yeah!



  • 8.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 09:08 PM

    Thanks everyone for the help.



  • 9.  RE: PowerCLI - export to CSV

    Posted Jun 15, 2012 04:44 AM

    The previous reply is correct, pipe the objects to the Export-Csv cmdlet instead of redirecting them to a file.

    One more remark, it's useful to add the UseCulture parameter on the Export-Csv cmdlet, that way the CSV will be created with the seperator as defined in your regional settings.