Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

Hi all,

In this blog I will explain an easy way to generate a Virtual Machine inventory and export it to a csv file. We will be using the “Get-VM” cmdlet and piping it to the “Export-csv” cmdlet to get the information we need in the examples below. To do anything with PowerShell in VMware you will need to install VMware vSphere PowerCLI. You will then need to open PowerCLI connect to your VMWare vCenter server by typing “Connect-VIServer vCenterServerName”.

The simplest way to get a VM inventory with lots of useful information is to type the following command replacing the path with the directory and file name you would like to use.

Get-VM | Export-Csv -path “c:\reports\vminventory.csv” –NoTypeInformation

The command above will dump a list of your VMs to a csv file containing the following fields:

PowerState, Version, Description, Notes, Guest, NumCpu, MemoryMB, MemoryGB, HardDisks, NetworkAdapters, UsbDevices, CDDrives, FloppyDrives, Host, HostId, VMHostId, VMHost, VApp, FolderId, Folder, ResourcePoolId, ResourcePool, PersistentId, UsedSpaceGB, ProvisionedSpaceGB, DatastoreIdList, HARestartPriority, HAIsolationResponse, DrsAutomationLevel, VMSwapfilePolicy, VMResourceConfiguration, Name, CustomFields, ExtensionData, Id, Uid, Client

Now let’s say this you didn’t need this much information in your report. You could type the following command to get the output with only the fields you need. In the following example we will only export the Name, Description, PowerState, NumCpu and MemoryGB fields.

Get-VM | select Name, Description, PowerState, NumCpu, MemoryGB | Export-Csv -path “c:\reports\vminventory.csv” -NoTypeInformation

If you needed to sort the spreadsheet by one of the fields for example the “Name” field, you can type the following command:

Get-VM | select Name, Description, PowerState, NumCpu, MemoryGB | sort Name | Export-Csv -path “c:\reports\vminventory.csv” –NoTypeInformation

The example above will create a csv with the Name, Description, PowerState, NumCpu and MemoryGB fields and sort the list by Name in alphabetical order.

I hope this helps. As always, if you have any questions or feedback, please leave a comment.

Related Links:

Using PowerShell to get a list Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to Gracefully Shut Down Multiple Virtual Machines in VMware

Using PowerShell to Remove Virtual Machine Snapshots in VMware ESXi 4.1

Using PowerShell to View Datastore and Virtual Machine Space Usage Information in VMware

6 Responses to Using PowerShell to create a Virtual Machine Inventory in VMware and Export it to a CSV File

  1. Steve Chung says:

    just wonder, do you know how to use powershell to add VM to vcenter intead of right click on the host in vcenter and select add to inventory. I am talking about adding vmx file here.

    Thanks

    Best Regards,

    Steve Chung

  2. BC says:

    Hi Steve,

    I just tried this and it worked for me. Run the following command replacing the path parameter with the path to your vmx file and the vmhost parameter with the path to your esx host. Hope this helps.

    New-VM -VMFilePath “[Datastore_Name] VMFOLDER/VMMACHINE.vmx” -VMHost ESXHost.priv

  3. s.sivakumar says:

    the memory GB parameter isn’t working in the command Get-VM | select Name, Description, PowerState, NumCpu, MemoryGB . I tried in Vsphere 5.0

  4. Almero says:

    Hi Guys , need help to gather the VMX path per VM , to loop it for NEW-VM command . The format of the VMX varailble must be

    “[Datastore_Name] VMFOLDER/VMMACHINE.vmx”

    But with Get-VM , VMXPATH is not a standard one you can select ..
    How can a pull that vmx path from a list of VMs in CSV and export to VMXpath.csv ?

    Examples from for Example LucD is too advanced for me , I am new to this 🙁

Leave a Reply

Your email address will not be published. Required fields are marked *