How to Get the Volume Label of a Drive in Batch Script
The "volume label" is the user-friendly name assigned to a disk drive partition, such as "Windows" for the C: drive or "BackupDrive" for an external hard drive. In scripting, retrieving this label is a powerful way to verify that you are working with the correct drive before performing a critical operation like a format or a backup. It's a much more reliable check than just relying on the drive letter, which can change.
This guide will teach you the two main methods for getting a drive's volume label. We'll cover the simple, classic VOL command and the more modern and script-friendly WMIC command, which is the recommended approach for reliable automation.
Classic Method: Parsing the VOL Command
The VOL command is a simple, legacy utility designed for one purpose: to display the volume label and serial number of a disk.
Syntax: VOL [drive:]
The output format is designed for humans and, crucially, it changes depending on whether the drive has a label or not, making it difficult to parse in a script.
Output (with a label):
Volume in drive C is Windows
Volume Serial Number is 1234-ABCD
Output (without a label):
Volume in drive C has no label.
Volume Serial Number is 1234-ABCD
The Modern Method (Recommended): Using WMIC
A much more robust and predictable method is to use WMIC (Windows Management Instrumentation Command-line) to query the properties of the logical disk.
Syntax: WMIC LOGICALDISK WHERE "DeviceID='C:'" GET VolumeName
LOGICALDISK: The WMI alias for a drive partition.WHERE "DeviceID='C:'": The filter to select only the C: drive.GET VolumeName: The specific property we want to retrieve.
The output of this command is clean, structured, and easy to parse.
Basic Example: Displaying the Volume Label
This script runs both commands to show the difference in their output for the C: drive.
@ECHO OFF
ECHO --- Getting Volume Label for C: ---
ECHO.
ECHO Method 1: Using VOL (human-readable)
VOL C:
ECHO.
ECHO Method 2: Using WMIC (script-friendly)
WMIC LOGICALDISK WHERE "DeviceID='C:'" GET VolumeName
How to Capture the Label in a Variable
To use the label in your script's logic, you must capture the command's output into a variable using a FOR /F loop. This is where the superiority of the WMIC method becomes clear.
Script capturing VOL output (Complex and Fragile)
@ECHO OFF
SET "DriveLabel="
FOR /F "tokens=4*" %%A IN ('VOL C:') DO (
SET "DriveLabel=%%A %%B"
GOTO :DoneVol
)
:DoneVol
ECHO VOL Method Result: "%DriveLabel%"
This is fragile because it assumes the label is always the 4th token and might fail if the drive has no label.
Script capturing WMIC output (Recommended)
@ECHO OFF
SET "DriveLabel="
FOR /F "skip=1 delims=" %%L IN (
'WMIC LOGICALDISK WHERE "DeviceID='C:'" GET VolumeName'
) DO (
SET "DriveLabel=%%L"
GOTO :DoneWmic
)
:DoneWmic
ECHO WMIC Method Result: "%DriveLabel%"
The skip=1 ignores the header, and the GOTO exits after capturing the first line of data, making it very reliable.
How the Methods Work
VOL: This is a simple, legacy command that reads the most basic information from the file system's boot sector.WMIC: This is a powerful interface to the WMI service. It queries theWin32_LogicalDiskobject class, which holds dozens of properties about the drive, including itsVolumeName. This is a more modern and structured way to get system information.
Common Pitfalls and How to Solve Them
Problem: The VOL Command's Output is Inconsistent
This is the main reason to avoid using VOL in scripts. The line containing the label is different when there is no label.
Volume in drive C is WindowsVolume in drive C has no label.AFOR /Floop designed to parse the first line will fail on the second. You would have to build a much more complex script with multipleFINDSTRcommands to handle both cases.
Solution: Use the WMIC method. Its output is always the same format: a header and then the value (which might be empty, but the structure is consistent).
Problem: The Drive Has No Label
VOLMethod: The output text changes, making parsing difficult.WMICMethod: The command simply returns an empty value. Your script can easily check for this.
Example of script to handle a missing label with WMIC:
@ECHO OFF
SET "DriveLabel="
FOR /F "skip=1 delims=" %%L IN ('WMIC ...') DO ( ... )
IF NOT DEFINED DriveLabel (
ECHO The drive has no label.
) ELSE (
ECHO The drive label is: %DriveLabel%
)
Practical Example: A Safe Backup Script
This script's purpose is to back up data to an external drive, which the user says is the E: drive. Before it does anything destructive, it checks the volume label of E: to make sure it's named "BACKUP_DRIVE", preventing the user from accidentally formatting the wrong external drive.
@ECHO OFF
SETLOCAL
SET "BackupDriveLetter=E:"
SET "ExpectedLabel=BACKUP_DRIVE"
SET "ActualLabel="
ECHO --- Safe Backup Script ---
ECHO Verifying that drive %BackupDriveLetter% is the correct backup drive...
IF NOT EXIST "%BackupDriveLetter%\" (ECHO [ERROR] Drive %BackupDriveLetter% not found! & GOTO :End)
REM --- Use the reliable WMIC method to get the label ---
FOR /F "skip=1 delims=" %%L IN (
'WMIC LOGICALDISK WHERE "DeviceID='%BackupDriveLetter%'" GET VolumeName'
) DO (
SET "ActualLabel=%%L"
GOTO :GotLabel
)
:GotLabel
ECHO The label for drive %BackupDriveLetter% is: "%ActualLabel%"
IF /I "%ActualLabel%"=="%ExpectedLabel%" (
ECHO [SUCCESS] Drive label matches. Proceeding with backup...
REM (Your Robocopy backup commands would go here)
) ELSE (
ECHO [FAILURE] Wrong drive! The label does not match "%ExpectedLabel%".
ECHO Backup aborted to prevent data loss.
)
:End
ENDLOCAL
Conclusion
Getting a drive's volume label is a key technique for writing safe and intelligent scripts that interact with storage devices.
- The
VOLcommand is fine for quick, interactive checks by a human, but its inconsistent output makes it unsuitable for reliable scripting. - The
WMIC LOGICALDISK GET VolumeNamecommand is the overwhelmingly recommended best practice. It provides clean, predictable output that is easy to parse with aFOR /Floop and handles all cases gracefully.