Automatically Attach VHD Images

Martin Brinkmann
Jan 14, 2010
Updated • Jun 2, 2016
Tutorials, Windows tips

The Windows 7 operating system comes with (a new) option to create virtual hard drive [vhd] images that can be mounted in the operating system after creation. It is naturally also possible to mount images not created on the target system as well.

This works pretty much the same way that iso images and other disk images can be mounted in an operating system (usually using third-party software).

VHD images are automatically attached after their creation but never again automatically after the computer system has been rebooted. This is a nuisance for users who need to work regularly with the same vhd image on the computer system.

An easy option to automatically attach vhd images is to use the diskpart command line utility which is part of the Windows 7 operating system. Diskpart is a powerful tool to enable storage configurations from the command line (as well as remote sessions and scripts).

Diskpart can be launched manually by typing in diskpart in the run box in the Windows 7 start menu which will open a new command line window with Diskpart launched.

Update: If you run Windows 8 or newer, tap on the Windows key, type diskpart and hit the enter key. Diskpart will launch as it is included in newer versions of Windows as well.

The first command specifies the vhd image that you want to attach to Windows 7 or newer versions of the Windows operating system.

You can copy the command below but need to adjust the path to the VHD file and the file name of it manually.

select vdisk file=c:\pathtovhdfile

The second command attaches the specified vhd image. What happens here is that Diskpart remembers the VHD image that you have selected using the first command, and uses the information to attach it to the system.

attach vdisk

This is the manual process. To automate the process of attaching a vhd to the system do the following:

  1. Create a new text document and place the two commands from above in it.
  2. You can add those two lines again for every vhd image that should be automatically mounted during system start.
  3. Save the file as vhdattach.s.
  4. Create a new text document and add the following line to it: diskpart -s "c:\pathtovhdattach.s". Make sure you change the path to the location you saved the vhdattach.s file to, and make sure it is accessible on the system during start.
  5. Rename the text document to diskpart.cmd and place this file in the startup folder of the Windows start menu.You find the startup folder by loading %appdata%\Microsoft\Windows\Start Menu\Programs\Startup in Windows Explorer.

Diskpart.cmd will be executed on startup of the operating system. It will process the commands in vhdattach in diskpart so that the vhd images will be attached to the system automatically on each start of the PC.

It may be necessary on the first run to go into the disk management tool to format the vhd drives in Windows so that drive letters are assigned to them. The Disk Management tool can also be used to detach vhd images. To run Disk Management, tap on the Windows-key, type diskmgmt.msc and hit enter.

Automatically Attach VHD Images
Article Name
Automatically Attach VHD Images
The article explains how you can mount VHD images automatically on a system running Windows 7 and newer versions of Windows.
Ghacks Technology News

Previous Post: «
Next Post: «


  1. Søren said on April 24, 2018 at 10:07 am

    How do you prevent Disk Management from asking what to do with the attached drive every time it is mounted? It’s pretty annoying and AutoPlay settings are no help…

  2. emt23 said on March 8, 2012 at 9:13 pm

    How did you set it to “run as administrator”?

    I can take ownership of any folder and I am familiar with setting permissions and control levels, but when I place the shortcut in the startup folder. I can’t set it to “run as administrator ” and I can’t do it for my .s or .cmd file either. I’m a little concerned about using regedit to modify the default permission level of “all .s and . cmd extensions” opened with powershell, cmd, or diskpart.
    My present configs work fine as follows;

    sel vol 6
    sel vdisk file=z:\ReadyBoost_VHD_1.vhd.
    attach vdisk

    above .s file is named vhdautoattach.s and is saved right beside the 4gb .vhd on my PCIe raid drive (z:)

    my .cmd file is as follows;

    @echo off
    diskpart -s “Z:\vhdautoattach.s”

    file is named “DiskpartAutoATTACHvhd.cmd” and is also saved along side my .vhd and .s files in (Z:) where the shortcut points to.

    , but I have to click OK on the UAC pop-up just after logon. I have the hidden admin Account unlocked and I even switched over to that account and tried from there to set them to run as admin, but to no avail.

    I hope its not as simple as lowering my UAC notification settings! Best regards and thank you all for your time and insight.

  3. r4k4 said on November 23, 2011 at 3:46 am

    Thank you very much, your post already being a part of my break through.. :)

    I created my own “DiskPartConf.s” like this:

    select vdisk file=D:VHD_Collectiondelicious.vhd
    attach vdisk

    I created my own “DiskPartConf.cmd” like this:

    @echo off
    diskpart.exe -s “D:5_KreasiDiskPartConf.s”

    I put shortcut into my own “StartUp Folder” and set it to be “Run as administrator”

    Any comments for better improvement are welcome.. :)

  4. Chan said on October 25, 2011 at 5:06 pm

    Thanks so much, It is really useful to me

  5. AbuAljazer said on October 11, 2011 at 5:34 pm

    Diskpart.cmd is a bad name, using Diskpart.cmd will make it run in an infinite loop!!!

    Use AttachVHD.cmd instead.

  6. Mark said on September 1, 2011 at 12:34 pm

    Thank you! Though you need quotes around the select vdisk statement:
    select vdisk file=”C:\Virtual Drives\dirvex.vhd”

    1. Martin Brinkmann said on September 1, 2011 at 2:12 pm

      good addition. But you only need quotes if the path has a space somewhere, I guess.

  7. Chad Krause said on August 17, 2011 at 4:58 pm

    Whenever I do it, an error occurs.

    DiskPart has encountered an error: The file or directory is corrupted and unable. See the System Event Log for more information. There is no virtual disk selected.

  8. Kendall said on January 21, 2011 at 4:29 am

    I made a program that will attach VHD automatically on startup.

Leave a Reply

Check the box to consent to your data being stored in line with the guidelines set out in our privacy policy

We love comments and welcome thoughtful and civilized discussion. Rudeness and personal attacks will not be tolerated. Please stay on-topic.
Please note that your comment may not appear immediately after you post it.