Skip to main content

How to Assign or Remove a Drive Letter with DiskPart in Batch Script

Drive letters are the access points to your storage volumes. When you plug in a new disk or USB drive, Windows automatically assigns the next available letter. But in a server environment, you need precise control, such as assigning D: to the data partition, L: to the log volume, and removing the letter from a sensitive backup drive so users can't browse it in Explorer. A Batch script can use DiskPart to assign, change, or remove drive letters, ensuring that your storage layout is predictable and consistent across machines.

This guide will explain how to manage drive letter assignments programmatically.

Understanding Volume Identification

DiskPart identifies volumes by number, but volume numbers can change between reboots when drives are added or removed. For reliable automation, you need to identify the correct volume before modifying it.

The safest approaches:

  1. By volume label: Query for a volume with a known label (e.g., "DataDrive") and find its number.
  2. By current drive letter: If you know the current letter, use it to identify the volume.
  3. By volume number: Use only after running list volume to confirm the current mapping.

All methods in this guide include identification and verification steps.

Method 1: Assign a Drive Letter to a Volume

This method assigns a specific drive letter to a volume, identified either by its current letter or by volume number with verification.

By Current Letter (Safest)

@echo off
setlocal

set "CurrentLetter=%~1"
set "NewLetter=%~2"

if "%NewLetter%"=="" (
echo Usage: %~nx0 ^<current_letter^> ^<new_letter^>
echo.
echo Reassigns a volume from one drive letter to another.
echo.
echo Example: %~nx0 E Z
echo (Changes drive E: to Z:^)
endlocal
exit /b 1
)

:: Verify admin privileges
net session >nul 2>&1
if errorlevel 1 (
echo [ERROR] DiskPart requires administrator privileges. >&2
endlocal
exit /b 1
)

:: Verify the source drive exists
if not exist %CurrentLetter%:\ (
echo [ERROR] Drive %CurrentLetter%: does not exist or is not accessible. >&2
endlocal
exit /b 1
)

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

:: Safety: prevent modifying C:
if /i "%CurrentLetter%"=="C" (
echo [ERROR] Refusing to modify the system drive (C:^). >&2
endlocal
exit /b 1
)

echo [INFO] Changing drive %CurrentLetter%: to %NewLetter%:

set "DPScript=%TEMP%\dp_assign_%RANDOM%.txt"
(
echo select volume %CurrentLetter%
echo remove letter=%CurrentLetter%
echo assign letter=%NewLetter%
) > "%DPScript%"

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

del "%DPScript%" 2>nul

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

:: Verify the new assignment
timeout /t 2 >nul
if exist %NewLetter%:\ (
echo [OK] Volume is now accessible as %NewLetter%:\
) else (
echo [WARNING] DiskPart reported success but %NewLetter%:\ is not accessible. >&2
)

endlocal
exit /b 0

By Volume Number (With Verification)

@echo off
setlocal

set "VolNum=%~1"
set "Letter=%~2"

if "%Letter%"=="" (
echo Usage: %~nx0 ^<volume_number^> ^<drive_letter^>
echo.
echo Run "diskpart" then "list volume" first to find the volume number.
echo.
echo Example: %~nx0 3 Z
endlocal
exit /b 1
)

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

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

:: Show current volumes so the user can verify the number
echo [INFO] Current volume configuration:
echo.

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

echo.
set /p "Confirm=Assign letter %Letter%: to Volume %VolNum%? (YES/no): "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)

echo [ACTION] Assigning letter %Letter%: to Volume %VolNum%...

set "DPScript=%TEMP%\dp_assign_%RANDOM%.txt"
(
echo select volume %VolNum%
echo assign letter=%Letter%
) > "%DPScript%"

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

del "%DPScript%" 2>nul

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

timeout /t 2 >nul
if exist %Letter%:\ (
echo [OK] Volume %VolNum% is now accessible as %Letter%:\
) else (
echo [WARNING] Assignment may have failed. Check Disk Management. >&2
)

endlocal
exit /b 0

Why select volume E works:

DiskPart's select volume command accepts both a volume number (select volume 3) and a drive letter (select volume E). Using the current drive letter is safer because it identifies the volume by its current assignment rather than by a number that may have changed since you last checked.

Method 2: Remove a Drive Letter (Hide a Volume)

