Automatically Attach VHD Images
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:
- Create a new text document and place the two commands from above in it.
- You can add those two lines again for every vhd image that should be automatically mounted during system start.
- Save the file as vhdattach.s.
- 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.
- 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.
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…
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”
echo.
pause
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.
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”
echo.
pause
I put shortcut into my own “StartUp Folder” and set it to be “Run as administrator”
Any comments for better improvement are welcome.. :)
Thanks so much, It is really useful to me
Diskpart.cmd is a bad name, using Diskpart.cmd will make it run in an infinite loop!!!
Use AttachVHD.cmd instead.
Thank you! Though you need quotes around the select vdisk statement:
select vdisk file=”C:\Virtual Drives\dirvex.vhd”
good addition. But you only need quotes if the path has a space somewhere, I guess.
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.
I made a program that will attach VHD automatically on startup.