Today’s oneliner is an incredibly fast way to check the usage of your VMware datastores. You should first connect to Virtual Center in the following way:
$VC = Connect-VIServer “YourVCServerName”
Here comes the oneliner:
Get-Datastore | Sort-Object Name | %{Get-View $_.Id} | Format-Table @{Label=”Name”;Expression={$_.info.name}}, @{Label=”NumVMs”;Expression={$_.vm.length}}
Only interested in datastores that are not used? Use the Where-Object cmdlet:
Get-Datastore | Sort-Object Name | %{Get-View $_.Id} | Where {$_.vm.length -eq 0} | Format-Table @{Label=”Name”;Expression={$_.info.name}}, @{Label=”NumVMs”;Expression={$_.vm.length}}
But watch out! Consider a vm with a disk in datastore A and the vmx in datastore B. When you create a snapshot of the vm, the delta files of all disks will be stored with the vmx (in datastore B). The script (as does the VI Client) will show the vm to be not connected to datastore A!
Enjoy!