How to List All Disk Partitions in Batch Script
For system administration, automated deployments, or diagnostic scripting, you often need to get information about the low-level layout of the storage devices. This means listing the partitions, the distinct segments a physical disk is divided into, rather than just the volumes (drive letters). Windows provides two powerful, built-in command-line tools for this task: WMIC and DISKPART.
This guide will teach you how to use both WMIC for easy, script-friendly data retrieval and DISKPART for more detailed, interactive-style listing. For most scripting needs where you just need to read information, WMIC is the recommended method.
The Modern Method (Recommended for Scripting): WMIC
The WMIC (Windows Management Instrumentation Command-line) utility is the best tool for querying system information in a script-friendly way. It can directly list all partitions and their properties.
Syntax: WMIC PARTITION GET Name, DeviceID, Size, Type
PARTITION: The WMI alias for theWin32_DiskPartitionclass.GET ...: The specific properties you want to retrieve.
Alternative Method (for Management): DISKPART
DISKPART is the primary interactive tool for managing disks, partitions, and volumes. While not ideal for parsing, it can be scripted to list partitions, and its output is often formatted in a more human-readable way.
You can not pipe commands to DISKPART. Instead, you must create a small script file with the DISKPART commands and then execute it.
For example, let's create a temporary script file list_partitions.txt:
LIST PARTITION
and then the Batch Script command is DISKPART /s list_partitions.txt where /s means "Runs a script".
Key Partition Properties Explained
DeviceID: The unique identifier for the partition on the system (e.g.,Disk #0, Partition #1).Name: A more descriptive name (e.g.,Disk #0, Partition #1).Size: The total size of the partition in bytes.Type: The type of partition (e.g.,Installable File System,Recovery,System).Bootable: Indicates if the partition is the active boot partition.DiskIndex: The index of the physical disk (0,1, etc.) that the partition belongs to.
Basic Example: Displaying All Partitions
This script runs both commands to show the difference in their output.
@ECHO OFF
ECHO --- Method 1: Using WMIC (Script-Friendly) ---
WMIC PARTITION GET Name, Size, Type
ECHO.
ECHO --- Method 2: Using DISKPART (Human-Readable) ---
ECHO SELECT DISK 0 > temp_diskpart.txt
ECHO LIST PARTITION >> temp_diskpart.txt
DISKPART /s temp_diskpart.txt
DEL temp_diskpart.txt
Note: The DISKPART example here is simplified to only show partitions on the first disk.
How to Capture the Partition Information in a Script
For automation, you need to capture the data into variables. WMIC is far superior for this, especially when formatted as CSV.
@ECHO OFF
SETLOCAL
ECHO --- Capturing All Partition Information ---
ECHO.
REM 'skip=1' ignores the header. 'tokens=2-5' grabs the 4 data columns from the CSV.
FOR /F "skip=1 tokens=2,3,4,5 delims=," %%A IN (
'WMIC PARTITION GET Bootable,DeviceID,Size,Type /FORMAT:CSV'
) DO (
ECHO DeviceID: "%%B" - Type: "%%D" - Bootable: "%%A"
)
ENDLOCAL
This method is robust because the comma delimiter reliably separates the fields.
Common Pitfalls and How to Solve Them
Problem: The Script is Not Run as Administrator
Both WMIC PARTITION and DISKPART require elevated privileges to access low-level disk information.
Example of error message:
Access is denied.
Solution: The script must be run as an Administrator. Right-click your .bat file or cmd.exe and select "Run as administrator."
Problem: Parsing the Output is Difficult
WMIC(Default): The default table format is hard to parse because theTypefield contains spaces.DISKPART: The output is purely for human reading and is very difficult to parse reliably in a script.
Solution: Always use the /FORMAT:CSV switch with WMIC when you intend to capture its output in a script. It makes parsing with FOR /F trivial and reliable. DISKPART should generally not be used if you need to capture its output for logic.
Practical Example: A Full Disk Layout Report
This advanced script creates a full report of the storage layout. It first lists the physical disks, and then for each disk, it lists all the partitions on it.
@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
ECHO --- System Disk and Partition Report ---
ECHO.
REM --- First, get a list of all physical disks ---
FOR /F "skip=1 tokens=2 delims=," %%D IN ('WMIC DISKDRIVE GET Index /FORMAT:CSV') DO (
SET "DiskIndex=%%D"
REM --- For each disk index, get the model name ---
FOR /F "skip=1 delims=" %%M IN ('WMIC DISKDRIVE WHERE Index^="!DiskIndex!" GET Model') DO (
SET "Model=%%M"
)
ECHO ==========================================================
ECHO Physical Disk !DiskIndex! - Model: !Model!
ECHO ==========================================================
REM --- Now, list all partitions for this specific disk index ---
WMIC PARTITION WHERE (DiskIndex="!DiskIndex!") GET Name, Size, Type
ECHO.
)
ENDLOCAL
Conclusion
Windows provides two powerful tools for listing disk partitions, each with its own strengths.
- The
WMIC PARTITIONcommand is the recommended best practice for scripting. It is designed for querying data and, when used with/FORMAT:CSV, provides clean, reliable output that is easy to parse with aFOR /Floop. - The
DISKPARTcommand is the primary tool for interactive management (creating, deleting, formatting). While it can be scripted with/s, its output is not ideal for parsing.
For any inventory or diagnostic script, WMIC is the superior and more professional choice. Always remember to run your script as an Administrator to ensure it has the necessary permissions.