Skip to main content

How to Fix the Master Boot Record (MBR) in Batch Script

The Master Boot Record (MBR) is the first 512-byte sector of a hard disk that contains the partition table and the boot code that starts the operating system loading process. When the MBR becomes corrupted, due to malware, dual-boot installation, disk errors, or accidental overwrite, the computer fails to start, displaying errors like "Error loading operating system," "Invalid partition table," or "Missing operating system." Repairing the MBR is a fundamental recovery technique performed from the Windows Recovery Environment.

This guide explains how to diagnose MBR issues, repair the boot code, and restore system bootability.

Understanding MBR Structure

MBR (First 512 bytes of the physical disk):
┌─────────────────────────────────────────────────┐
│ Boot Code (446 bytes) │ ← bootrec /fixmbr repairs THIS
│ (Executable code that finds the active │
│ partition and loads its boot sector) │
├─────────────────────────────────────────────────┤
│ Partition Table (64 bytes) │ ← NOT modified by /fixmbr
│ (4 entries, 16 bytes each - defines where │
│ each partition starts and ends) │
├─────────────────────────────────────────────────┤
│ Boot Signature (2 bytes: 0x55AA) │ ← Marks the sector as bootable
└─────────────────────────────────────────────────┘

Critical distinction: bootrec /fixmbr replaces only the boot CODE (the first 446 bytes). It does NOT touch the partition table (the next 64 bytes). This makes it safe: your partition layout and data remain intact.

Recovery Environment Only

MBR repair commands must be run from the Windows Recovery Environment (WinRE), not from a running Windows session. Boot from Windows installation media → Repair your computer → Troubleshoot → Command Prompt.

You cannot fix the MBR of the disk you are currently booted from: the disk is locked by the running operating system.

MBR vs. GPT - know your system:

FeatureMBRGPT
FirmwareBIOS (Legacy)UEFI
Boot mechanismMBR boot code → active partition → bootmgrUEFI firmware → EFI System Partition → bootmgfw.efi
bootrec /fixmbr applicable?YesNo - use bcdboot instead
Maximum disk size2 TB18 EB
Partition limit4 primary128
Verify Your Disk Type First

Running bootrec /fixmbr on a UEFI/GPT system has no effect (or produces an error). Before attempting MBR repair, confirm the disk uses MBR partitioning:

diskpart
list disk
:: If the disk has an asterisk (*) in the Gpt column, it's GPT - use bcdboot instead.

Common MBR Failure Symptoms

Error MessageLikely Cause
"Error loading operating system"MBR boot code is corrupt
"Invalid partition table"MBR partition table entries are damaged
"Missing operating system"No active partition, or boot sector is invalid
"BOOTMGR is missing"Boot sector or BCD issue (not MBR code itself)
Black screen, no errorMBR boot code zeroed out or overwritten
GRUB/Linux prompt instead of WindowsAnother OS overwrote the MBR boot code

What causes MBR corruption:

  • Malware (bootkits): Virus specifically targets the MBR to load before Windows, evading antivirus
  • Dual-boot installation: Installing Linux (GRUB) or an older Windows overwrites the MBR boot code
  • Disk imaging tools: Incorrect disk cloning can corrupt or overwrite the MBR
  • Power failure during write: If the system crashed while updating the MBR (rare)
  • Bad sectors: Physical media damage at the start of the disk (sector 0)

Method 1: Standard MBR Repair (Recovery Environment)

This is the standard recovery procedure for BIOS/MBR systems. It repairs the MBR boot code, optionally fixes the boot sector, and verifies the active partition.

Run from Recovery Command Prompt

Boot from Windows installation media → Repair your computer → Troubleshoot → Command Prompt. These commands will not work from a normal Windows session.

Implementation

@echo off
setlocal

echo ============================================================
echo Master Boot Record (MBR^) Repair Utility
echo ============================================================
echo.
echo [INFO] This script must be run from the Recovery Environment.
echo (Boot from Windows USB ^> Repair ^> Command Prompt^)
echo.

:: =============================================
:: Step 1: Verify we're in Recovery Environment
:: =============================================
where bootrec.exe >nul 2>&1
if errorlevel 1 (
echo [ERROR] bootrec.exe not found. >&2
echo This utility is only available in the Recovery Environment. >&2
echo Boot from Windows installation media to access it. >&2
endlocal
exit /b 1
)

:: =============================================
:: Step 2: Identify the disk type (MBR vs. GPT)
:: =============================================
echo [1/5] Checking disk configuration...
echo.

:: Use DiskPart to identify the disk type
set "DPScript=%TEMP%\dp_check_%RANDOM%.txt"
(echo list disk) > "%DPScript%"
echo [INFO] Disk listing:
diskpart /s "%DPScript%"
del "%DPScript%" 2>nul

echo.
echo [INFO] If Disk 0 has an asterisk (*^) in the Gpt column, this disk
echo uses GPT partitioning. Use bcdboot instead of bootrec.
echo This script is for MBR (non-GPT^) disks only.
echo.

set /p "Proceed=Is Disk 0 an MBR disk (no * in Gpt column)? (YES/no): "
if /i not "%Proceed%"=="YES" (
echo [INFO] For GPT disks, use:
echo bcdboot C:\Windows /s S: /f UEFI
echo (where S: is the EFI System Partition^)
endlocal
exit /b 0
)

:: =============================================
:: Step 3: Find the Windows drive
:: =============================================
echo.
echo [2/5] Locating Windows installation...

set "WinDrive="
for %%d in (C D E F G) do (
if exist "%%d:\Windows\System32\config\SYSTEM" (
set "WinDrive=%%d:"
echo [FOUND] Windows installation on %%d:\
)
)

if not defined WinDrive (
echo [ERROR] No Windows installation found. >&2
echo The hard drive may not be detected or Windows may be severely corrupted. >&2
endlocal
exit /b 1
)

:: =============================================
:: Step 4: Fix the Master Boot Record
:: =============================================
echo.
echo [3/5] Writing new Master Boot Record...

bootrec /fixmbr

if not errorlevel 1 (
echo [OK] MBR boot code repaired.
) else (
echo [ERROR] Failed to write MBR. >&2
echo The disk may be write-protected or have physical damage at sector 0. >&2
)

:: =============================================
:: Step 5: Fix the boot sector
:: =============================================
echo.
echo [4/5] Writing new boot sector...

bootrec /fixboot 2>nul

if not errorlevel 1 (
echo [OK] Boot sector repaired.
) else (
echo [WARNING] bootrec /fixboot failed. >&2
echo This is a known issue in Windows 10/11 Recovery. >&2
echo Trying alternative method (bootsect^)... >&2

:: Alternative: use bootsect utility
bootsect /nt60 sys /force /mbr >nul 2>&1
if not errorlevel 1 (
echo [OK] Boot sector repaired via bootsect.
) else (
echo [WARNING] Boot sector repair failed with both methods. >&2
echo The system may still boot if only the MBR was the issue. >&2
)
)

:: =============================================
:: Step 6: Verify active partition
:: =============================================
echo.
echo [5/5] Checking active partition...

set "DPScript=%TEMP%\dp_active_%RANDOM%.txt"
(
echo select disk 0
echo list partition
) > "%DPScript%"

echo [INFO] Partition layout:
diskpart /s "%DPScript%"
del "%DPScript%" 2>nul

echo.
echo [INFO] One partition should be marked as Active (with an asterisk^).
echo If no partition is active, the BIOS won't know which to boot from.
echo Use DiskPart to set the correct partition as active if needed.

echo.
echo ============================================================
echo MBR repair complete.
echo Remove installation media and restart to test.
echo ============================================================

endlocal
exit /b 0

Why /fixboot has a fallback to bootsect:

bootrec /fixboot has a known bug in Windows 10/11 where it returns "Access is denied" - even in the Recovery Environment with full access to the disk. The bootsect /nt60 sys /force /mbr command achieves the same result through a different code path and doesn't have this access control issue.

Why the active partition check:

Even with a perfect MBR, the system won't boot if no partition is marked as "Active." The BIOS reads the MBR boot code, which then scans the partition table for the active partition and jumps to its boot sector. If no partition is active, the MBR code has nowhere to go - producing "Missing operating system" or a blank screen.

Method 2: Complete Boot Chain Repair

When you don't know which component is broken, this method repairs the entire boot chain: MBR → boot sector → BCD → active partition.

@echo off
setlocal

