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.