VMware Cloud Community
scotty_p
Enthusiast
Enthusiast
Jump to solution

PowerCLI - export to CSV

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

0 Kudos
1 Solution

Accepted Solutions
Kasraeian
Expert
Expert
Jump to solution

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"

If you found this note/reply useful, please consider awarding points for "Correct" or "Helpful" If there's any mistake in my notes, please correct me! Sohrab Kasraeianfard | http://www.kasraeian.com | @Kasraeian

View solution in original post

8 Replies
Kasraeian
Expert
Expert
Jump to solution

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"

If you found this note/reply useful, please consider awarding points for "Correct" or "Helpful" If there's any mistake in my notes, please correct me! Sohrab Kasraeianfard | http://www.kasraeian.com | @Kasraeian
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

scotty_p
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

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

0 Kudos
LucD
Leadership
Leadership
Jump to solution

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

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
GuilhermeStela
Enthusiast
Enthusiast
Jump to solution

yeah!

0 Kudos
scotty_p
Enthusiast
Enthusiast
Jump to solution

Thanks everyone for the help.

0 Kudos