How to Rebuild the Boot Configuration Data (BCD) in Batch Script
The Boot Configuration Data (BCD) is the database that tells Windows how to start. If the BCD becomes corrupted or missing, due to disk failure, improper shutdown, failed OS clone, interrupted Windows Update, or dual-boot configuration changes, you'll see errors like "The Boot Configuration Data for your PC is missing or contains errors" or "An operating system wasn't found." Rebuilding the BCD restores the boot database, allowing Windows to start again.
This guide explains how to rebuild the BCD using bootrec, bcdboot, and bcdedit from a recovery environment.
Understanding BCD Location by Firmware Type
BIOS/MBR System: UEFI/GPT System:
┌────────────────────┐ ┌──────────────────────┐
│ Active Partition │ │ EFI System Partition │
│ (usually C:\) │ │ (hidden FAT32, │
│ │ │ usually 100-500 MB) │
│ \Boot\BCD │ │ \EFI\Microsoft\ │
│ \bootmgr │ │ Boot\BCD │
└────────────────────┘ │ \EFI\Boot\ │
│ bootx64.efi │
└──────────────────────┘
| Firmware | Partition Style | BCD Location | Boot Files |
|---|---|---|---|
| BIOS (Legacy) | MBR | \Boot\BCD on the active partition | bootmgr |
| UEFI | GPT | \EFI\Microsoft\Boot\BCD on the EFI System Partition | bootmgfw.efi |
Modifying the BCD can prevent your computer from booting if done incorrectly. This script should be:
- Run from the Windows Recovery Environment (WinRE) or a bootable USB, NOT from a running Windows session.
- Tested in a non-production environment first.
- Used only after other boot repair methods (Startup Repair) have failed.
Always back up the existing BCD before rebuilding.
The Recovery Tool Reference
| Command | What It Does | When to Use |
|---|---|---|
bootrec /fixmbr | Writes a new Master Boot Record | MBR corrupted (BIOS/MBR systems only) |
bootrec /fixboot | Writes a new boot sector | Boot sector damaged |
bootrec /scanos | Scans all disks for Windows installations | Identify which installations exist |
bootrec /rebuildbcd | Rebuilds the BCD store from found installations | BCD is corrupted or missing |
bcdboot C:\Windows | Copies boot files from Windows directory to boot partition | Modern replacement for bootrec on UEFI |
bcdedit /export | Exports (backs up) the current BCD | Before any modification |
Method 1: Standard BCD Rebuild (BIOS/MBR Systems)
This is the traditional recovery procedure for Legacy BIOS systems with MBR partition tables.
These commands must be run from the Windows Recovery Environment command prompt, not from a normal Windows session. Boot from Windows installation media → Repair your computer → Command Prompt.
Implementation
@echo off
setlocal
echo ============================================================
echo BCD Rebuild Utility (BIOS/MBR)
echo ============================================================
echo.
echo [INFO] This script should be run from the Recovery Environment.
echo (Windows Installation USB ^> Repair ^> Command Prompt)
echo.
:: =============================================
:: Step 1: Identify the Windows drive
:: =============================================
:: In WinRE, drive letters are often different from normal boot
:: C: in recovery may not be C: in normal Windows
echo [1/5] Identifying Windows installation...
echo.
set "WinDrive="
for %%d in (C D E F G) do (
if exist "%%d:\Windows\System32\config\SYSTEM" (
echo [FOUND] Windows installation on %%d:\
set "WinDrive=%%d:"
)
)
if not defined WinDrive (
echo [ERROR] No Windows installation found on any drive. >&2
echo Check that the hard drive is connected and recognized. >&2
endlocal
exit /b 1
)
echo.
echo Using Windows drive: %WinDrive%
echo.
:: =============================================
:: Step 2: Back up existing BCD (if it exists)
:: =============================================
echo [2/5] Backing up existing BCD...
if exist "%WinDrive%\Boot\BCD" (
bcdedit /export "%WinDrive%\BCD_Backup.bak" >nul 2>&1
if not errorlevel 1 (
echo [OK] BCD backed up to: %WinDrive%\BCD_Backup.bak
) else (
echo [INFO] Could not export BCD (may already be corrupt^).
:: Try manual backup
attrib -h -r -s "%WinDrive%\Boot\BCD" >nul 2>&1
copy "%WinDrive%\Boot\BCD" "%WinDrive%\BCD_Backup.bak" >nul 2>&1
)
) else (
echo [INFO] No existing BCD found - it may be missing.
)
echo.
:: =============================================
:: Step 3: Scan for Windows installations
:: =============================================
echo [3/5] Scanning for Windows installations...
echo.
bootrec /scanos
echo.
:: =============================================
:: Step 4: Rebuild the BCD
:: =============================================
echo [4/5] Rebuilding BCD store...
echo [INFO] If prompted, type 'Y' to add each Windows installation.
echo.
bootrec /rebuildbcd
set "RebuildResult=%errorlevel%"
:: If rebuild failed, try manual method
if %RebuildResult% neq 0 (
echo.
echo [WARNING] Automatic rebuild failed. Attempting manual reset...
echo.
:: Remove attributes from existing BCD
attrib -h -r -s "%WinDrive%\Boot\BCD" >nul 2>&1
:: Rename the corrupt BCD
ren "%WinDrive%\Boot\BCD" BCD.corrupt >nul 2>&1
:: Try rebuild again with a clean slate
bootrec /rebuildbcd
if errorlevel 1 (
echo [ERROR] Manual rebuild also failed. >&2
echo Try Method 2 (bcdboot^) as an alternative. >&2
endlocal
exit /b 1
)
)
:: =============================================
:: Step 5: Fix MBR and boot sector
:: =============================================
echo.
echo [5/5] Repairing Master Boot Record and boot sector...
bootrec /fixmbr
if not errorlevel 1 (
echo [OK] MBR repaired.
) else (
echo [WARNING] MBR repair failed. >&2
)
bootrec /fixboot
if not errorlevel 1 (
echo [OK] Boot sector repaired.
) else (
echo [WARNING] Boot sector repair failed. >&2
echo If "Access is denied", try Method 2 (bcdboot^) instead. >&2
)
echo.
echo ============================================================
echo BCD rebuild complete.
echo Remove installation media and restart to test.
echo ============================================================
endlocal
exit /b 0
Why drive letters change in WinRE:
The Windows Recovery Environment doesn't use the same drive letter assignments as the normal Windows boot. The System Reserved partition may take C:, pushing the Windows partition to D: or E:. The script scans all common drive letters for \Windows\System32\config\SYSTEM (a file that exists only on the Windows installation drive) to find the correct one.
Why the manual BCD rename fallback:
bootrec /rebuildbcd sometimes fails because it finds an existing (corrupted) BCD that it cannot overwrite. Renaming the corrupt file to BCD.corrupt forces rebuildbcd to create a brand-new store from scratch.
Method 2: BCD Rebuild for UEFI/GPT Systems
UEFI systems store the BCD on the EFI System Partition (ESP), which has no drive letter by default. The bcdboot command is the modern approach, it copies boot files from the Windows directory to the ESP and creates a new BCD.
@echo off
setlocal
echo ============================================================
echo BCD Rebuild Utility (UEFI/GPT)
echo ============================================================
echo.
echo [INFO] This script must be run from the Recovery Environment.
echo.
:: =============================================
:: Step 1: Find the Windows drive
:: =============================================
echo [1/4] Locating Windows installation...
set "WinDrive="
for %%d in (C D E F G) do (
if exist "%%d:\Windows\System32" (
set "WinDrive=%%d:"
echo [FOUND] Windows on %%d:\
)
)
if not defined WinDrive (
echo [ERROR] Windows installation not found. >&2
endlocal
exit /b 1
)
:: =============================================
:: Step 2: Assign a drive letter to the EFI partition
:: =============================================
echo.
echo [2/4] Assigning drive letter to EFI System Partition...
:: Create a DiskPart script to mount the ESP
set "DPScript=%TEMP%\dp_esp_%RANDOM%.txt"
(
echo list vol
) > "%DPScript%"
echo [INFO] Current volumes:
diskpart /s "%DPScript%"
del "%DPScript%" 2>nul
echo.
echo [INFO] The EFI partition is typically a small FAT32 volume (100-500 MB^).
echo.
set /p "ESPVolNum=Enter the EFI partition volume number: "
set "DPScript=%TEMP%\dp_mount_esp_%RANDOM%.txt"
(
echo select volume %ESPVolNum%
echo assign letter=S
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
del "%DPScript%" 2>nul
if not exist "S:\" (
echo [ERROR] Could not mount the EFI partition. >&2
endlocal
exit /b 1
)
echo [OK] EFI partition mounted as S:\
echo.
:: =============================================
:: Step 3: Back up existing BCD
:: =============================================
echo [3/4] Backing up existing BCD...
if exist "S:\EFI\Microsoft\Boot\BCD" (
attrib -h -r -s "S:\EFI\Microsoft\Boot\BCD" >nul 2>&1
copy "S:\EFI\Microsoft\Boot\BCD" "S:\BCD_Backup.bak" >nul 2>&1
echo [OK] BCD backed up to S:\BCD_Backup.bak
)
echo.
:: =============================================
:: Step 4: Rebuild boot files with bcdboot
:: =============================================
echo [4/4] Rebuilding UEFI boot files...
:: bcdboot copies boot files from the Windows directory to the ESP
:: /s S: = target the ESP mounted as S:
:: /f UEFI = create UEFI boot files (not BIOS)
bcdboot %WinDrive%\Windows /s S: /f UEFI
if not errorlevel 1 (
echo [OK] Boot files and BCD rebuilt successfully.
) else (
echo [ERROR] bcdboot failed. >&2
echo. >&2
echo Possible causes: >&2
echo - Windows installation at %WinDrive%\ is corrupted >&2
echo - EFI partition is too small or has no space >&2
echo - Wrong volume was selected as the EFI partition >&2
endlocal
exit /b 1
)
echo.
echo ============================================================
echo UEFI boot repair complete.
echo Remove installation media and restart to test.
echo ============================================================
:: Clean up: remove the ESP drive letter
set "DPScript=%TEMP%\dp_unmount_esp_%RANDOM%.txt"
(
echo select volume %ESPVolNum%
echo remove letter=S
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
del "%DPScript%" 2>nul
endlocal
exit /b 0
Why bcdboot instead of bootrec for UEFI:
bootrec /fixboot has a known bug in Windows 10/11 where it returns "Access is denied" on UEFI/GPT systems. bcdboot is the modern replacement that works reliably on both UEFI and BIOS systems. It:
- Copies the correct boot files (
.efifiles for UEFI,bootmgrfor BIOS) - Creates a fresh BCD with the correct entries
- Sets the firmware boot order
- Works from both WinRE and a running Windows session (with admin rights)
The /f parameter:
| Value | Creates Boot Files For | When to Use |
|---|---|---|
/f UEFI | UEFI firmware only | GPT disk with UEFI firmware |
/f BIOS | BIOS firmware only | MBR disk with Legacy BIOS |
/f ALL | Both UEFI and BIOS | Dual-firmware support or unknown configuration |
Method 3: Quick Boot Repair (Single Command)
When you know the system configuration and just need the fastest fix:
For BIOS/MBR:
:: Run from Recovery Command Prompt
bootrec /fixmbr
bootrec /fixboot
bootrec /rebuildbcd
For UEFI/GPT:
:: Run from Recovery Command Prompt
:: Replace C: with the actual Windows drive letter in WinRE
bcdboot C:\Windows /s S: /f UEFI
For unknown firmware type:
:: This works for both BIOS and UEFI
bcdboot C:\Windows /f ALL
Method 4: BCD Export and Verification
After rebuilding, verify the BCD contains the correct entries.
@echo off
echo [INFO] Current BCD entries:
echo --------------------------------------------------
echo.
bcdedit /enum
echo.
echo --------------------------------------------------
echo.
echo [INFO] Key things to verify:
echo 1. "Windows Boot Manager" entry exists
echo 2. "Windows Boot Loader" entry exists
echo 3. The "device" and "osdevice" paths point to the correct drive
echo 4. The "path" shows \Windows\system32\winload.exe (BIOS)
echo or \Windows\system32\winload.efi (UEFI)
echo.
echo [TIP] To export a backup of the working BCD:
echo bcdedit /export C:\BCD_Working_Backup.bak
What a healthy BCD contains:
Windows Boot Manager
--------------------
identifier {bootmgr}
device partition=\Device\HarddiskVolume1
path \EFI\Microsoft\Boot\bootmgfw.efi (UEFI)
or \bootmgr (BIOS)
Windows Boot Loader
--------------------
identifier {current}
device partition=C:
path \Windows\system32\winload.efi (UEFI)
or \Windows\system32\winload.exe (BIOS)
osdevice partition=C:
systemroot \Windows
If device or osdevice point to the wrong partition, or path references the wrong loader file, the system won't boot even with a rebuilt BCD.
How to Avoid Common Errors
Wrong Way: Running from Normal Windows Session
:: These commands don't work properly from a running Windows session
bootrec /fixmbr
bootrec /fixboot
:: Many of these fail or have no effect because the boot partition is locked
Correct Way: Boot from Windows installation media → Repair your computer → Command Prompt. The Recovery Environment has direct access to the boot partition.
Problem: Drive Letters Are Different in WinRE
In the Recovery Environment, C: is often NOT the Windows drive. The System Reserved or EFI partition may take C:, pushing Windows to D: or E:.
Solution: Method 1 scans all drive letters for \Windows\System32\config\SYSTEM to find the correct Windows drive. Always verify before running any commands that reference a specific drive letter.
Problem: bootrec /fixboot Returns "Access is Denied"
This is a known bug in Windows 10/11 on UEFI/GPT systems.
Solution: Use bcdboot instead (Method 2). It's the modern replacement that doesn't have this access control issue:
bcdboot C:\Windows /s S: /f UEFI
Problem: bootrec /rebuildbcd Reports "0 Windows Installations Found"
This means bootrec cannot locate the Windows directory. Common causes:
- Wrong drive letter (drive letters change in WinRE)
- The Windows installation is corrupted
- The hard drive is not being detected (driver issue)
Solution: Verify Windows exists on the detected drive:
dir D:\Windows\System32\winload.* 2>nul
If it exists, try specifying the drive explicitly with bcdboot D:\Windows.
Problem: BCD Corrupts Again After Rebuild
If the BCD corruption recurs:
- Disk health: Bad sectors on the boot partition corrupt the BCD file. Run
chkdsk /f /ron the boot partition. - Dual-boot conflicts: Other operating systems (Linux GRUB) may overwrite the BCD during their boot loader installation.
- Power failures: Unexpected shutdowns during Windows Update can corrupt the BCD.
Repeated BCD corruption is almost always a symptom of failing storage hardware. The BCD file is on the boot partition, if that area of the disk has bad sectors, the BCD will be re-corrupted every time Windows writes to it. Run chkdsk /f /r on the boot partition and check S.M.A.R.T. data for the drive before rebuilding the BCD again.
Best Practices and Rules
1. Always Back Up Before Rebuilding
Export the existing BCD with bcdedit /export before making changes. If the rebuild makes things worse, you can restore with bcdedit /import.
2. Identify Firmware Type First
Use Method 1 for BIOS/MBR systems and Method 2 for UEFI/GPT. Using the wrong method produces a BCD that references the wrong boot loader file.
3. Verify Drive Letters in WinRE
Drive letters change in the Recovery Environment. Always check which drive contains the Windows installation before running any commands.
4. Use bcdboot for UEFI Systems
bootrec /fixboot has known issues on UEFI/GPT systems. bcdboot is the reliable modern alternative.
5. Verify After Rebuilding
After the rebuild, run bcdedit /enum (Method 4) to confirm the BCD contains correct entries before rebooting. Specifically check that device, osdevice, and path reference the correct partition and loader file.
6. Check Disk Health for Recurring Corruption
If the BCD corrupts repeatedly, the boot partition likely has physical media errors. Address the hardware issue before rebuilding again.
Conclusion
Rebuilding the BCD is the definitive recovery operation for an unbootable Windows system where the boot configuration database is missing or corrupted. By identifying the correct firmware type (BIOS/MBR vs. UEFI/GPT), finding the Windows drive in the Recovery Environment, backing up the existing BCD, and using the appropriate rebuild tool (bootrec for BIOS, bcdboot for UEFI), you restore the boot chain systematically. Post-rebuild verification with bcdedit /enum confirms the repair before rebooting, preventing wasted recovery cycles from incorrect BCD entries.