Using Get-VMGuestDisk to map virtual machine drives to disks

Using Get-VMGuestDisk to map virtual machine drives to disks

VMWare’s PowerCLI version 12 includes a new commandlet, Get-VMGuestDisk, which enables sysadmins to query the VMWare API and obtain a mapping of virtual machine drives to disks. Prior to this version of PowerCLI, you had to use various workarounds to get this information.

Looking online, we were surprised that there were no readily available examples online that show how to use Get-VMGuestDisk in this way. Therefore, we provide here a working example that returns the relevant properties for each VM’s drives.

# Script to map drive letters to disks – Frontier Technology 2021-07-23

# Need to be connected to VCenter to run this script - refer to a connection script or uncomment the below line
### Connect-VIServer -Server 0.0.0.0

# Set initial variables
$resultFilePath = 'C:\PowerCLI\result.csv'
$vm = Get-VM

# Run Get-VMGuestDisk against VMs, then run those drives against Get-HardDisk
# New-Object allows you to select properties from both Get-HardDisk and Get-VMGuestDisk to include in the results.

$result = Get-VMGuestDisk -VM $vm | Foreach-Object{

$guestDisk = $_
$hardDisk = Get-HardDisk -VMGuestDisk $guestDisk

New-Object -TypeName PSObject -Property @{
        VMName = $hardDisk.Parent
        VMGuest = $guestDisk.VMGuest
    DriveLetter = $guestDisk.DiskPath
    CapacityGB = $guestDisk.CapacityGB
    FreeSpaceGB = $guestDisk.FreeSpaceGB
    DiskID = $hardDisk.ID
    DiskName = $hardDisk.Name
    VMDK = $hardDisk.Filename
    }
}

# Display result in PowerCLI console
$result

# Export result to CSV
$result | Export-CSV -Path $resultFilePath -Force –NoTypeInformation

In the PowerCLI console, the results of this particular implementation appear as follows:
PowerCLI console

Results are also exported to a CSV file.

Please note that the above script is provided as is – you may need to adapt it for your environment. Also, as documented in the PowerCLI 12 release notes, your environment must also be using vSphere 7 and each VM must be running VMware Tools 11.0 or newer, and only Windows operating systems are supported.