29

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

Eduardo Ivanec
  • 14,881
  • 1
  • 37
  • 43
Chris Magnuson
  • 3,771
  • 10
  • 42
  • 46

14 Answers14

41

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
Dennis Williamson
  • 62,149
  • 16
  • 116
  • 151
EBGreen
  • 1,453
  • 11
  • 10
  • 1
    This doesn't list local disk drives like any internal hard drives or cdroms. Am I missing something? – Chris Magnuson Sep 04 '09 at 21:12
  • Nope, I misunderstood the question. I thought that you only wanted mapped drives. I will edit. – EBGreen Sep 04 '09 at 21:18
  • 2
    The wmic solution should give you everything. – EBGreen Sep 04 '09 at 21:20
  • You need to remove the quotes from wmic logicaldisk get "caption,providername,drivetype" so that it is wmic logicaldisk get caption,providername,drivetype other wise it throws an error. EBGreen you have single handedly blown my mind for the day, I have never seen wmic even though I have been scripting various server builds for weeks and I am extremely excited to see what else I can do with it. Great job! – Chris Magnuson Sep 04 '09 at 21:24
  • That's odd, when I was testing it I tried it first without the quotes because that is how you would actually do it in the WMIC console, but when I was passing the command into the console from a regular command prompt I had to add the quotes. – EBGreen Sep 04 '09 at 21:26
  • Is there a way to get the labels to display as well? Caption just prints the drive letter. – Chris Magnuson Sep 04 '09 at 21:26
  • 1
    wmic logicaldisk get caption,providername,drivetype,volumename the VolumeName was what I was missing, if you update yours with that I will accept it as the answer. Thanks – Chris Magnuson Sep 04 '09 at 21:31
  • Please remove the "". I have tested this on four computers and have asked another co-worker to test this and they all require that the double quotes be removed. I want to make sure the solution is something that most people can just copy and paste and have it work. – Chris Magnuson Sep 09 '09 at 00:07
  • Done. No idea why I need them on my system. – EBGreen Sep 09 '09 at 13:24
  • If you just run **wmic logicaldisk** you'll get a full list of all the columns that are available. – slm Dec 21 '11 at 03:42
  • Thanks for the wmic! I've been wanting something to do this easily for years and for some reason I never tried tht. Doh! – Ken Nov 08 '13 at 16:08
16

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

echo list volume | diskpart
gnoise
  • 161
  • 1
  • 6
11
@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.

Dharma Leonardi
  • 161
  • 2
  • 6
  • I don't know what versions of Windows this works on but I just tested it on my Win 7 and XP machines and got the following output from both: ` DRIVE LETTER(s):USED: " =" DRIVE LETTER(s):FREE: "!TMP_FREE_DRIVE_LETTERS!"` – John Gardeniers May 12 '11 at 00:44
  • Sorry, you need to put this in the beginning of the batch file. setlocal enabledelayedexpansion – Dharma Leonardi May 12 '11 at 03:19
  • I've edited your answer to include the missing line. It works now. +1 – John Gardeniers May 12 '11 at 03:31
7

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.

Geoleven
  • 3
  • 4
Yuning
  • 171
  • 1
  • 3
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:\
Jader Dias
  • 4,705
  • 19
  • 50
  • 51
4

If anyone is lucky enough to be using Vista (Vista Ultimate SP2 b6002, in my case) and the gwmi and wmic snippets given here don't work exactly, here is what I did to make it work.

For gwmi, if you receive no output, try changing the DriveType to 3. If still having problems, remove the -filter option altogether and analyze output.

gwmi win32_LogicalDisk -filter DriveType=3

For wmic, if you receive "Invalid GET Expression", then try putting the get expression in quotes:

wmic logicaldisk get "caption,providername,drivetype,volumename"
tdowg1
  • 41
  • 2
2
@echo off
echo list volume > scriptdiskpart
diskpart/s scriptdiskpart
del scriptdiskpart
pause
phuclv
  • 169
  • 1
  • 16
5d4gd
  • 21
  • 1
  • I tried this one and it didn't work. I think there needs to be a space in front of the /s but even with that change it didn't work. I put the above in a file, d.bat and I see the diskpart window flash up but then disappear and the cmd shell where I'm running this then shows the pause running. – slm Dec 21 '11 at 03:27
2

This site has a much simpler set of calls:

http://en.code-bude.net/2013/02/23/show-all-drives-in-command-prompt/

Show local drives:

wmic logicaldisk get deviceid, volumename, description


If you want to show only drives of a particular type, the wmic command can be further expanded to include a where clause.

wmic logicaldisk where drivetype=2 get deviceid, volumename, description

Types

0 => Unknown

1 => No Root Directory

2 => Removable Disk

3 => Local Disk

4 => Network Drive

5 => Compact Disc

6 => RAM Disk

David Tr
  • 21
  • 1
1
for %a in (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 @vol %a 2>&1 | find "drive"

I found the diskpart approach reports DVD drives with no disc inserted, and doesn't report SD cards with valid drive volumes

jscott
  • 24,484
  • 8
  • 79
  • 100
Nick
  • 11
  • 1
1
for %%p 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 if exist %%p:\nul your_command_and_parameters_here
pbies
  • 169
  • 13
0

Using wmic in a for loop to determine all the available drives.

@echo off
for /f "tokens=2*delims==" %%i in ('wmic logicaldisk get caption /value') do for /f "delims=" %%d in ("%%i") do echo %%d

There is also this little hack using exit codes 65 to 90 converted to ascii which will return all Alpabetical characters A to Z with all credit to Aacini.

@echo off
setlocal enabledelayedexpansion
for /L %%i in (65,1,90) do cmd /C exit %%i & if exist "!=ExitCodeAscii!:" echo !=ExitCodeAscii!:
Gerhard
  • 101
0

Although it has enough answer, I'd like to add one when you want to use it in batch file. If you get "Invalid GET Expression", you could put a ^ before the ',', like below:

for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,providername^,drivetype^,volumename 2^>NUL`) do echo %%i %%l
0

Somewhat kludgy, but works from a batch file:

echo LIST VOLUME > temp.txt && diskpart /s temp.txt && del /q temp.txt
Nexus
  • 850
  • 1
  • 8
  • 19
-1
@ECHO OFF
IF NOT EXIST A: GOTO B
:A
VOL A:
:B
IF NOT EXIST B: GOTO C
VOL B:
:C
IF NOT EXIST C: GOTO D
VOL C:
:D
IF NOT EXIST D: GOTO E
VOL D:
:E
IF NOT EXIST E: GOTO F
VOL E:
:F
IF NOT EXIST F: GOTO G
VOL F:
:G
IF NOT EXIST G: GOTO H
VOL G:
:H
IF NOT EXIST H: GOTO I
VOL H:
:I
IF NOT EXIST I: GOTO J
VOL I:
:J
IF NOT EXIST J: GOTO K
VOL J:
:K
IF NOT EXIST K: GOTO L
VOL K:
:L
IF NOT EXIST L: GOTO M
VOL L:
:M
IF NOT EXIST M: GOTO N
VOL M:
:N
IF NOT EXIST N: GOTO O
VOL N:
:O
IF NOT EXIST O: GOTO P
VOL O:
:P
IF NOT EXIST P: GOTO Q
VOL P:
:Q
IF NOT EXIST Q: GOTO R
VOL Q:
:R
IF NOT EXIST R: GOTO S
VOL R:
:S
IF NOT EXIST S: GOTO T
VOL S:
:T
IF NOT EXIST T: GOTO U
VOL T:
:U
IF NOT EXIST U: GOTO V
VOL U:
:V
IF NOT EXIST V: GOTO W
VOL V:
:W
IF NOT EXIST w: GOTO X
VOL W:
:X
IF NOT EXIST X: GOTO Y
VOL X:
:Y
IF NOT EXIST Y: GOTO Z
VOL Y:
:Z
IF NOT EXIST Z: GOTO END
VOL Z:
:END
Scott
  • 1,173
  • 3
  • 13
  • 25
  • Um, wow. So if you can distill that into something that would actually just output the drive letters and labels that exist on the system that would be closer to the answer to the question though this is it least movement in the right direction. – Chris Magnuson Sep 04 '09 at 21:16
  • I'm getting down-voted for providing a solution that meets the criteria? Wow... – Scott Sep 05 '09 at 05:19
  • 4
    Well, for one thing, it produces errors for drives that exist but contain no media, such as a CD/DVD drive. – Dennis Williamson Sep 06 '09 at 12:47