How to List All Drives and Their Types (Fixed, CD-ROM, Removable) in a Batch Script
When writing batch scripts for system administration, software deployment, or data backup, it's often necessary to programmatically identify all connected drives and determine their type—for example, to distinguish between a local hard drive (Fixed), a CD/DVD drive (CD-ROM), or a USB flash drive (Removable).
This guide provides two robust methods using built-in Windows command-line tools to enumerate all connected drives and report their respective types, along with an explanation of common pitfalls to avoid.
Why You Need to Identify Drive Types
A script that can detect drive types can make intelligent decisions. Common use cases include:
- Backup Scripts: Ensuring backups are only written to fixed hard drives, not to a CD-ROM or a slow network drive.
- Software Installation: Checking for a CD or DVD in the optical drive before attempting to run an installer from it.
- System Inventory: Generating reports of all connected storage devices.
- Targeted Operations: Performing actions only on removable drives, such as safely ejecting all connected USB sticks.
Method 1: The Recommended Approach Using WMIC
For reliability and simplicity, the best method is to use the Windows Management Instrumentation Command-line (WMIC
). Its primary advantage is providing clean, language-independent output that is easy to process in a loop.
The Complete WMIC
Script
@ECHO OFF
CLS
ECHO Listing all connected drives and their types (using WMIC)...
ECHO ==========================================================
REM -- WMIC provides a clean list, one drive per line.
REM -- "skip=1" is used to ignore the header row ("Caption").
SETLOCAL EnableDelayedExpansion
FOR /F "skip=1" %%D in ('wmic logicaldisk get caption') do (
SET "driveLetter=%%D"
IF NOT "!driveLetter!"=="" (
REM -- Check if the drive letter contains a colon (valid drive letter like C:)
ECHO !driveLetter! | find ":" >nul
IF NOT errorlevel 1 (
REM -- Valid drive letter found, query its type using fsutil
ECHO Querying drive !driveLetter!
fsutil fsinfo drivetype !driveLetter!
)
)
)
ENDLOCAL
ECHO ==========================================================
ECHO Scan complete.
Output:
Listing all connected drives and their types (using WMIC)...
==========================================================
Querying drive C:
C: - Fixed Drive
Querying drive D:
D: - Fixed Drive
Querying drive E:
E: - CD-ROM Drive
==========================================================
Scan complete.
Script Breakdown
wmic logicaldisk get caption
: This command queries the system for all logical drives and asks for only one property: itscaption
(the drive letter, e.g.,C:
).FOR /F "skip=1" %%D IN ('...')
: This loop processes the output of thewmic
command.skip=1
: This is crucial. It tells the loop to ignore the first line of thewmic
output, which is just the header (Caption
).
FOR /F "tokens=" %%C IN ("%%D") DO
: This nested loop is a best practice for cleaning up output fromwmic
.wmic
can sometimes produce strange line endings that include extra carriage returns. This second loop re-processes the line, effectively trimming any unwanted whitespace or invisible characters.%%C
will now be a perfectly clean drive letter likeC:
.fsutil fsinfo drivetype %%C
: For each clean drive letter, this command is executed to get its type.
Example Output:
Listing all connected drives and their types (using WMIC)...
==========================================================
C: - Fixed Drive
D: - Removable Drive
E: - CD-ROM Drive
==========================================================
Scan complete.
Method 2: The Classic Approach Using fsutil
The fsutil
command can also achieve this, but it requires more careful parsing because its output is designed for human reading and can change based on the system's language (e.g., Drives:
in English vs. Unità:
in Italian).
The Correct fsutil
Script
@ECHO OFF
CLS
ECHO Listing all connected drives and their types (using fsutil)...
ECHO ============================================================
REM -- The command 'fsutil fsinfo drives' produces a line like "Drives: C:\ D:\ E:\"
REM -- We use "tokens=1,*" to split this into two parts.
SET "driveList="
FOR /F "tokens=1,*" %%A IN ('fsutil fsinfo drives') DO (
REM %%A will get the first token ("Drives:"). We ignore it.
REM %%B will get the rest of the line ("C:\ D:\ E:\").
SET "driveList=%%B"
)
REM -- Now, iterate through the clean list of drives.
FOR %%D IN (%driveList%) DO (
REM %%D will be "C:\", then "D:\", etc.
fsutil fsinfo drivetype %%D
)
ECHO ============================================================
ECHO Scan complete.
Output:
Listing all connected drives and their types (using fsutil)...
============================================================
C:\ - Fixed Drive
D:\ - Fixed Drive
E:\ - CD-ROM Drive
============================================================
Scan complete.
Script Breakdown
- Get the Drive Line:
fsutil fsinfo drives
outputs a single line, likeDrives: C:\ D:\ E:\
. - Parse the Line: The
FOR /F "tokens=1,*" %%A IN ('...')
command is the key to the solution.tokens=1,*
: This instructs the loop to split the line at the first space. The first part (token 1) goes into%%A
(this will beDrives:
), and the*
tells it to put everything else on the line into the next variable,%%B
.SET "driveList=%%B"
: We capture%%B
, which now contains the clean listC:\ D:\ E:\
, and store it in thedriveList
variable.
- Iterate the List: The second loop,
FOR %%D IN (%driveList%) DO ...
, iterates over the space-separated items in ourdriveList
variable and runsfsutil fsinfo drivetype
on each one.
Important Considerations
Drive Types Reported by fsutil
The fsutil fsinfo drivetype
command can return several values:
Type | Description |
---|---|
Fixed Drive | An internal hard drive (HDD) or Solid State Drive (SSD). |
Removable Drive | A USB flash drive, external USB hard drive, or SD card reader. |
CD-ROM Drive | A physical CD, DVD, or Blu-ray drive. |
Remote Drive | A network share mapped to a drive letter. |
Unknown Drive | The drive type could not be determined. |
No such Root Directory | The drive letter does not exist. |
Administrative Privileges Required
fsutil.exe
requires administrator privileges to query drive information. If you run these scripts as a standard user, they will fail with an "Error: Access is denied." message. You must right-click your batch file and select "Run as administrator".
Conclusion: Which Method Should You Use?
For new scripts, the WMIC
method is superior due to its reliability across different language versions of Windows and its simpler, cleaner output.
Feature | WMIC Method | fsutil Method |
---|---|---|
Reliability | Excellent. Language-independent. | Good. Works, but requires careful parsing. |
Simplicity | High. The logic is straightforward. | Medium. Requires a two-step parsing process. |
Compatibility | Excellent on modern Windows. WMIC is being deprecated but will be available for years. | Excellent. fsutil is a core utility. |
Use the WMIC
approach for robust, modern scripts. Use the fsutil
method if you have a specific reason to avoid WMIC
or are working in a very restricted environment. Both are excellent techniques to have in your scripting toolkit.