Skip to main content

How to Initialize and Format a New Disk with DiskPart in Batch Script

When you plug a brand-new hard drive or SSD into your computer, Windows often doesn't know what to do with it. The drive shows up as "Unallocated" in Disk Management, and it won't appear in File Explorer until someone initializes, partitions, and formats it. In a server or production environment, doing this manually for every new disk doesn't scale. A Batch script can use the DiskPart utility to perform the entire sequence, from raw, uninitialized hardware to a fully functional, labeled NTFS volume, in a single automated pass.

This guide will explain how to prepare new disks for use via the command line.

Critical Safety Warning

DiskPart's clean command permanently destroys ALL data on the selected disk. There is no confirmation prompt, no undo, and no recycle bin. Selecting the wrong disk number will erase your operating system, data drives, or backup volumes instantly.

Before running any initialization script:

  1. Run list disk to verify disk numbers and sizes.
  2. Confirm the target disk by matching its size to the new hardware you installed.
  3. Never hardcode select disk 0 - Disk 0 is almost always your operating system.
  4. Consider using the "Identify Before Initialize" pattern (Method 3) for interactive confirmation.

Method 1: Full Initialization (GPT + NTFS)

This method takes a clean, uninitialized disk and brings it to a usable state with a GPT partition table, an NTFS filesystem, and a specific drive letter.

Implementation

@echo off
setlocal

set "DiskNum=%~1"
set "DriveLetter=%~2"
set "Label=%~3"

if "%DiskNum%"=="" (
echo Usage: %~nx0 ^<disk_number^> [drive_letter] [volume_label]
echo.
echo disk_number The disk number from "list disk" (NEVER use 0^)
echo drive_letter Letter to assign (default: E^)
echo volume_label NTFS volume label (default: Data^)
echo.
echo IMPORTANT: Run "diskpart" then "list disk" first to identify the correct disk.
echo The CLEAN command DESTROYS ALL DATA on the selected disk.
echo.
echo Example: %~nx0 2 E DataDrive
endlocal
exit /b 1
)

if "%DriveLetter%"=="" set "DriveLetter=E"
if "%Label%"=="" set "Label=Data"

:: Verify admin privileges
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] DiskPart requires administrator privileges. >&2
echo Right-click and select "Run as administrator." >&2
endlocal
exit /b 1
)

:: Safety check: refuse to target disk 0
if "%DiskNum%"=="0" (
echo [ERROR] Refusing to initialize Disk 0 - this is typically your OS drive. >&2
echo If you are certain, edit the script to remove this safety check. >&2
endlocal
exit /b 1
)

:: Verify drive letter is available
if exist %DriveLetter%:\ (
echo [ERROR] Drive letter %DriveLetter%: is already in use. >&2
endlocal
exit /b 1
)

:: Show the disk list so the user can verify
echo [INFO] Current disk configuration:
echo.

set "DPList=%TEMP%\dp_list_%RANDOM%.txt"
(echo list disk) > "%DPList%"
diskpart /s "%DPList%" 2>nul
del "%DPList%" 2>nul

echo.
echo [WARNING] You are about to ERASE ALL DATA on Disk %DiskNum%.
echo [WARNING] This operation CANNOT be undone.
echo.

:: Require confirmation
set "Confirm="
set /p "Confirm=Type YES to proceed with Disk %DiskNum%: "
if /i not "%Confirm%"=="YES" (
echo [INFO] Operation cancelled by user.
endlocal
exit /b 0
)

echo.
echo [ACTION] Initializing Disk %DiskNum% as GPT with NTFS...

:: Create the DiskPart script
set "DPScript=%TEMP%\dp_init_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo clean
echo convert gpt
echo create partition primary
echo format fs=ntfs label="%Label%" quick
echo assign letter=%DriveLetter%
) > "%DPScript%"

:: Execute DiskPart
diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"

del "%DPScript%" 2>nul

if %DPResult% neq 0 (
echo [ERROR] DiskPart initialization failed. >&2
echo Possible causes: >&2
echo - Disk number %DiskNum% does not exist >&2
echo - Disk is in use or write-protected >&2
echo - USB enclosure firmware does not support the operation >&2
endlocal
exit /b 1
)

:: Verify the drive is accessible
timeout /t 2 >nul
if exist %DriveLetter%:\ (
echo [OK] Disk %DiskNum% initialized and mounted at %DriveLetter%:\
echo [OK] Partition: GPT, Filesystem: NTFS, Label: "%Label%"
) else (
echo [WARNING] DiskPart reported success but %DriveLetter%:\ is not accessible. >&2
echo Check Disk Management for details. >&2
)

endlocal
exit /b 0

When to use GPT:

  • UEFI systems (all modern Windows 10/11 machines)
  • Disks larger than 2 TB (MBR cannot address space beyond 2 TB)
  • Windows 10/11 and Server 2016+ (full GPT support)

GPT is the correct choice for all modern systems unless you have a specific compatibility requirement for MBR.

