VHD-To-BCD: Add Virtual-Hard-Disks to Bootmanager

DESCRIPTION:

VHD-To-BCD adds a virtual-hard-disk to Windows' BootManager using BCDEdit (which is part of Windows).

IMPORTANT:

The script will not change your default bootdevice or anything else. It just automates the things you need to do by hand to add a VHD to BootManager:

bcdedit /copy {default} /d "<bootdescription>" bcdedit /set {GUID} device vhd=[<driveletter>:]\<path\filenname.vhd> bcdedit /set {GUID} osdevice vhd=[<driveletter>:]\<path\filenname.vhd> bcdedit /set {GUID} detecthal on
The "tricky" thing to automate these 4 lines is to get the default GUID and the brackets around the driveletter ("[C:]")! This is what the script does.

I tested it on several Windows 8.1 and 10 installations without problems. In the worst case (e.g. you run the command not using a .vhd-file) you'll get a new boot-entry, which simply doesn't work, but it will not affect your default bootdevice and you can easily remove the entry (see "Remove a VHD from BootManager").

If you're using Windows 7 or Bitlocker, please be careful, as I did not test it!

Nethertheless I need to say that you use following script on your own risk and that I'm not responsible for any damage to your OS.

INSTALLATION:

Copy the code at the bottom in your language to clipboard, create a new filetype for ".vhd" and ".vhdx" and add it to the contextmenu by creating a new entry. Set it to "MS-DOS-Batch-function".

When done easily rightclick a vhd(x)-file and choose "Add VHD to bootmanager" (or whatever you named it). Finished!

CHOOSE CORRECT LANGUAGE

As said we need the GUID, which we'll get after copying default boot-entry. It'll be copied to a tempfile and determined by using tokens ("word-counting"). But due to different output-text depending on the used language, the tokens-value in the script differs. Examples:

EN: "The entry was successfully copied to {GUID}.", so the GUID is the 7th word (tokens).
DE: "Der Eintrag wurde erfolgreich in {GUID} kopiert.", so the GUID is the 6th word (tokens).

That's why you need to choose the correct language (or change the tokens-value) to get the GUID.

If you should use the wrong language, you'll get only a non-working boot-entry, but it will not affect or destroy your bootdevices. If that should happen, just remove the entry (see "Delete a VHD from Bootmanager).

If somebody can help me getting the GUID out of a text without using tokens (e.g. find and copy everything between "{...}"), we wouldn't need different scripts for each language and it would run on all languages!

BOOT FROM A VIRTUAL-HARD-DISK:

Go to startmenu, hold "shift"-key and press "restart". Once in Bootmanager go to "Use another operating system" and choose the VHD.

If you want to enter the Bootmanager from DirOpus, just add following code to a button:

@runmode:hide %windir%\system32\shutdown.exe /r /o /t 0

BACKUP BCD:

Creating a backup of the current BCD (run command prompt with admin rights):

bcdedit /export C:\BCD_Backup

REMOVE A VHD FROM BOOTMANAGER:

To remove a VHD from BootManager simply use msconfig or free EasyBCD and... delete it!

VHD-To-BCD

English Version:

[code]@admin
@runmode hide
@nofilenamequoting
@externalonly

set VHDPATH={filepath$}
set BOOTENTRY="{dlgstringS|Add virtual harddisk to bootmanager.\n\nPlease enter description or press cancel to abort:|Virtual-Hard-Disk}"

cmd /c " bcdedit /copy {default} /d %BOOTENTRY% " > %Temp%\VHD-To-BCD_GUID.txt

Rem ---------------------------------------------------
Rem ENTER YOUR TOKENS-VALUE HERE (English=7; German=6):
Rem ---------------------------------------------------
for /F "tokens=7 delims=. " %%A in (%Temp%\VHD-To-BCD_GUID.txt) DO set GUID=%%A

set STRIPPEDDRIVE=%VHDPATH:~1,2%
set STRIPPEDPATH=%VHDPATH:~3%
del %Temp%\VHD-To-BCD_GUID.txt

bcdedit /set %GUID% device vhd="[%STRIPPEDDRIVE%]%STRIPPEDPATH%"
bcdedit /set %GUID% osdevice vhd="[%STRIPPEDDRIVE%]%STRIPPEDPATH%"
bcdedit /set %GUID% detecthal on
[/code]

German Version:

[code]@admin
@runmode hide
@nofilenamequoting
@externalonly

set VHDPATH={filepath$}
set BOOTENTRY="{dlgstringS|Die virtuelle Harddisk wird dem Bootmanager hinzugefügt.\n\nBitte gewünschten Bootmenü-Eintrag eingeben:|Virtual-Hard-Disk}"

cmd /c " bcdedit /copy {default} /d %BOOTENTRY% " > %Temp%\VHD-To-BCD_GUID.txt

Rem ---------------------------------------------------
Rem ENTER YOUR TOKENS-VALUE HERE (English=7; German=6):
Rem ---------------------------------------------------
for /F "tokens=6 delims=. " %%A in (%Temp%\VHD-To-BCD_GUID.txt) DO set GUID=%%A

set STRIPPEDDRIVE=%VHDPATH:~1,2%
set STRIPPEDPATH=%VHDPATH:~3%
del %Temp%\VHD-To-BCD_GUID.txt

bcdedit /set %GUID% device vhd="[%STRIPPEDDRIVE%]%STRIPPEDPATH%"
bcdedit /set %GUID% osdevice vhd="[%STRIPPEDDRIVE%]%STRIPPEDPATH%"
bcdedit /set %GUID% detecthal on
[/code]

Be careful that your OS partition is not "bitlockered":
technet.microsoft.com/en-us/libr ... imitations

Lou

The script does nothing else than you would do "by hand" and "delete" should also be used carefully :slight_smile:. But I will add that to the topic and also how to backup BCD. Thanks.

Regarding your token/language issue, you could try to check wether your %%A for-loop-var starts with "{" and if it does, set the GUID. Could work for all languages if you raise the number of tokens to maybe 99. The for-construct hopefully does not get angry if there are less tokens available. o)

Worth a try. I also found some examples finding brackets and copying everything between them (that would be the best solution). For today I'm glad that it works perfect (I have some VHD's and always renamed them instead of adding each to bootmanager - that's over now :slight_smile:).

Hey Sasa :smiley:

Cool stuff here! Have you thought about switching it all to something like VBScript instead? Then you'd be able to use regex for finding the GUID, and you can still do all of the other stuff as well.

Might even be sweeter to have it possibly detect if the .vhd(x) is already in the bootmanager, and if so, offer another option to remove it again. :slight_smile: