Script: How to get VM Disk Actual Usage, or VMDK UsedSpace Report Of Actual Datastore Usage using PowerCLI or Powershell

Ever wondered how to get the actual vmdk usage report on OS level utilising vmtools? This is a quick blogpost which you might find useful to get the results wanted.

$cred​​ =​​ Get-Credential
$vCenters​​ =​​ "lab-vc01.lab.local",​​ "lab-vc02.lab.local"
Connect-VIServer​​ $vCenters​​ -Credential​​ $cred

$global:DefaultVIServers​​ |​​ Select​​ Name,Version​​ |​​ ft​​ -a ​​​​ 

Get-VM |
Select Name, PowerState,
@{N="VMDK UsedSpace(GB)";E={[math]::Round(($_.Guest.Disks | %{
      $_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum |
      Select -ExpandProperty Sum),1)}}, 
@{N="VMDK FreeSpace(GB)";E={[math]::Round(($_.Guest.Disks | %{
      $_.FreeSpaceGB} | Measure-Object -Sum |
      Select -ExpandProperty Sum),1)}},
@{N="VMDK Capacity (GB)";E={[math]::Round(($_.Guest.Disks | %{
      $_.CapacityGB} | Measure-Object -Sum |
      Select -ExpandProperty Sum),1)}},
@{N="Datastore";E={Get-Datastore -vm $_}},
@{N="Cluster";E={Get-Cluster -vm $_}},
@{N="VMHost";E={Get-VMhost -vm $_}},
@{N="vCenter";e={(($_.Uid).split("@")[1]).split(":")[0]}}​​ | export-csv path C:\Temp\VMDiskUsageReport.csv -NoTypeInformation  

Results of above script as follows showing the actual usage space of the VM and not just the provisioned space.

Alternatively, you can create a report similar to below

$cred​​ =​​ Get-Credential
$vCenters​​ =​​ "lab-vc01.lab.local",​​ "lab-vc02.lab.local"
Connect-VIServer​​ $vCenters​​ -Credential​​ $cred

$global:DefaultVIServers​​ |​​ Select​​ Name,Version​​ |​​ ft​​ -a ​​​​ 


$Report = @()

Get-Cluster Cluster | Get-VM |?{$_.PowerState -like "*On*"} | %{
  $ReportRow = "" | Select-Object VMName,PowerState,NumCPU, MemoryGB, UsedSpace, DiskCapacity,DiskFreespace
  $ReportRow.VMName = $_.Name
  $ReportRow.PowerState = $_.PowerState
  $ReportRow.NumCPU = $_.NumCPU
  $ReportRow.MemoryGB = $_.MemoryGB
  #$ReportRow.UsedSpace = (($_.Guest.Disks| %{$_.CapacityGB - $_.FreeSpaceGB} | Measure-Object -Sum | Select -ExpandProperty Sum),1)
  $ReportRow.UsedSpace = $ReportRow.DiskCapacity - $ReportRow.DiskFreespace
  $ReportRow.DiskCapacity = $_.Guest.Disks | Measure-Object CapacityGB -Sum | Select -ExpandProperty Sum
  $ReportRow.DiskFreespace = $ReportRow.DiskCapacity - ($_.Guest.Disks | Measure-Object FreeSpaceGB -Sum | Select -ExpandProperty Sum)
  $Report += $ReportRow
}

$Report | export-csv path C:\Temp\VMDiskUsageReport.csv -NoTypeInformation  

You May Also Like

About the Author: VirtuallyThatGuy

Leave a Reply

Your email address will not be published. Required fields are marked *