Windows 7 always attach VHD

I don't see a way to re-attach it at boot "natively". You could use a "startup script" with the DISKPART commands to re-attach it, or deploy a script in the "Startup" program group to re-attach it after logon. That's disappointing and hackish, but at least it would give you what you wanted.

@echo off
SET TEMPFILE="%TEMP%\%RANDOM%.TXT"
echo SELECT VDISK FILE=(vdisk filename here)>%TEMPFILE%
echo ATTACH VDISK>>%TEMPFILE%
DISKPART /s %TEMPFILE%
del %TEMPFILE%

It's sad, but it's the best I can see to do for now...


Another way is to attach on service level. You need instsrv.exe and srvany.exe from Windows Resource Kit Tools. Put them to any folder with the following files:

disk-W-install-permanently.cmd

@echo off
echo RUN THIS AS ADMINISTRATOR!
pause
cd %~dp0
copy srvany.* c:\windows\system32\
copy srvany-attach-disk-W c:\windows\system32\
instsrv disk_W_attach "c:\windows\system32\srvany.exe"
regedit disk-W-install-permanently.reg
pause

disk-W-install-permanently.reg

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\disk_W_attach\Parameters]
"AppEnvironment"=""
"Application"="c:\\windows\\system32\\cmd.exe"
"AppParameters"="/c srvany.bat"
"AppDirectory"="C:\\Windows\\System32"

srvany.bat

@echo on 
set log=%temp%\srvany.log

echo log: Running at %date% %time% >>%log%
diskpart /s srvany-attach-disk_W >>%log%
net stop disk_W_attach

srvany-attach-disk-W

select vdisk file=”D:\private\vm\HDD\disk_W.vhd"
attach vdisk
exit

dir /b

disk-W-install-permanently.cmd
disk-W-install-permanently.reg
instsrv.exe
srvany.bat
srvany.exe
srvany-attach-disk-W

Now edit script srvany-attach-disk_W to point to your VHD, execute disk-W-install-permanently.cmd with admin privileges and reboot. That's all!


You can try using VHD attach. It has option to attach VHD after each boot. Since one part runs as service, no UAC prompt will bother you.


VHD attach program worked for me. It attaches VHD for all the users. No need to create a start up script or scheduled task. The program attaches VHD itself. you just need to install the program. Attach your VHD file once or go to options -> Add the VHD file and select Auto Attach. Thanks Josip. You are awesome