Mounting VHDs in Windows 7 from a command-line script

January 4th, 2012
Share

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.

  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 http://picsee.net/upload/2013-02-22/74830f3172a3.jpg 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

    @broxsoft.com
    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

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

  1. No trackbacks yet.