How to Mark a Partition as Active in Batch Script
On a legacy BIOS (non-UEFI) system, the firmware needs to know which partition on the hard drive contains the bootloader. The partition marked as Active is the one the BIOS will attempt to boot from. If no partition is marked active, or if the wrong one is, the computer will display a "No bootable device found" or "Missing operating system" error. A Batch script can use DiskPart to set the active flag on the correct partition, which is a common fix for broken boot configurations on MBR disks.
This guide will explain how to manage the active partition flag.
Important: MBR/BIOS Only
The active command applies only to MBR-partitioned disks on BIOS (Legacy) systems. On modern UEFI systems with GPT disks, the concept of "Active" does not exist, the UEFI firmware automatically locates the EFI System Partition (ESP) and boots from it.
Before using any method in this guide, verify your system:
| Your System | Partition Style | Active Flag Needed? |
|---|---|---|
| BIOS (Legacy) + MBR disk | MBR | Yes, BIOS needs it to find the bootloader |
| UEFI + GPT disk | GPT | No, UEFI finds the ESP automatically |
| UEFI + MBR disk (CSM mode) | MBR | Yes, but consider converting to GPT |
:: Quick check: is the boot disk MBR or GPT?
powershell -NoProfile -Command "(Get-Disk -Number 0).PartitionStyle"
If the result is GPT, you do not need the active command. If your system won't boot from a GPT disk, the issue is in the UEFI boot configuration, not the active flag.
Method 1: Identify and Set the Active Partition
This method first identifies the current partition layout and active status, then allows the user to set the active flag with proper verification.
Implementation
@echo off
setlocal EnableDelayedExpansion
:: Verify admin privileges
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] DiskPart requires administrator privileges. >&2
endlocal
exit /b 1
)
:: Verify the boot disk uses MBR
for /f "delims=" %%s in (
'powershell -NoProfile -Command "(Get-Disk -Number 0).PartitionStyle"'
) do set "BootDiskStyle=%%s"
if /i "!BootDiskStyle!"=="GPT" (
echo [INFO] Disk 0 uses GPT partition style.
echo [INFO] The "active" flag is not used on GPT disks.
echo [INFO] UEFI firmware automatically locates the EFI System Partition.
echo.
echo If the system won't boot, the issue is likely:
echo - Corrupted EFI boot files (fix with: bcdboot C:\Windows /s S:^)
echo - Incorrect UEFI boot order (check BIOS settings^)
endlocal
exit /b 0
)
:: =============================================
:: Step 1: Show current partition layout
:: =============================================
echo [INFO] Current partition layout on Disk 0 (MBR^):
echo.
set "DPList=%TEMP%\dp_listpart_%RANDOM%.txt"
(
echo select disk 0
echo list partition
) > "%DPList%"
diskpart /s "%DPList%" 2>nul
del "%DPList%" 2>nul
:: Show detailed info via PowerShell
echo.
powershell -NoProfile -Command ^
"$parts = Get-Partition -DiskNumber 0 -ErrorAction SilentlyContinue;" ^
"if ($parts) {" ^
" $parts | ForEach-Object {" ^
" $sizeMB = [math]::Round($_.Size / 1MB);" ^
" $sizeStr = if ($sizeMB -ge 1024) { '{0:N1} GB' -f ($_.Size / 1GB) } else { \"$sizeMB MB\" };" ^
" $letter = if ($_.DriveLetter) { \"$($_.DriveLetter):\" } else { '(none)' };" ^
" [PSCustomObject]@{" ^
" 'Part #' = $_.PartitionNumber;" ^
" 'Drive' = $letter;" ^
" 'Size' = $sizeStr;" ^
" 'Active' = $_.IsActive;" ^
" 'Boot' = $_.IsBoot;" ^
" 'Type' = $_.Type" ^
" }" ^
" } | Format-Table -AutoSize" ^
"}"
echo [INFO] In DiskPart output above, an asterisk (*^) in the "Active" column
echo indicates the current boot partition.
echo.
:: =============================================
:: Step 2: Get user selection
:: =============================================
set /p "PartNum=Enter partition number to mark as active (or Q to quit): "
if /i "!PartNum!"=="Q" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)
if "!PartNum!"=="" (
echo [ERROR] No partition number entered. >&2
endlocal
exit /b 1
)
:: =============================================
:: Step 3: Verify the partition exists and is reasonable
:: =============================================
for /f "tokens=1-3 delims=|" %%a in (
'powershell -NoProfile -Command ^
"$p = Get-Partition -DiskNumber 0 -PartitionNumber !PartNum! -ErrorAction SilentlyContinue;" ^
"if (-not $p) { Write-Output ''NOT_FOUND|0|''; exit 1 };" ^
"$sizeMB = [math]::Round($p.Size / 1MB);" ^
"Write-Output \"$($p.IsActive)|$sizeMB|$($p.Type)\""'
) do (
set "IsActive=%%a"
set "SizeMB=%%b"
set "PartType=%%c"
)
if "!IsActive!"=="NOT_FOUND" (
echo [ERROR] Partition !PartNum! does not exist on Disk 0. >&2
endlocal
exit /b 1
)
if /i "!IsActive!"=="True" (
echo [INFO] Partition !PartNum! is ALREADY the active partition. No change needed.
endlocal
exit /b 0
)
:: Warn if the selected partition seems wrong
:: A typical boot partition is 100-500 MB (System Reserved) or the main OS partition
powershell -NoProfile -Command "if (!SizeMB! -gt 1024 -and !SizeMB! -lt 30000) { exit 0 } elseif (!SizeMB! -le 500) { exit 0 } else { exit 1 }" >nul 2>&1
if errorlevel 1 (
echo.
echo [WARNING] Partition !PartNum! is !SizeMB! MB, which is unusually large
echo for a boot partition. Typical boot partitions are 100-500 MB
echo (System Reserved^) or the main OS partition.
echo.
echo Are you sure this partition contains the bootloader?
)
:: =============================================
:: Step 4: Confirm and apply
:: =============================================
echo.
echo [ACTION] This will mark Partition !PartNum! on Disk 0 as ACTIVE.
echo [ACTION] The BIOS will attempt to boot from this partition.
echo.
set /p "Confirm=Type BOOT to proceed: "
if /i not "!Confirm!"=="BOOT" (
echo [INFO] Cancelled. No changes were made.
endlocal
exit /b 0
)
set "DPScript=%TEMP%\dp_active_%RANDOM%.txt"
(
echo select disk 0
echo select partition !PartNum!
echo active
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"
del "%DPScript%" 2>nul
if %DPResult% neq 0 (
echo [ERROR] Failed to set active flag. >&2
echo The partition may be on a GPT disk (active flag is MBR-only^). >&2
endlocal
exit /b 1
)
echo [OK] Partition !PartNum! is now marked as ACTIVE (boot partition^).
:: Verify
for /f "delims=" %%v in (
'powershell -NoProfile -Command ^
"(Get-Partition -DiskNumber 0 -PartitionNumber !PartNum!).IsActive"'
) do set "VerifyActive=%%v"
if /i "!VerifyActive!"=="True" (
echo [OK] Verified: Active flag is set.
) else (
echo [WARNING] Active flag may not have been set correctly. >&2
)
:: Log the change
for /f "delims=" %%t in (
'powershell -NoProfile -Command "Get-Date -Format ''yyyy-MM-dd HH:mm:ss''"'
) do echo [%%t] Set Partition !PartNum! as ACTIVE on Disk 0 by %USERNAME% >> "%~dp0boot_changes.log"
endlocal
exit /b 0
What the active flag does:
When a BIOS system powers on, it reads the MBR (first 512 bytes of Disk 0) to find the partition table. It then looks for the partition marked as "Active" and loads the boot code from that partition's first sector (the Volume Boot Record). If no partition is active, or if the active partition doesn't contain valid boot code, the BIOS displays an error.
Which partition should be active:
On most Windows MBR installations:
- If a "System Reserved" partition exists (100–500 MB, usually Partition 1): This should be active. It contains the Windows Boot Manager.
- If no System Reserved partition exists: The main Windows partition (usually the largest partition on Disk 0) should be active. The boot files are stored directly on the Windows volume.
What happens when you set a new active partition:
Only one partition per MBR disk can be active. Setting a new active partition automatically removes the flag from the previous one. This is done atomically by DiskPart, there is no moment where zero or two partitions are active.
Method 2: Boot Repair - Restore the Active Flag
When a computer displays "No bootable device" or "Missing operating system" after a failed dual-boot setup, partition tool operation, or Windows Update, the active flag may have been lost or moved to the wrong partition. This method identifies the correct partition and restores the flag.
@echo off
setlocal EnableDelayedExpansion
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] Administrator privileges required. >&2
endlocal
exit /b 1
)
:: Check for GPT first
for /f "delims=" %%s in (
'powershell -NoProfile -Command "(Get-Disk -Number 0).PartitionStyle"'
) do set "Style=%%s"
if /i "!Style!"=="GPT" (
echo [INFO] Disk 0 is GPT. The active flag does not apply.
echo [INFO] For GPT boot repair, try:
echo bootrec /fixboot
echo bcdboot C:\Windows /s S:
endlocal
exit /b 0
)
echo [INFO] Attempting boot repair on MBR Disk 0...
echo.
:: Find the System Reserved partition (small NTFS partition, typically 100-500 MB)
set "BootPartNum="
powershell -NoProfile -Command ^
"$parts = Get-Partition -DiskNumber 0 -ErrorAction SilentlyContinue;" ^
"if (-not $parts) { exit 1 };" ^
"$sysReserved = $parts | Where-Object {" ^
" $_.Size -ge 50MB -and $_.Size -le 600MB -and" ^
" $_.Type -eq 'IFS'" ^
"} | Select-Object -First 1;" ^
"if ($sysReserved) {" ^
" Write-Output $sysReserved.PartitionNumber" ^
"} else {" ^
" $largest = $parts | Sort-Object Size -Descending | Select-Object -First 1;" ^
" Write-Output $largest.PartitionNumber" ^
"}"
for /f "delims=" %%n in (
'powershell -NoProfile -Command ^
"$parts = Get-Partition -DiskNumber 0;" ^
"$sr = $parts | Where-Object { $_.Size -ge 50MB -and $_.Size -le 600MB -and $_.Type -eq ''IFS'' } | Select-Object -First 1;" ^
"if ($sr) { $sr.PartitionNumber } else { ($parts | Sort-Object Size -Descending | Select-Object -First 1).PartitionNumber }"'
) do set "BootPartNum=%%n"
if "!BootPartNum!"=="" (
echo [ERROR] Could not identify the boot partition. >&2
echo Manual intervention required. >&2
endlocal
exit /b 1
)
:: Check if it's already active
for /f "delims=" %%a in (
'powershell -NoProfile -Command "(Get-Partition -DiskNumber 0 -PartitionNumber !BootPartNum!).IsActive"'
) do set "AlreadyActive=%%a"
if /i "!AlreadyActive!"=="True" (
echo [INFO] Partition !BootPartNum! is already active.
echo [INFO] The boot failure may have a different cause.
echo [INFO] Try running from a recovery environment:
echo bootrec /fixmbr
echo bootrec /fixboot
echo bootrec /rebuildbcd
endlocal
exit /b 0
)
for /f "delims=" %%s in (
'powershell -NoProfile -Command "[math]::Round((Get-Partition -DiskNumber 0 -PartitionNumber !BootPartNum!).Size / 1MB)"'
) do set "PartSizeMB=%%s"
echo [INFO] Identified boot partition: Partition !BootPartNum! (!PartSizeMB! MB^)
echo [ACTION] Setting Partition !BootPartNum! as ACTIVE...
set "DPScript=%TEMP%\dp_bootfix_%RANDOM%.txt"
(
echo select disk 0
echo select partition !BootPartNum!
echo active
) > "%DPScript%"
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"
del "%DPScript%" 2>nul
if %DPResult% neq 0 (
echo [ERROR] Failed to set active flag. >&2
endlocal
exit /b 1
)
echo [OK] Active flag restored on Partition !BootPartNum!.
echo [INFO] Try rebooting. If the problem persists, boot from recovery media and run:
echo bootrec /fixmbr
echo bootrec /fixboot
echo bootrec /rebuildbcd
for /f "delims=" %%t in (
'powershell -NoProfile -Command "Get-Date -Format ''yyyy-MM-dd HH:mm:ss''"'
) do echo [%%t] BOOT REPAIR: Set Partition !BootPartNum! active on Disk 0 by %USERNAME% >> "%~dp0boot_changes.log"
endlocal
exit /b 0
How the automatic detection works:
- The script looks for a small NTFS partition (50–600 MB) on Disk 0, which is typically the "System Reserved" partition containing the Windows Boot Manager.
- If no such partition exists (some installations put boot files on the main Windows partition), it falls back to the largest partition, which is likely the OS volume.
- If the identified partition is already active, the boot problem has a different cause (corrupted boot files, damaged MBR), and the script suggests
bootreccommands instead.
When this fix doesn't work:
If setting the active flag doesn't resolve the boot failure, the problem is likely:
- Corrupted Master Boot Record: Fix with
bootrec /fixmbrfrom a recovery environment. - Missing boot files: Fix with
bootrec /fixbootandbootrec /rebuildbcd. - Wrong BIOS boot order: Check BIOS settings to ensure the correct disk is first in the boot sequence.
- Hardware failure: The disk may have bad sectors in the boot area.
How to Avoid Common Errors
Wrong Way: Marking a Data Partition as Active
:: BREAKS BOOT: BIOS tries to boot from a data partition with no bootloader
select disk 0
select partition 3
active
If you mark a data partition (like D:) as active, the BIOS tries to load boot code from it, fails because there is no bootloader, and displays "Missing operating system."
Correct Way: Method 1 shows partition sizes and types so you can identify the correct boot partition. Method 2 auto-detects it.
Wrong Way: Using active on a GPT Disk
:: INVALID: the active flag concept does not exist on GPT disks
:: DiskPart will either reject the command or silently do nothing
select disk 0
select partition 1
active
Correct Way: Both methods in this guide check the disk's partition style first and inform the user that active is not applicable to GPT disks, with GPT-specific boot repair commands as alternatives.
Problem: Multiple Operating Systems on One MBR Disk
In a dual-boot setup, each OS may expect to be on the active partition. Only one can be active at a time.
Solution: Boot managers like GRUB or Windows Boot Manager handle this by chainloading from a single active partition. The partition containing the boot manager should be active, and the boot manager handles selecting which OS to load.
Problem: Active Flag Lost After Partition Operations
Disk management tools (both GUI and command-line) can sometimes reset or move the active flag when resizing, creating, or deleting partitions.
Solution: After any partition modification on an MBR boot disk, verify the active flag with list partition and restore it if necessary using Method 1 or 2.
Best Practices and Rules
1. Verify MBR Before Using active
The active command is only meaningful on MBR disks with BIOS boot. All methods in this guide check the partition style first and inform the user if the disk is GPT.
2. Identify the Boot Partition by Size
On MBR Windows installations, the boot partition is typically:
- 100–500 MB "System Reserved" (contains Windows Boot Manager)
- Or the main Windows partition (if System Reserved doesn't exist)
A partition that is many gigabytes with user data is almost certainly not the boot partition.
3. Only One Active Partition Per Disk
Setting a partition as active automatically removes the flag from the previously active partition. This is expected behavior, not an error.
4. Log Boot Configuration Changes
Changes to the active flag affect whether the system can boot. Log every change for troubleshooting:
echo [%date% %time%] Set Partition %PartNum% active on Disk 0 >> boot_changes.log
5. Have Recovery Media Ready
Before changing the active flag, ensure you have Windows installation or recovery media available. If the change causes a boot failure, you need to boot from external media to fix it.
6. Consider bootrec for Complete Boot Repair
Setting the active flag is only one part of boot repair. If the system still won't boot after setting the correct partition as active, the boot files themselves may be corrupted. Use these commands from a recovery environment:
bootrec /fixmbr - Writes a new Master Boot Record
bootrec /fixboot - Writes a new boot sector to the active partition
bootrec /rebuildbcd - Scans for Windows installations and rebuilds the boot configuration
Conclusions
Marking a partition as active via DiskPart is a targeted repair and configuration tool for legacy BIOS/MBR systems. By verifying the partition style first, showing detailed partition information for identification, detecting the correct boot partition automatically, and logging every change, you can fix boot failures quickly and safely. For modern UEFI/GPT systems, the active flag is not applicabl: use UEFI boot configuration tools (bcdboot, bcdedit) instead.