≡ Menu

Get VMware VM Disk size using powershell

In this post, I will talk about querying the disk size details of a VMs in VMware ESX environment.

First of all you need to connect to Virtual Center where your VMs are hosted. As you already know, use the below command from PowerCLI console.

Connect-VIServer -Server VCServer1

Once connected to VC, we need to get the VirtualMachine Object of VM that we want to query for disk size details.

$VMObj = Get-VM -Name $VMName

Above command will get the Object of Virtual Machine. In previous version of PowerCLI, the disk details are generally part of VM object returned by Get-VM cmdlet. The latest versions are a bit different, and you need to use Get-HardDisk cmdlet.

$Disks = Get-Harddisk -VM $VMObj

The Get-HardDisk cmdlet returns array of disks that are connected to the VM. To get the details of each disk, you need to loop through the returned array and get the required properties.

So, here is the complete combined code:

function Get-VMDisks {            
[cmdletbiding()]            
param(            
[string]$VMName            
)            
$VMObj = Get-VM -Name $VMName            
$Disks = Get-Harddisk -VM $VMObj            
foreach($disk in $Disks) {            

$DiskName = $Disk.Name            

$DisksizeinGB = ($Disk.CapacityKB * 1024)/1GB            

"{0} : {1}" -f $DiskName, $DisksizeinGB            

}            
}

Hope this helps…

Comments on this entry are closed.

  • Jayesh July 2, 2015, 6:58 pm

    This worked. Thanks.