Coding Blog

Jim Scott's coding blog

Automatically attaching VHD files in Windows 7 and Windows 2008

If you have played with the new VHD feature in windows 7 or windows 2008 then you know just how cool of a feature this is. However the problem is that when you reboot your machine you find that when it comes back up all your VHD files are no longer attached? Here is what I did to get around the issue.

  1. Create a batch file that will hold the following line:

    diskpart /s “c:\path to script\diskpartscript.txt”

    I named my batch file attachvhd.bat and placed it in the same folder as my VHD files


    attachvhd

  2. Create the script file that is being referenced by the attachvhd.bat batch file. Here is what the contents of that script needs to contain:

    select vdisk file="c:\path to vhd files\myvhddrive.vhd"
    attach vdisk

    I named my script file diskpartscript.txt and placed it in the same folder as my VHD files.

    script 

  3. Create a scheduled task that will automatically run when your machine starts up.

    • Go to Start / Administrative Tools / Task Scheduler
    • Click Create Basic Task

      1_addtask

    • Fill in the name of the task and the description and Click Next

      2_createbasictask 

    • Select “Start a program” radio button option and then Click Next

      3_start_program

    • Select “When the computer starts” and then Click Next

      3_when_computer_starts

    • Browse to the folder that you setup your batch file in and select it. Click Next

      5_script

    • Click Finish

      6_finish 


  4. You have now completed all the necessary steps. Restart your computer and you should find that your VHD files are now automatically attached. One caveat is that if you reboot and you log into your machine quick enough it is possible that the task may not have been run yet. Once you are logged in if the task runs you will get an Autoplay dialog as follows. Simply close it. This does not happen if the task runs before you get logged in.

    auto_play
Posted: Nov 26 2009, 10:05 by jscott | Comments (3) RSS comment feed |
  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Filed under: Windows 7

Comments

DotNetKicks.com said:

trackbackAutomatically attaching VHD files in Windows 7 and Windows 2008

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# November 26 2009, 12:44

Oscar United States said:

OscarThat's a clever trick. I just wish we didn't have to do these kinds of things and just keep the VHDs mounted at boot, like a network drive. But Thanks for posting this workaround.

# February 10 2010, 19:15

jscott United States said:

jscottAgreed, hopefully MS will enhange the VHD support so that one day we can define in disk manager that a VHD should persist after reboots.

# February 10 2010, 22:38

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading