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

Solution 1:

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

Solution 2:

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

echo list volume | diskpart

Solution 3:

@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.


Solution 4:

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.


Solution 5:

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:\