Mounting VHDs in Windows 7 from a command-line script

January 4th, 2012

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.

Share this:

  1. Sergei Belous
    February 22nd, 2013 at 02:14 | #1

    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 you have any ideas to help me.

    • February 22nd, 2013 at 06:42 | #2

      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?

  2. Sergei Belous
    February 22nd, 2013 at 10:10 | #3

    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!

  3. July 13th, 2015 at 16:40 | #4

    This does not work with VHDs that are protected by Bitlocker.

  4. Anonymous
    November 24th, 2016 at 08:10 | #5
    You can add to the script the following:
    manage-bde -unlock -pw

    For example
    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

  5. gugu
    February 14th, 2017 at 08:29 | #6 – 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.

  6. Zoli1972
    May 1st, 2017 at 11:57 | #7

    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.

  7. May 28th, 2017 at 22:37 | #8

    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).

    Greetz :o)

  8. Alain
    March 30th, 2018 at 00:58 | #9

    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!!!

  9. Valentin
    December 15th, 2018 at 10:42 | #10

    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.

  1. No trackbacks yet.