How To Automatically Create BCDEdit Data For A Boot2VHD File On Your Windows 7 or Windows Server 2008 R2 Machine
Many people have told me they have lots of problems with BCDEdit. Others have told me they are afraid to do it for fear of making a mistake. To simplify the process I have created a batch file that takes a few parameters to “install” the VHD into BCDEdit for you. It is a batch file that requires only one parameter and that is the full path and file name of the .VHD file. It has two other optional parameters to set the Menu Description and to set this entry to be the default. See below for the syntax and examples.
This article assumes you already have a VHD. If you do not have a VHD see Native Boot To VHD Part I of IV – Overview which is a four part video series to learn how to create a boot to VHD if you do not have a VHD already.
If you make a mistake while “playing” with BCDEdit see Server 2008 R2 or Windows 7 System Will NOT Boot After Making Changes To Boot Manager Using BCDEdit.
If you have a bootable VHD all you need to do is download the attached .TXT file and save it as a .CMD file. I recommend you save it to the location where you put your BOOT2VHD.VHD file. Open an Administrative command box and launch the program with the proper parameters.
Start | type cmd | CTRL-SHIFT-ENTER | Accept UAC prompt
navigate to the place you put the downloaded command file and execute the command file with the proper parameters.
NOTE: You must run the command from an elevated command prompt.
Syntax: AutoBootVHD-BCD.cmd (VHD_FileName_AND_Path) [BootMenuDescription] [-d]
BootMenuDescription is optional and defaults to "Boot2VHD"
-d is optional and will make this entry the default boot option
Ex: AutoBootVHD-BCD D:\bootvhd\Boot2008R2.vhd My_Boot_To_VHD –d
If you have a path or description with spaces put quotes around it
Ex: AutoBootVHD-BCD "D:\My VHds\Win 2008 R2.vhd" "My Boot2Vhd" –d
Written by Dan Stolts ITProGuru no warranties, Use at your own risk
Example (not case sensitive):
- If your VHD file is named Win7UltSP1.VHD and you have it stored at c:\Boot2VHD\
- You want to set the boot menu description to “Windows 7 SP1 Boot2VHD” you would execute the following from an elevated command prompt…
c:
cd \boot2vhd
AutoBootVHD-BCD.cmd c:\boot2VHD\Win7UltSP1.VHD “Windows 7 SP1 Boot2VHD”
Download the Script Here… (provided in .cmd or .txt)
- https://mschnlnine.vo.llnwd.net/d1/inetpub/danstolts/resources/AutoBootVHD-BCD.txt
- https://mschnlnine.vo.llnwd.net/d1/inetpub/danstolts/resources/AutoBootVHD-BCD.cmd
- Zip File with both files... AutoBootVHD-BCD
In case you have a problem downloading… The full script is…
@echo off
Echo AutoBootVHD-BCD.cmd written by Dan Stolts https://itproguru.com
echo.
if "%1" == "" (
goto :Usage
)
if "%1" == "/?" (
goto :Usage
)
if "%1" == "-?" (
goto :Usage
)
set VHDFILENAME=%1
set VHDDESC="Boot2VHD"
if not {%2} == {} (
set VHDDESC=%2
)
@echo VHDFileName=%1
@echo Creating VHD boot entry for %VHDDESC%....
Rem we need to store the output of the copy so we can reuse the GUID
Rem We could do this without a temp file but it would be much harder to read
Rem Need to have write permissions in the %temp% folder (assumed)
cmd /c " bcdedit /copy {current} /d %VHDDESC% " > %temp%\tmpVHDGUID.txt
Rem Find the GUID in the output of the BCDEdit
FOR /F "tokens=7 delims=. " %%A in (%temp%\tmpVHDGUID.txt) DO set GUID=%%A
@echo VHD boot entry GUID = %GUID%
Rem We need to strip out the drive of the filename parameter
Rem ~pna in for loop = Path+Name+Extension
Rem
echo %1
Rem You must use the quotes in the for loop
for /F %%B in ("%VHDFileName%") do set VHDBCDFName=%%~pnxB
echo.
Rem cleanup temp file
del %temp%\tmpVHDGUID.txt
@echo Configuring VHD boot entry....
@echo.
@echo setting Device... vhd=[locate]%VHDBCDFName%
bcdedit /set %GUID% device vhd=[locate]%VHDBCDFName%
@echo setting OSDevice... vhd=[locate]%VHDBCDFName%
bcdedit /set %GUID% osdevice vhd=[locate]%VHDBCDFName%
@echo Setting detecthal on
bcdedit /set %GUID% detecthal on
set vhdDEFAULT=0
Rem If they did not pass a BootMenuDescription the -d will be %2
if `%2` == `-d` (
set vhdDEFAULT=1
@echo Default Enabled..
)
if `%3` == `-d` (
set vhdDEFAULT=1
Echo Default Enabled...
)
if %vhdDEFAULT% == 1 (
@echo Making VHD boot entry the default....
bcdedit /default %GUID%
)
@echo.
@echo VHD boot configuration complete.
@echo Please restart your machine to use the new option!
@echo.
@Echo run bcdedit to see the new entry
@echo.
goto :Exit
:Usage
@echo.
@echo NOTE: You must run the command from an elevated command prompt.
@echo.
@echo Syntax: AutoBootVHD-BCD.cmd (VHD_FileName_AND_Path) [BootMenuDescription] [-d]
@echo. BootMenuDescription is optional and defaults to "Boot2VHD"
@echo -d is optional and will make this entry the default boot option
@echo Ex: AutoBootVHD-BCD D:\bootvhd\Boot2008R2.vhd My_Boot_To_VHD -d
@echo .
@echo If you have a path or description with spaces put quotes around it
@echo Ex: AutoBootVHD-BCD "D:\My VHds\Win 2008 R2.vhd" "My Boot2Vhd" -d
@echo .
@echo Written by Dan Stolts ITProGuru no warranties, Use at your own risk
@echo see blog post at ITProguru.com
@echo.
@echo NOTE: You must run the command from an elevated command prompt.
:Exit
Rem the following commands were used for debugging only.
rem bcdedit
Rem pause
Rem bcdedit /delete %GUID%
@Echo.
@Echo written by Dan Stolts (ITProGuru) provide feedback to https://ITProGuru.com
@Echo.
@Echo.
Rem - 2012/03/01 - Added quotes in FOR Loop to eliminate issues with folder structure and names bigger than 8.3
Comments
Anonymous
January 01, 2003
Nice example of batch, handy script for bcdedit. Thanks jbAnonymous
January 01, 2003
Think I will try running this from a right click shell ext on .vhd file types.Anonymous
April 12, 2012
Brilliant. Thanks very much for taking the time to do this. Downloading now!Anonymous
December 31, 2012
The comment has been removedAnonymous
June 06, 2013
Hi, i've tried your script but it fails for me. It seems to be a problem with the [locate] variable which is not resolved to [C:]. Any suggestions?Anonymous
February 16, 2016
A million thanks! I'm getting into diffdisk deployment and want a contextmenu on vhd (parents) to "make child"
Still got some coding to do, but this is amazingly helpful. Thanks buddy!!