Method 2: MBR Legacy Format

Use MBR only for older BIOS-based systems, legacy hardware, or when compatibility with very old operating systems is required.

@echo off
setlocal

set "DiskNum=%~1"
set "DriveLetter=%~2"
set "Label=%~3"

if "%DiskNum%"=="" (
echo Usage: %~nx0 ^<disk_number^> [drive_letter] [volume_label]
echo.
echo Creates an MBR partition (for BIOS/legacy systems^).
echo.
echo Example: %~nx0 3 F LegacyDrive
endlocal
exit /b 1
)

if "%DriveLetter%"=="" set "DriveLetter=F"
if "%Label%"=="" set "Label=Data"

net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] Administrator privileges required. >&2
endlocal
exit /b 1
)

if "%DiskNum%"=="0" (
echo [ERROR] Refusing to target Disk 0. >&2
endlocal
exit /b 1
)

if exist %DriveLetter%:\ (
echo [ERROR] Drive letter %DriveLetter%: is already in use. >&2
endlocal
exit /b 1
)

echo [WARNING] This will ERASE ALL DATA on Disk %DiskNum%.
set /p "Confirm=Type YES to proceed: "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)

echo [ACTION] Initializing Disk %DiskNum% as MBR with NTFS...

set "DPScript=%TEMP%\dp_mbr_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo clean
echo convert mbr
echo create partition primary
echo format fs=ntfs label="%Label%" quick
echo assign letter=%DriveLetter%
) > "%DPScript%"

diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"

del "%DPScript%" 2>nul

if %DPResult% neq 0 (
echo [ERROR] Initialization failed. >&2
endlocal
exit /b 1
)

timeout /t 2 >nul
if exist %DriveLetter%:\ (
echo [OK] Disk %DiskNum% initialized as MBR at %DriveLetter%:\
) else (
echo [WARNING] Initialization may have partially succeeded. Check Disk Management. >&2
)

endlocal
exit /b 0

GPT vs. MBR comparison:

FeatureGPTMBR
Maximum disk size18 exabytes (effectively unlimited)2 TB
Maximum partitions1284 primary (or 3 primary + 1 extended)
Boot compatibilityUEFI onlyBIOS and UEFI (with compatibility mode)
Data protectionBackup partition table at end of diskSingle partition table - no redundancy
Recommended forAll modern systemsLegacy BIOS systems, USB drives for old hardware

Why active is not included:

The active command marks a partition as bootable: this is only appropriate when creating a boot partition for the operating system. For data drives, marking the partition as active is unnecessary and can cause boot confusion if the BIOS tries to boot from the data drive.

Method 3: Interactive Disk Identification and Initialization

For maximum safety, this method shows all connected disks, lets the user identify the correct one by size and status, and then requires explicit confirmation before proceeding.

@echo off
setlocal EnableDelayedExpansion

net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] Administrator privileges required. >&2
endlocal
exit /b 1
)

:: =============================================
:: Step 1: List all disks
:: =============================================
echo.
echo ============================================================
echo DISK INITIALIZATION TOOL
echo ============================================================
echo.
echo [INFO] Connected disks:
echo.

set "DPList=%TEMP%\dp_list_%RANDOM%.txt"
(echo list disk) > "%DPList%"
diskpart /s "%DPList%" 2>nul
del "%DPList%" 2>nul

echo.
echo [IMPORTANT] Match the disk SIZE to the new hardware you installed.
echo [IMPORTANT] Disk 0 is almost always your OS drive - DO NOT SELECT IT.
echo.

:: =============================================
:: Step 2: Get user input
:: =============================================
set /p "DiskNum=Enter disk number to initialize (1-9): "

:: Validate input
if "%DiskNum%"=="" (
echo [ERROR] No disk number entered. >&2
endlocal
exit /b 1
)

if "%DiskNum%"=="0" (
echo [ERROR] Refusing to target Disk 0 (OS drive protection^). >&2
endlocal
exit /b 1
)

set /p "DriveLetter=Enter drive letter to assign (e.g., E): "
if "%DriveLetter%"=="" set "DriveLetter=E"

if exist %DriveLetter%:\ (
echo [ERROR] Drive letter %DriveLetter%: is already in use. >&2
endlocal
exit /b 1
)

set /p "PartStyle=Partition style - GPT or MBR (default: GPT): "
if /i "%PartStyle%"=="" set "PartStyle=GPT"
if /i not "%PartStyle%"=="GPT" if /i not "%PartStyle%"=="MBR" (
echo [ERROR] Invalid partition style. Use GPT or MBR. >&2
endlocal
exit /b 1
)

set /p "VolumeLabel=Volume label (default: Data): "
if "%VolumeLabel%"=="" set "VolumeLabel=Data"

