Finding the drive letter of a mounted VHD
In Windows 8/2012 you can mount a VHD into the file system. Is there a way to discover the drive letter of the mounted VHD
function get-mountedvhdDrive {
$disks = Get-CimInstance -ClassName Win32_DiskDrive | where Caption -eq "Microsoft Virtual Disk"
foreach ($disk in $disks){
$vols = Get-CimAssociatedInstance -CimInstance $disk -ResultClassName Win32_DiskPartition
foreach ($vol in $vols){
Get-CimAssociatedInstance -CimInstance $vol -ResultClassName Win32_LogicalDisk |
where VolumeName -ne 'System Reserved'
}
}
}
Use Get-CimInstance to get the Win32_DiskDrive class. Filter on caption equalling "Microsoft Virtual Disk"
for each “physical” disk returned get the associated Win32_Volume and use that to get the associated Win32_Logical disk where you will find the drive letter.
Nice example of using associations in the CIM cmdlets