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
By: Adam on September 13, 2013 at 12:32 am
An easier command is: (Mount-VHD –Path c:\test\testvhdx –PassThru | Get-Disk | Get-Partition | Get-Volume).DriveLetter
By: RichardSiddaway on September 13, 2013 at 12:55 pm
Mount-VHD is only available in the Hyper-V module. You can mount a vhd from file explorer in Windows 12/8 so my approach was designed to be generic and not assume the presence of the Hyper-V module.
It was also a good demonstration of CIM class associations