Skip to content

How to List All Disk Volumes in a Batch Script

Learn the two best methods, using WMIC (recommended for scripting) and diskpart, to programmatically list all disk volumes in a Windows batch script, including parsing techniques and common pitfalls.

A disk volume is a storage area (typically a formatted partition) that Windows can access, usually identified by a drive letter like C: or D:. System administrators and power users often need to programmatically list all available volumes to perform audits, check for free space across all drives, or find a specific volume to operate on.

This guide will teach you the two primary built-in methods for listing disk volumes. We'll cover the diskpart utility, which is great for human-readable output, and the more powerful and script-friendly WMIC command, which is the recommended method for any automation task.

The WMIC (Windows Management Instrumentation Command-line) utility is the definitive tool for querying system information in a script-friendly format. The LOGICALDISK alias provides direct access to volume information.

Command: WMIC LOGICALDISK GET Caption,VolumeName,Size,FreeSpace

  • LOGICALDISK: The WMI class for volumes.
  • GET ...: Specifies the properties you want to display.

This command produces a clean, well-structured table that is easy to read and parse.

Caption  FreeSpace     Size          VolumeName
C:       129393303552  511002210304  Windows
D:       875432109876  999876543210  Data
E:

This is the best method for scripting because the output is consistent and predictable.

Method 2 (For Display): Using diskpart

The diskpart.exe utility is an interactive shell for managing disks. While not ideal for parsing, its list volume command provides a quick, human-readable summary similar to the one in the graphical Disk Management tool.

To use it in a script, you must feed it a command script.

@ECHO OFF
REM This script requires Administrator privileges.
SET "DISKPART_SCRIPT=%TEMP%\list_volumes.txt"

ECHO list volume > "%DISKPART_SCRIPT%"

ECHO --- Listing volumes using diskpart ---
diskpart /s "%DISKPART_SCRIPT%"

DEL "%DISKPART_SCRIPT%"

The output is formatted for people, not scripts, but is very clear.

  Volume ###  Ltr  Label        Fs     Type        Size     Status     Info
  ----------  ---  -----------  -----  ----------  -------  ---------  --------
  Volume 0     D   Data         NTFS   Partition    931 GB  Healthy
  Volume 1     C   Windows      NTFS   Partition    475 GB  Healthy    Boot
  Volume 2         Recovery     NTFS   Partition    529 MB  Healthy    Hidden

Parsing the WMIC Output to Process Each Volume

The real power of the WMIC method is the ability to use its output in a FOR loop to perform an action on each volume.

This script iterates through each logical disk and prints its drive letter.

@ECHO OFF
ECHO --- Iterating through all volumes ---
ECHO.

REM The 'skip=1' ignores the header line of the WMIC output.
FOR /F "skip=1 tokens=1" %%V IN ('WMIC LOGICALDISK GET Caption') DO (
    ECHO Found Volume: %%V
)

Output:

--- Iterating through all volumes ---

Found Volume: C:
Found Volume: D:
Found Volume: E:

You can now perform actions on %%V inside the loop, such as checking its free space or listing its contents.

Key WMIC LOGICALDISK Properties Explained

You can GET many useful properties from the LOGICALDISK class:

  • Caption: The drive letter (e.g., C:).
  • VolumeName: The user-assigned label of the volume (e.g., "Data").
  • Size: The total size of the volume in bytes.
  • FreeSpace: The available free space in bytes.
  • FileSystem: The file system type (e.g., NTFS, FAT32).
  • DriveType: A number representing the type of drive. The most common are:
    • 2: Removable Disk (USB drive)
    • 3: Local Fixed Disk (HDD/SSD)
    • 4: Network Drive
    • 5: CD-ROM

Common Pitfalls and How to Solve Them

  • Administrator Rights: For a complete and accurate list of all volumes (including hidden recovery partitions), your script must be run as an Administrator.
  • WMIC Output Quirks: WMIC output can sometimes include invisible trailing carriage return characters that can break string comparisons or calculations. Solution: Clean the variable by re-assigning it in a simple FOR loop: FOR %%N IN ("%Var%") DO SET "Var=%%~N".
  • Filtering by Drive Type: You often only want to process local hard drives. You can filter the WMIC query directly.
    REM This gets local fixed disks only.
    WMIC LOGICALDISK WHERE "DriveType=3" GET Caption

Practical Example: A Drive Space Report Script

This script uses the robust WMIC method to generate a report on the usage of all local fixed disks on the system.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION

ECHO --- Disk Usage Report for Local Fixed Disks ---
ECHO Generated on: %DATE% %TIME%
ECHO =================================================

REM The 'skip=1' ignores the header. tokens=1,2,3 gets the three columns.
FOR /F "skip=1 tokens=1,2,3" %%A IN (
    'WMIC LOGICALDISK WHERE "DriveType=3" GET Caption^,FreeSpace^,Size'
) DO (
    SET "Drive=%%A"
    SET "FreeBytes=%%B"
    SET "TotalBytes=%%C"
    
    REM Clean the variables
    FOR %%N IN (!FreeBytes!) DO SET "FreeBytes=%%N"
    FOR %%N IN (!TotalBytes!) DO SET "TotalBytes=%%N"
    
    REM Calculate GB values (this may fail on drives > 2TB with 32-bit math)
    SET /A "FreeGB = !FreeBytes! / 1073741824"
    SET /A "TotalGB = !TotalBytes! / 1073741824"
    
    ECHO Drive !Drive! -- Total: !TotalGB! GB -- Free: !FreeGB! GB
)

ENDLOCAL

Conclusion

While both diskpart and WMIC can list system volumes, they serve different purposes for scripting.

  • diskpart is best for a quick, human-readable display that mimics the Disk Management tool.
  • WMIC LOGICALDISK is the overwhelmingly superior and recommended method for scripting. It provides clean, predictable, and parsable output that can be easily captured in a FOR loop for automation.

By using WMIC, you can write powerful scripts that can audit, report on, or perform actions on every volume on a system.

Navigation