Removing a drive letter hides the volume from File Explorer. The data remains intact and accessible via DiskPart, but casual users cannot navigate to it. This is useful for backup volumes, recovery partitions, or sensitive data drives.

@echo off
setlocal

set "Letter=%~1"

if "%Letter%"=="" (
echo Usage: %~nx0 ^<drive_letter^>
echo.
echo Removes the drive letter, hiding the volume from Explorer.
echo The data is NOT deleted. Use DiskPart to reassign a letter later.
echo.
echo Example: %~nx0 D
endlocal
exit /b 1
)

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

:: Safety: prevent removing C:
if /i "%Letter%"=="C" (
echo [ERROR] Refusing to remove the system drive letter (C:^). >&2
echo This would make Windows unbootable. >&2
endlocal
exit /b 1
)

:: Verify the drive exists
if not exist %Letter%:\ (
echo [ERROR] Drive %Letter%: does not exist or has no letter assigned. >&2
endlocal
exit /b 1
)

:: Show what's on the drive before hiding it
echo [INFO] Contents of %Letter%:\
dir %Letter%:\ /a /o 2>nul | findstr /i "File(s) Dir(s)" 2>nul
echo.

echo [WARNING] This will hide drive %Letter%: from File Explorer.
echo [WARNING] The data will NOT be deleted.
set /p "Confirm=Remove letter %Letter%:? (YES/no): "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)

echo [ACTION] Removing letter %Letter%:...

set "DPScript=%TEMP%\dp_remove_%RANDOM%.txt"
(
echo select volume %Letter%
echo remove letter=%Letter%
) > "%DPScript%"

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

del "%DPScript%" 2>nul

if %DPResult% neq 0 (
echo [ERROR] Failed to remove drive letter. >&2
endlocal
exit /b 1
)

:: Verify removal
timeout /t 2 >nul
if not exist %Letter%:\ (
echo [OK] Drive letter %Letter%: removed. Volume is now hidden.
echo [INFO] To restore access, run DiskPart and use: assign letter=%Letter%
) else (
echo [WARNING] Letter removal may have failed. %Letter%:\ is still accessible. >&2
)

endlocal
exit /b 0

Important: removing a letter is NOT security

Removing a drive letter hides the volume from Explorer, but any administrator can reassign it using DiskPart. This is not access control, it's convenience. For actual security, use NTFS permissions or BitLocker encryption.

Method 3: Swap Two Drive Letters

When two drives have incorrect letters (e.g., Data ended up on E: but should be on D:), you need a three-step swap: remove both letters, then assign them in the correct order.

@echo off
setlocal

set "Letter1=%~1"
set "Letter2=%~2"

if "%Letter2%"=="" (
echo Usage: %~nx0 ^<letter1^> ^<letter2^>
echo.
echo Swaps two drive letters.
echo.
echo Example: %~nx0 D E
echo (D: becomes E: and E: becomes D:^)
endlocal
exit /b 1
)

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

:: Safety checks
if /i "%Letter1%"=="C" (
echo [ERROR] Cannot swap the system drive. >&2
endlocal
exit /b 1
)
if /i "%Letter2%"=="C" (
echo [ERROR] Cannot swap the system drive. >&2
endlocal
exit /b 1
)

:: Verify both drives exist
if not exist %Letter1%:\ (
echo [ERROR] Drive %Letter1%: does not exist. >&2
endlocal
exit /b 1
)
if not exist %Letter2%:\ (
echo [ERROR] Drive %Letter2%: does not exist. >&2
endlocal
exit /b 1
)

echo [INFO] Swapping drive letters %Letter1%: and %Letter2%:
echo.

:: Show current labels for verification
for /f "tokens=6*" %%a in ('vol %Letter1%: 2^>nul ^| findstr /i "Volume"') do echo Current %Letter1%: label: %%a %%b
for /f "tokens=6*" %%a in ('vol %Letter2%: 2^>nul ^| findstr /i "Volume"') do echo Current %Letter2%: label: %%a %%b

echo.
set /p "Confirm=Proceed with swap? (YES/no): "
if /i not "%Confirm%"=="YES" (
echo [INFO] Cancelled.
endlocal
exit /b 0
)

:: Use a temporary letter to avoid conflicts during the swap
set "TempLetter=_"

echo [ACTION] Performing letter swap...

