How to get a list of drive letters on a system through a windows shell (bat, cmd)?

How do I get a list of drive letters and their associated labels on a windows system through a bat file?


This will get most of it:

Net Use

If you have any drives mapped via subst you would also need to get those:

Subst

For completeness, you would do it like this in Powershell (if you are on windows 7 or have installed it):

gwmi win32_LogicalDisk -filter DriveType=4

You can also do it from the command prompt or a batch file using WMI like this:

wmic logicaldisk get caption,providername,drivetype,volumename

To use diskpart, there is no need to create an intermediate file. Try:

echo list volume | diskpart

@echo off
cls
setlocal enabledelayedexpansion
set "_DRIVE.LETTERS.FREE=Z Y X W V U T S R Q P O N M L K J I H G F E D C B A "
for /f "skip=1 tokens=1,2 delims=: " %%a in ('wmic logicaldisk get deviceid^,volumename') do (
   set "_DRIVE.LETTERS.USED=!_DRIVE.LETTERS.USED!%%a,%%b@"
   set "_DRIVE.LETTERS.FREE=!_DRIVE.LETTERS.FREE:%%a =!"
)
set _DRIVE.LETTERS.USED=%_DRIVE.LETTERS.USED:~0,-2%
set _DRIVE.LETTERS.USED=%_DRIVE.LETTERS.USED:,@=, @%
set _DRIVE.LETTERS

Fast, flexible and efficient. Although a little complex.


inspired by Scott

for %i 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 @%i: 2>nul && set/pz=%i <nul

also shows CD drive letters and network mounted disk letters. No need to have admin rights.


mountvol

sample output

\\?\Volume{11197e59-f977-11dd-afc6-111e6f6e6963}\
   *** NO MOUNTING POINT ***

\\?\Volume{11197e59-f977-11dd-afc6-111e6f6e6963}\
   D:\

\\?\Volume{11197e59-f977-11dd-afc6-111e6f6e6963}\
   C:\

\\?\Volume{11197e59-f977-11dd-afc6-111e6f6e6963}\
   E:\