echo ============================================================
echo Complete Boot Chain Repair
echo ============================================================
echo.
echo [INFO] This repairs: MBR → Boot Sector → BCD → Active Flag
echo [INFO] Run from Recovery Environment only.
echo.

where bootrec.exe >nul 2>&1
if errorlevel 1 (
echo [ERROR] Not in Recovery Environment. >&2
endlocal
exit /b 1
)

:: Find Windows drive
set "WinDrive="
for %%d in (C D E F G) do (
if exist "%%d:\Windows\System32" set "WinDrive=%%d:"
)

if not defined WinDrive (
echo [ERROR] Windows not found on any drive. >&2
endlocal
exit /b 1
)

echo [INFO] Windows found on %WinDrive%
echo.

:: Step 1: Fix MBR
echo [1/4] Repairing Master Boot Record...
bootrec /fixmbr
echo Done.

:: Step 2: Fix boot sector
echo [2/4] Repairing boot sector...
bootrec /fixboot 2>nul
if errorlevel 1 (
bootsect /nt60 sys /force /mbr >nul 2>&1
)
echo Done.

:: Step 3: Rebuild BCD
echo [3/4] Rebuilding Boot Configuration Data...
echo [INFO] Type Y if prompted to add Windows installations.

:: Back up existing BCD
if exist "%WinDrive%\Boot\BCD" (
attrib -h -r -s "%WinDrive%\Boot\BCD" >nul 2>&1
ren "%WinDrive%\Boot\BCD" BCD.bak >nul 2>&1
)

bootrec /rebuildbcd
echo Done.

:: Step 4: Verify active partition
echo [4/4] Verifying active partition...

set "DPScript=%TEMP%\dp_active_%RANDOM%.txt"
(
echo select disk 0
echo list partition
) > "%DPScript%"

diskpart /s "%DPScript%"
del "%DPScript%" 2>nul

echo.
echo [INFO] Verify that a partition is marked Active (*^) above.
echo If not, use DiskPart:
echo select disk 0
echo select partition 1
echo active
echo.
echo ============================================================
echo Boot chain repair complete.
echo Remove media and restart.
echo ============================================================

endlocal
exit /b 0

The repair sequence matters:

MBR → Boot Sector → BCD → Active Partition

1. MBR (fixmbr): BIOS reads this first to find the active partition
2. Boot Sector (fixboot): The active partition's boot sector loads BOOTMGR
3. BCD (rebuildbcd): BOOTMGR reads the BCD to find Windows
4. Active Flag: Without it, MBR code has nowhere to jump to

Each layer depends on the one before it. Repairing them in order ensures each component is correct before the next one is tested.

Method 3: Malware MBR Recovery

If the MBR was corrupted by malware (a "bootkit"), simply fixing the MBR may not be enough - the malware may re-infect the MBR on the next boot. This method includes an offline antivirus scan step.

@echo off
setlocal

echo ============================================================
echo Malware MBR Recovery
echo ============================================================
echo.
echo [INFO] Use this when MBR corruption was caused by malware.
echo [WARNING] Fix the MBR AFTER removing the malware, not before.
echo.

:: Step 1: Remind about malware removal
echo [STEP 1] Before fixing the MBR, ensure malware has been removed:
echo.
echo Option A: Boot from an antivirus rescue disk (Kaspersky, Avira, etc.^)
echo and run a full scan of all drives.
echo.
echo Option B: Remove the hard drive, connect it to a clean computer
echo as a secondary drive, and scan with antivirus.
echo.
echo Option C: Use Windows Defender Offline scan:
echo (Settings ^> Windows Security ^> Virus protection ^>
echo Scan options ^> Microsoft Defender Offline scan^)
echo.

set /p "MalwareRemoved=Has the malware been removed? (YES/no): "
if /i not "%MalwareRemoved%"=="YES" (
echo [INFO] Remove the malware first, then re-run this script.
endlocal
exit /b 0
)

echo.

:: Step 2: Fix MBR
echo [STEP 2] Repairing Master Boot Record...
bootrec /fixmbr
echo [OK] MBR repaired.

:: Step 3: Fix boot sector
echo [STEP 3] Repairing boot sector...
bootrec /fixboot 2>nul
if errorlevel 1 bootsect /nt60 sys /force /mbr >nul 2>&1
echo [OK] Boot sector repaired.