set "DPScript=%TEMP%\dp_swap_%RANDOM%.txt"
(
echo select volume %Letter1%
echo remove letter=%Letter1%
echo assign letter=%TempLetter%
echo select volume %Letter2%
echo remove letter=%Letter2%
echo assign letter=%Letter1%
echo select volume %TempLetter%
echo remove letter=%TempLetter%
echo assign letter=%Letter2%
) > "%DPScript%"

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

del "%DPScript%" 2>nul

if %DPResult% neq 0 (
echo [ERROR] Swap failed. Check Disk Management for current state. >&2
echo [INFO] You may need to manually reassign letters in DiskPart. >&2
endlocal
exit /b 1
)

:: Verify
timeout /t 2 >nul
if exist %Letter1%:\ if exist %Letter2%:\ (
echo [OK] Letters swapped successfully.
echo [OK] %Letter1%: and %Letter2%: are now reversed.
) else (
echo [WARNING] Swap may have partially completed. Check Disk Management. >&2
)

endlocal
exit /b 0

Why a temporary letter is needed:

DiskPart cannot assign a letter that is currently in use. The swap process is:

  1. Remove D: from Volume A → assign temporary letter _: to Volume A
  2. Remove E: from Volume B → assign D: to Volume B
  3. Remove _: from Volume A → assign E: to Volume A

Without the temporary letter, step 2 would fail because D: is still assigned to Volume A when you try to assign it to Volume B.

How to Avoid Common Errors

Wrong Way: Assigning a Letter That's Already in Use

DiskPart will fail if the target letter is assigned to another volume or mapped to a network share. The error message may not clearly indicate this.

Solution: All methods in this guide check if exist %Letter%:\ before attempting assignment. Also check for network drive mappings with net use.

Wrong Way: Removing the C: Drive Letter

Removing the letter from the system volume makes Windows unable to find its own files. The system may become unbootable immediately or on the next restart.

Solution: All methods in this guide refuse to operate on C: with a clear error message.

Problem: Volume Numbers Change Between Reboots

Adding or removing drives (especially USB devices) can shift volume numbers. Volume 3 in your last session may be Volume 4 today.

Solution: Use select volume E (by current letter) instead of select volume 3 (by number). If you must use numbers, always run list volume first and verify visually. Method 1 demonstrates both approaches.

Problem: Letter Assignment Doesn't Persist After Reboot

On some systems, Windows reassigns drive letters based on plug-in order during boot, overriding manual assignments.

Solution: Run the assignment script as a startup scheduled task to enforce your letter map on every boot. Alternatively, use Disk Management's GUI to set a permanent letter (right-click volume > Change Drive Letter and Paths), which is stored in the registry.

Problem: Hidden Volume Still Accessible to Administrators

Removing a drive letter hides the volume from Explorer but any administrator can reassign it via DiskPart. This is not a security control.

Solution: For actual access restriction, use NTFS permissions to control who can read the volume, or BitLocker to encrypt the entire drive.

Best Practices and Rules

1. Identify Volumes by Label or Current Letter

Volume numbers are unreliable across reboots. Use select volume E (current letter) or identify volumes by their label before operating on them.

2. Never Modify C:

The system drive letter should never be changed or removed by a script. All methods should include a safety check that refuses to operate on C:.

3. Verify After Every Operation

After assigning or removing a letter, check if exist %Letter%:\ to confirm the change took effect. DiskPart may report success even when the operation had no effect.

4. Document Your Drive Map

Maintain a record of which volumes have which letters and labels:

echo [%date% %time%] %Letter%: assigned to Volume "%Label%" >> "%~dp0drive_map.log"

This is essential for disaster recovery, as if the server crashes, you need to know how the drives were organized.

5. Use High Letters for Non-Standard Drives

Letters near the end of the alphabet (V, W, X, Y, Z) are rarely used by default assignments or network mappings. Use them for volumes that need stable, predictable letters.

6. Confirm Before Swapping

Drive letter swaps affect running applications. Any program with an open file handle on D:\ will lose access when the letter is removed. Warn users and close applications before performing a swap.

Conclusions

Managing drive letters via Batch script provides the precision and repeatability needed for professional server management.

By using DiskPart with proper identification, safety checks, and verification, you ensure that your storage topology is exactly as designed.

Whether assigning predictable letters to data volumes, hiding backup drives from casual browsing, or correcting swapped assignments, automated letter management eliminates the inconsistency of manual Disk Management operations.