Categories

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

2 Responses to Finding the drive letter of a mounted VHD

  • Adam says:

    An easier command is: (Mount-VHD –Path c:\test\testvhdx –PassThru | Get-Disk | Get-Partition | Get-Volume).DriveLetter

  • RichardSiddaway says:

    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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>