:: Step 4: Rebuild BCD (malware may have modified boot entries)
echo [STEP 4] Rebuilding BCD...
bootrec /rebuildbcd
echo [OK] BCD rebuilt.

echo.
echo ============================================================
echo MBR recovery complete.
echo.
echo IMPORTANT: After booting, run a FULL antivirus scan from
echo within Windows to ensure no residual malware components
echo remain in the operating system.
echo ============================================================

endlocal
exit /b 0
Malware Removal Order

If a bootkit infected the MBR, fixing the MBR while the malware is still present on the disk is futile. The malware will immediately re-infect the MBR on the next boot. Remove the malware FIRST (using an offline antivirus scan), THEN fix the MBR. This ensures the boot code stays clean.

How to Avoid Common Errors

Wrong Way: Running MBR Repair on a GPT Disk

:: HAS NO EFFECT: GPT disks don't use the MBR boot code
bootrec /fixmbr
:: On a UEFI/GPT system, this either does nothing or returns an error

GPT disks don't use the MBR boot mechanism. UEFI firmware reads the EFI System Partition directly. Use bcdboot for UEFI/GPT boot repair.

Correct Way: Check disk type first (Method 1's DiskPart step), then use the appropriate tool: bootrec /fixmbr for MBR, bcdboot for GPT.

Wrong Way: Running from a Normal Windows Session

:: FAILS: the boot disk is locked by the running OS
bootrec /fixmbr
:: Result: "bootrec is not recognized" or "Access is denied"

Correct Way: Boot from Windows installation USB → Repair your computer → Command Prompt. The Recovery Environment has direct access to the boot disk.

Problem: "BOOTMGR is missing" After MBR Fix

Fixing the MBR repairs the disk's first sector, but if the boot manager file (bootmgr) is missing from the active partition, the system still won't boot.

Solution: After fixing the MBR, rebuild the BCD:

bootrec /rebuildbcd

Or copy boot files with bcdboot:

bcdboot C:\Windows /s C: /f BIOS

Problem: No Partition Marked Active

The MBR boot code scans the partition table for the active partition. If no partition is active, the code has nowhere to jump, producing "Missing operating system."

Solution: Use DiskPart to set the correct partition as active:

diskpart
select disk 0
select partition 1
active
exit

Problem: MBR Corruption Recurs

If the MBR corrupts again after repair:

  • Malware: A bootkit is still active. Scan with offline antivirus before repairing.
  • Dual-boot: Another OS (Linux GRUB) is overwriting the MBR on update. Configure the other OS's bootloader to install to its own partition, not the MBR.
  • Disk damage: Bad sectors at sector 0 corrupt the MBR every time it's written. Check disk health with S.M.A.R.T. tools.

Best Practices and Rules

1. Verify MBR vs. GPT Before Repairing

bootrec /fixmbr only applies to MBR disks. Use DiskPart's list disk to check for the GPT asterisk before proceeding.

2. Repair the Entire Boot Chain

MBR repair alone may not be sufficient. Method 2's complete chain repair (MBR → boot sector → BCD → active flag) covers all components.

3. Remove Malware Before Repairing

If malware caused the corruption, remove it FIRST with an offline antivirus scan. Otherwise, the malware re-infects the MBR immediately.

4. Check for Active Partition

After MBR repair, verify that a partition is marked active. Without the active flag, the MBR boot code has no target partition.

5. Have bootsect Ready as Fallback

bootrec /fixboot has known access-denied issues in Windows 10/11. The bootsect /nt60 sys /force /mbr command is the reliable alternative.

6. Always Boot from Recovery Media

MBR repair must be done from the Recovery Environment, not from a running Windows session. The boot disk is locked while Windows is running from it.

Conclusion

Fixing the Master Boot Record is the foundational recovery step for BIOS/MBR systems that refuse to boot. By verifying the disk type (MBR vs. GPT), identifying the Windows drive in the Recovery Environment, repairing the MBR boot code without disturbing the partition table, and addressing the known fixboot access-denied issue with the bootsect fallback, you restore the first link in the boot chain. For complete recovery, repair the entire chain: MBR → boot sector → BCD → active partition. For malware-related corruption, always remove the infection before repairing the boot code.