Powershell - get datastore disk usage rather than total VM disk usage

Hi.  Thanks to LucD I am progressing with my script to get vCenter details but the one bit I am stuck on is with regard to the datastore disk usage particularly if a VM has more than one VMDK and they are on separate datastores.

At the moment, I am using the code below to get the datastore names:

(Get-View -Id $vm.Datastore -Property Name).Name -join "|"

and the code below to get the VM diskspace usage:

[math]::Round(($vm.Storage.PerDatastoreUsage.Committed | Measure-Object -Sum).Sum/1GB,1)

What I would like to do is get the disk size for each VMDK.  For example if a VM has 2 x VMDKs on separate datastores, I would like the report to show:

datastore1          50Gb

datastore2          30Gb

Any ideas please?

Tags (2)
0 Kudos
1 Reply

You mean something like this?

Note that for Thin harddisk,the capacity property doesn't reflect what the harddisk is actually using on that datastore.

I show a method for that in my Yadr – A Vdisk Reporter post.

Get-View -ViewType VirtualMachine -PipelineVariable vm |

ForEach-Object -Process {

    $vm.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualDisk]} |

    ForEach-Object -Process {

        New-Object PSObject -Property @{

            VM = $vm.Name

            HD = $_.DeviceInfo.Label

            Datastore = (Get-View -Id $_.Backing.Datastore -Property Name).Name

            CapacityGB = [math]::Round($_.CapacityInBytes/1GB,1)




Blog:  Twitter: @LucD22  Co-author PowerCLI Reference