Get VM vhdx

Sometimes you need to get which VM that most consume the disk storage. You can check the vhdx size by inspecting the disk one by one. However, It will waste the time if there is too many VMs on the Hyper-V server. I’ve created the script that will collect all VM’s vhdx and check it’s size.

Get-VHD

On the Hyper-V module, there is Get-VHD cmdlet to gets the virtual hard disk object associated with a virtual hard disk. If you want to check the VMs on the same Hyper-V server you run the command, you will need the VmId of the VM that you want to check. Below of the example of the command.

Get-VM -VMName angga-dc1 | Select-Object VMId | Get-VHD 

Result:

PS C:\Windows\system32> Get-VM -VMName angga-dc1 | Select-Object VMId | Get-VHD 
ComputerName : WOWHVDEV1
Path : C:\ClusterStorage\HyperV-Disk1\Hyper-V\Angga\ANGGA-DC1\Virtual Disks\ANGGA-DC1-C.vhdx
VhdFormat : VHDX
VhdType : Dynamic
FileSize : 30639390720
Size : 85899345920
MinimumSize : 85898314240
LogicalSectorSize : 512
PhysicalSectorSize : 4096
BlockSize : 33554432
ParentPath :
DiskIdentifier : 6F8F1001-8E27-4253-925A-92008252EB15
FragmentationPercentage : 9
Alignment : 1
Attached : True
DiskNumber :
Number :

As you can see in the result above, there are some properties that we can use to get the report, for example, we need to get VHDType, VHDFormat, and FileSize. For the FileSize, it would be nice if we can convert the result to GB instead. Below the one-line command for that.

Get-VM -VMName angga-dc1 | Select-Object VMId | Get-VHD | select vhdtype,path,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}}

Result:

VhdType Path                                                                                  Size(GB)
------- ---- --------
Dynamic C:\ClusterStorage\HyperV-Disk1\Hyper-V\Angga\ANGGA-DC1\Virtual Disks\ANGGA-DC1-C.vhdx 29

It’s nice, right? But, how we collect the VHDSize for all VMs on the server? First, We need to get all VMs Id from the server, we can do that using Get-VM cmdlet. Let’s add it to the command above.

Get-VM | Select-Object VMId | Get-VHD | select vhdtype,path,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}}

Result:

Get All Disk Size

But, I need to get the VHDSize from all Hyper-V cluster node, how i can do that?

If you want to get all the VHD Size for all VMs on the Hyper-V cluster node, you need to get the Hyper-V cluster node first to passing the computername parameter. Below the command to get the VHDSize for All VMs on the Hyper-V node:

Get-VM -ComputerName (Get-ClusterNode) | ForEach-Object {Get-VHD -ComputerName $_.ComputerName -VMId $_.VMId} | Select -Property path,computername,vhdtype,@{label='Size(GB)';expression={$_.filesize/1gb -as [int]}}

If you like this article, please share, subscribe or you can follow our Facebook Page and Twitter.