Mounting VHDs in Windows 7 from a command-line script
Windows 7 has native support for VHDs (virtual hard disks) built into the OS. VHDs are great for virtual machines, native VHD booting into recent Windows OSs, or even moving whole file systems around.
While you can mount VHDs from the Windows 7 diskmgmt.msc GUI, or via vhdmount, if you need support for mounting or unmounting VHDs from the command-line on a vanilla Windows 7 / Server 2008 install, you have to use diskpart.
diskpart’s mount commands are pretty simple:
C:\> diskpart DISKPART> sel vdisk file="[location of vhd]" DISKPART> attach vdisk
Unmounting is just as simple:
C:\> diskpart DISKPART> sel vdisk file="[location of vhd]" DISKPART> detach vdisk
These commands work fine on an ad-hoc basis, but I had the need to automate loading a VHD from a script. Luckily, diskpart takes a single parameter, /s, which specifies a diskpart “script”. The script is simply the command you would have typed in above:
C:\> diskpart /s [diskpart script file]
I’ve created two simple scripts, MountVHD.cmd and UnmountVHD.cmd that create a “diskpart script”, run it, then remove the temporary file. This way, you can simply run MountVHD.cmd and point it to your VHD:
C:\> MountVHD.cmd [location of vhd] [drive letter - optional]
Or unmount the same VHD:
C:\> UnMountVHD.cmd [location of vhd]
These files are hosted at Gist.Github if you want to use them or contribute changes.
Hi Nic. Thank you. I found very helpful. But there is a problem to assign drive letters. When I run – C: \> MountVHD.cmd xxxx.vhd H, the system assigns a drive letter to another, and still I get the window http://picsee.net/upload/2013-02-22/74830f3172a3.jpg you have any ideas to help me.
Strange, I haven’t seen this problem before. Do you see any errors in the Windows System or Application Event Log?
Is anything listed in regedit under HKLM\System\MountedDevices for H?
Nic, sorry for my bad English. This problem occurs only if Winodws7 virtual drive is not connected once. The first time you plug in the drive, the system installs the driver and possibly assign a drive letter itself. In Winodws 8, this is not a problem. I need to mount a virtual disk in WinPE 3.5. I’ve been looking for a solution and found your tool. Maybe you can help me write a script that requires a drive letter, but I don’t know how to do it. Thanks for the reply!
This does not work with VHDs that are protected by Bitlocker.
You can add to the script the following:
manage-bde -unlock -pw
manage-bde -unlock D: -pw
The only thing is with bitlocker, you are required to type a password so you’ll need to key it in when the script prompts you
@braxsoft.com – yes, this seems to be a problem in WIndows 10 only.
The “attach” command causes an “access denied” prompt to appear if the VHD is BitLocker-protected. Extremely annoying and doesn’t happen in earlier versions of Windows.
The access denied issue even occurs, if the VHD image isn’t BitLocker protected. What’s going wrong? Starting the script with elevated privileges (as an admin via UAC). Turning off the UAC is not a safe plan to me.
Just in case you’re interested to automate that task have a look at Autohotkey (free, open source) an its “Run”-command set (beside its other options, eg to compile your cmd-snippets).
Thank you very much!
Now when I turn on the computer, Windows 10 runs a .bat that is on startup directory. That bat runs this:
c:\MountVHD.lnk C:/myHardDisk.vhd E
The .lnk is a Shortcut to your MountVHD.cmd but with Administrator privileges.
Very easy to use, thank you!!!
Thanks for the helpful post. I know this a rather old post, but if others like me still find it when googling for this question, here an addendum: instead of using the /s parameter and temporary files, you might as well use one-liners like this:
:: mount an image
(echo select vdisk file=”[location of vhd]” & echo attach vdisk) | diskpart
:: unmount an image
(echo select vdisk file=”[location of vhd]” & echo detach vdisk) | diskpart
Works both in CMD shell and batch scripts.