Scripting with bcdedit

5.6k Views Asked by At

After rebuilding a HDD using ImageX and a WIM, the BCD sometimes gets corrupted. I therefor need to rebuid the BCD from a script running unattended in a command prompt.

The below code does the job, when entered manually. I need help to automate it (see further below code example):

bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
bcdedit.exe /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader
bcdedit.exe /set {GUID} device partition=C:
bcdedit.exe /set {GUID} osdevice partition=C:
bcdedit.exe /set {GUID} path \Windows\system32\winload.exe
bcdedit.exe /set {GUID} systemroot \Windows
bcdedit.exe /displayorder {GUID}

As started above, I need to run this in a unattended command prompt. The output from the 6th last statement "bcdedit.exe /create /d "Microsoft Windows" /application osloader" is a newly creates GUID. This ID is needed in the following commands.

How do I load this new GUID from bcdedit to a variable I can call in the following code?

Best Regards Henrik V. Nielsen

5

There are 5 best solutions below

0
On

There is an easier way for fixing BCD.

bcdboot c:\windows 

for example replaces all bcdedit commands from question.

See explanations for using bcdboot to fix BCD.

The utilities bcdboot and bootsect can fix all boot problems (regarding initial boot sequence).

sfc.exe can fix corrupted system files.

0
On

If other should face the same problem, I solved it by adding the following line.

For /F "tokens=2 delims={}" %%i in ('bcdedit.exe') do (set _NEWGUID=%%i)

This works because there is only one GUID in the file.

0
On

Dylan Grasha Your answer has some errors and I have added some enhancements to make it more complete.

@Echo Off
bootrec.exe /fixmbr
bootsect.exe /nt60 C: /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
attrib -h -s C:\boot\bcd.temp >nul
del C:\boot\bcd.temp >nul
bcdedit /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader>GUID.txt
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set _NEWGUID=%%i)
bcdedit.exe /set {%_NEWGUID%} device partition=C:
bcdedit.exe /set {%_NEWGUID%} osdevice partition=C:
bcdedit.exe /set {%_NEWGUID%} path \Windows\system32\winload.exe
bcdedit.exe /set {%_NEWGUID%} systemroot \Windows
bcdedit.exe /displayorder {%_NEWGUID%}
del guid.txt
cmd
0
On

This is a solution based on Henrik's code

This takes the GUID created from BCD into a text file and the for loop gets the GUID from the file

bootrec.exe /fixmbr
bootsect.exe /nt60 all /force
attrib -h -s C:\boot\BCD
del C:\boot\BCD
bcdedit.exe /createstore c:\boot\bcd.temp
bcdedit.exe /store c:\boot\bcd.temp /create {bootmgr} /d "Windows Boot Manager"
bcdedit.exe /import c:\boot\bcd.temp
bcdedit.exe /set {bootmgr} device partition=C:
bcdedit.exe /timeout 10
attrib -h -s C:\boot\bcd.temp
del c:\boot\bcd.temp
bcdedit.exe /create /d "Microsoft Windows" /application osloader>GUID.txt
For /F "tokens=2 delims={}" %%i in (GUID.txt) do (set _NEWGUID=%%i)
bcdedit.exe /set %_NEWGUID% device partition=C:
bcdedit.exe /set %_NEWGUID% osdevice partition=C:
bcdedit.exe /set %_NEWGUID% path \Windows\system32\winload.exe
bcdedit.exe /set %_NEWGUID% systemroot \Windows
bcdedit.exe /displayorder %_NEWGUID%
2
On

there is an easier way.

When creating a new entry BCD accepts all GUIDs in the Form aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee (number of digits 8-4-4-4-12)

This means you can define an GUID an don´t have to search the GUID with the For-Loop.

It´s working for me.