Tag Archives: DiskSpace

Analyze your VHD(x) usage with PowerShell

In this case, I wanted a quick analysis on my Hyper-V VM’s vhd files and get the disk size and free disk space before upgrading to Server 2012 R2 Hyper-V.

The snippet basically loops trough all VM’s on a host (or in a cluster if you would want that), and gives you output as below.

2014-01-11 21-01-04

2014-01-11 21-01-23

If you want information on all disks for all VM’s in a cluster you can get that with Get-VHDStat -Cluster ClusterName.

Get-VHDStat

function Get-VHDStat {
param(
[Parameter(Mandatory=$false)]
[string]
$Cluster
)

if ($Cluster) {
    $VMs = Get-ClusterGroup -Cluster $cluster | where grouptype -eq 'virtualmachine' | Get-VM
} else {
    $VMs = Get-VM 
}
	foreach ($VM in $VMs){
		$VHDs=Get-VHD $vm.harddrives.path -ComputerName $vm.computername
            foreach ($VHD in $VHDs) {
        	    New-Object PSObject -Property @{
                    Name = $VM.name
				    Type = $VHD.VhdType
				    Path = $VHD.Path
				    'Total(GB)' = [math]::Round($VHD.Size/1GB)
                    'Used(GB)' = [math]::Round($VHD.FileSize/1GB)
				    'Free(GB)' =  [math]::Round($VHD.Size/1GB- $VHD.FileSize/1GB)
                 }
	        }
    }
}

Enjoy!

/Johan