
I had a recent request for “virtual machine disk usage, per datastore”. While there are a number of simpler ways to get this information, most of the solutions I’ve seen do not account for VMs that a re split across different datastores, and other solutions that didn’t take into account used vs provisioned sizes.
This script solves that problem, detailing each .vmdk, its used/provisioned size, as well as the datastore it’s located on. You can then use Excel to manipulate the data further.
Use PowerCLI, change the name of the vCenter server you’re connecting to, and the rest takes care of itself.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
|
# Connect to vcenter connect-viserver "VC01"; # Create an empty array $table = @(); # Get all VMs $vms = get-vm | sort name; # Loop through each VM foreach ($vm in $vms) { # Get hard disks $harddisks = $vm | get-harddisk | sort name; # Loop through each disk in each VM foreach ($harddisk in $harddisks) { # Create an empty row $row = "" | select name, harddiskname, datastore, vmdkpath, vmdkused, vmdkallocated; $row.name = $vm.name; $row.harddiskname = $harddisk.name; $row.datastore = $harddisk.filename.split("]")[0].trimstart("["); $row.vmdkpath = $harddisk.FileName; $row.vmdkused = [int](($vm.extensiondata.layoutex.file| where {$_.name -contains $harddisk.filename.replace(".","-flat.")}).size/1GB); $row.vmdkallocated = [int]$harddisk.capacitygb; # Add row to array $table = $table + $row; } } # Export array to CSV $table | export-csv c:\temp\vmdisks.csv; |
The resulting CSV should look like this (sizes listed in GB):

