Mounting a Virtual Hard Disk with Hyper-V

You can mount a virtual hard disk under the parent partition with Hyper-V and a simple script:

VBScript:

Option Explicit
 
Dim WMIService
Dim VHDService
Dim VHD
 
'Specify the VHD to be mounted
VHD = "F:\Windows.vhd"
 
'Get instance of 'virtualization' WMI service on the local computer
Set WMIService = GetObject("winmgmts:\\.\root\virtualization") 
 
'Get the MSVM_ImageManagementService
Set VHDService = WMIService.ExecQuery("SELECT * FROM Msvm_ImageManagementService").ItemIndex(0)
 
'Mount the VHD
VHDService.Mount(VHD)

PowerShell:

#Specify the VHD to be mounted
$VHDName = "F:\Windows.vhd"
 
#Get the MSVM_ImageManagementService
$VHDService = get-wmiobject -class "Msvm_ImageManagementService" -namespace "root\virtualization" -computername "."
 
#Mount the VHD
$Result = $VHDService.Mount($VHDName)

This is a very straightforward script.  You just get a MSVM_ImageManagementService object and call the "Mount" method on it.  To unmount the virtual hard disk you run the same code but call "Unmount" instead.

Note that the virtual hard disk will be offline when it is mounted, so you will need to online the disk under the Server Manager after running this script.

Cheers,

Ben