:: =============================================
:: Step 3: Confirm
:: =============================================
echo.
echo ============================================================
echo REVIEW YOUR SELECTIONS
echo ============================================================
echo.
echo Disk Number: %DiskNum%
echo Partition Style: %PartStyle%
echo Drive Letter: %DriveLetter%:
echo Volume Label: %VolumeLabel%
echo.
echo *** ALL DATA ON DISK %DiskNum% WILL BE PERMANENTLY ERASED ***
echo.
echo ============================================================

set /p "Confirm=Type ERASE to proceed (anything else cancels): "
if /i not "%Confirm%"=="ERASE" (
echo [INFO] Operation cancelled. No changes were made.
endlocal
exit /b 0
)

:: =============================================
:: Step 4: Execute
:: =============================================
echo.
echo [ACTION] Initializing Disk %DiskNum%...

set "DPScript=%TEMP%\dp_interactive_%RANDOM%.txt"
(
echo select disk %DiskNum%
echo clean
echo convert %PartStyle%
echo create partition primary
echo format fs=ntfs label="%VolumeLabel%" quick
echo assign letter=%DriveLetter%
) > "%DPScript%"

diskpart /s "%DPScript%" >nul 2>&1
set "DPResult=%errorlevel%"

del "%DPScript%" 2>nul

if %DPResult% neq 0 (
echo [ERROR] DiskPart initialization failed. >&2
endlocal
exit /b 1
)

timeout /t 2 >nul
if exist %DriveLetter%:\ (
echo.
echo [OK] Disk %DiskNum% initialized successfully.
echo [OK] %PartStyle% partition, NTFS, label "%VolumeLabel%", drive %DriveLetter%:\
) else (
echo [WARNING] DiskPart reported success but drive is not accessible. >&2
)

endlocal
exit /b 0

Why the confirmation word is "ERASE" instead of "YES":

Requiring the user to type the word "ERASE" (rather than just "Y" or "YES") ensures they consciously acknowledge the destructive nature of the operation. It's much harder to type "ERASE" accidentally than to reflexively press "Y" in response to a prompt.

How to Avoid Common Errors

Wrong Way: Hardcoding Disk 0

:: CATASTROPHIC: Disk 0 is almost always the OS drive
select disk 0
clean

This instantly destroys your Windows installation. There is no undo.

Correct Way: Always run list disk first, identify the target by its size, and never hardcode disk 0 in a script. Methods 1 and 2 refuse to accept disk 0 as a parameter.

Wrong Way: No Confirmation for Destructive Operations

A script that runs clean without any user confirmation is dangerous: a typo in the disk number, a script modification error, or an unexpected disk enumeration change can destroy the wrong drive.

Correct Way: All methods in this guide require explicit confirmation (typing "YES" or "ERASE") before executing the clean command.

Problem: USB Enclosures Rejecting Commands

Some USB-to-SATA/NVMe enclosures have firmware that doesn't fully support clean or convert gpt commands, producing "Virtual Disk Service error" messages.

Solution: Try convert mbr instead (which has better USB compatibility). If that also fails, connect the drive directly via SATA or NVMe.

Problem: Disk Number Changes Between Boots

Disk numbers are assigned dynamically based on detection order. Adding or removing a USB drive can shift the numbers, causing Disk 2 from your last session to now be Disk 3.

Solution: Always run list disk immediately before initialization and identify the target by its size (in GB), not by a remembered disk number from a previous session.

Problem: Quick Format vs. Full Format

format ... quick only initializes the filesystem metadata: it does not scan for bad sectors. On a new, untested drive, bad sectors could cause silent data corruption later.

Solution: For new hardware, consider using a full format (omit the quick keyword). This scans every sector and marks bad ones, but takes significantly longer (hours for large drives).

Best Practices and Rules

1. Always List Disks Before Operating

Run list disk and verify the target by size and status. Never rely on remembered disk numbers.

2. Use GPT for Modern Systems

Use GPT for all UEFI systems and disks over 2 TB. Use MBR only for BIOS compatibility with older hardware.

3. Require Explicit Confirmation

Any script that runs DiskPart's clean command should require the user to type a confirmation word. Accidental data destruction is permanent.

4. Protect Disk 0

Build a safety check that refuses to operate on Disk 0. While there are rare legitimate reasons to reinitialize Disk 0, a general-purpose script should never do it by default.

5. Verify After Initialization

After DiskPart completes, verify the drive letter is accessible with if exist %DriveLetter%:\. Report success only after confirmation.

6. Record the Operation

Log the disk number, size, partition style, and timestamp for asset tracking:

echo [%date% %time%] Initialized Disk %DiskNum% as %PartStyle% on %DriveLetter%: >> "%~dp0disk_operations.log"

Conclusions

Initializing and formatting new disks via Batch script transforms a manual, error-prone task into a reliable, repeatable procedure. By combining DiskPart automation with mandatory safety checks, disk identification, Disk 0 protection, explicit confirmation prompts, and post-initialization verification, you gain the speed and consistency needed for data center deployments and workstation setups while protecting against the catastrophic risk of erasing the wrong drive.