How to List Drive Letters and Labels in Windows Using Batch Script (CMD/BAT)


1 views

When working with Windows systems, you may need to programmatically retrieve a list of available drives along with their volume labels. This is particularly useful for:

  • Disk management utilities
  • Backup scripts
  • System inventory tools
  • Automated deployment scripts

The most reliable way to get drive information is through WMIC (Windows Management Instrumentation Command-line):

wmic logicaldisk get caption, volumename

This command returns output like:

Caption  VolumeName
C:       OS
D:       Data
E:       Backup

For newer Windows versions, you can use:

fsutil fsinfo drives

To get labels with fsutil:

@echo off
for %%d in (A B C D E F G H I J K L M N O P Q R S T U V W X Y Z) do (
    if exist "%%d:\" (
        fsutil fsinfo volumeinfo %%d: | find "Volume Name"
    )
)

Here's a robust script that handles both letters and labels:

@echo off
setlocal enabledelayedexpansion

echo Drive Letters and Labels:
echo -------------------------

for /f "tokens=1,2" %%a in ('wmic logicaldisk get caption^, volumename ^| find ":"') do (
    set "drive=%%a"
    set "label=%%b"
    if "!label!"=="" set "label=No Label"
    echo !drive! - !label!
)

endlocal

To include network drives in your listing:

wmic logicaldisk where "drivetype=3 or drivetype=4" get caption, volumename

Add error checking to make your script more robust:

@echo off
setlocal enabledelayedexpansion

echo Checking available drives...
echo.

set "errorcount=0"
for /f "tokens=1,2" %%a in ('wmic logicaldisk get caption^, volumename 2^>^&1 ^| find ":"') do (
    if errorlevel 1 (
        set /a "errorcount+=1"
        echo Error reading drive information >&2
    ) else (
        echo %%a - %%b
    )
)

if %errorcount% gtr 0 (
    echo Warning: %errorcount% errors occurred while reading drive information >&2
)

endlocal

For better readability in reports:

@echo off
echo ^
echo Drive Report - %date% %time%
echo ----------------------------
wmic logicaldisk get caption, volumename, size, freespace | findstr /v "Caption"
echo ^

When working with batch scripts, you'll often need to interact with the system's storage devices. Windows provides several methods to enumerate logical drives, each with their own advantages.

For older systems (pre-Windows 10), WMIC offers reliable drive information:

@echo off
for /f "skip=1 tokens=1,2" %%a in ('wmic logicaldisk get caption^, volumename') do (
    if not "%%a"=="" (
        echo Drive: %%a  Label: %%b
    )
)

For newer systems, combining batch with PowerShell provides more robust results:

@echo off
for /f "tokens=1,2 delims=:" %%a in ('powershell -command "Get-Volume | Where-Object {$_.DriveLetter} | Select-Object -Property DriveLetter, FileSystemLabel | ConvertTo-Csv -NoTypeInformation"') do (
    if not "%%a"=="" (
        set "drive=%%a"
        set "label=%%b"
        setlocal enabledelayedexpansion
        echo Drive: !drive:~1!  Label: !label!
        endlocal
    )
)

Another built-in tool that doesn't require admin privileges:

@echo off
for /f "tokens=3 delims= " %%d in ('fsutil fsinfo drives') do (
    for %%a in (%%d) do (
        for /f "tokens=4*" %%l in ('fsutil fsinfo volumeinfo %%a ^| find "Volume Name"') do (
            echo Drive: %%a  Label: %%m
        )
    )
)

For comprehensive scripts, consider these scenarios:

  • Network drives (may require different handling)
  • USB drives that might be temporarily disconnected
  • Drives without labels
  • Systems with more than 26 drives

Each method has different performance characteristics:

  1. WMIC: Slowest but most compatible
  2. PowerShell: Faster on modern systems
  3. FSUTIL: Moderate speed, good for simple cases