8

I have a USB drive which letter/path could differ, but its name never changes. I would like to find the path/letter of this drive, by looking up its name. Is there a cmd command which could do this for me? I've been searching for a good while and can't come up with a good solution.

Right now I have:

wmic path CIM_LogicalDevice where "Name like 'driveName'" get name

which confirms for me that the specific drive is indeed plugged in, but how would I go about finding its letter now?

I found it can be done using this PowerShell command:

Get-WMIObject Win32_Volume | ? { $_.Label -eq 'driveName' }

But I can't use PowerShell in my case...

InSync
  • 103
  • 3
W.Donald
  • 327

3 Answers3

13

Run a powershell command in cmd like this:

powershell -command "Get-WMIObject Win32_Volume | ? { $_.Label -eq 'driveName' }"

Yes, it's me answering my own question.

W.Donald
  • 327
6

You need to loop trough your drives;

-One line cmd;

for /f "tokens=2 delims==" %D in ('wmic volume where "Label='Win7Boot'" get DriveLetter /value') do start %D:\

-save as bat file

@echo off
set "usbName=Win7Boot"

for /f "tokens=2 delims==" %%D in ('wmic volume where "Label='%usbName%'" get DriveLetter /value') do ( start explorer %%D:
exit /b )

echo USB drive with label "%usbName%" not found. pause

-PowerShell from cmd

powershell -command "(Get-Volume | Where-Object { $_.FileSystemLabel -eq 'Win7Boot' }).DriveLetter + ':\'"
Danijel
  • 461
2
  • An alternative approach with a single loop could be:
@echo off

set "_VolumeName=BOOTDISK" && for /f usebackq^ ^Tokens^=^4^ ^Delims^=^<^> %%i in (wmic LogicalDisk where &quot;DriveType=2 and VolumeName='%%_VolumeName%%'&quot; get DeviceID^,VolumeSerialNumber /format:xml ^| find &quot;VALUE&quot;) do echo/"%%~i"| >nul find ":" && set "_DeviceID=%%~i" || set "_VolumeSerialNumber=%%~i"

set _ | findstr "_Dev _Vol""

  • Output:
_DeviceID=D:
_VolumeName=BOOTDISK
_VolumeSerialNumber=9ED49E6A

  • A different way to handle this, using a single loop, could be:
@echo off

set "_VolumeName=BOOTDISK"

%:^? if not defined _Caption =;( set "_get=Get Caption" );= else if not defined _DeviceID =;( set "_get=Get DeviceID" );= else if not defined _VolumeSerialNumber =;( set "_get=Get VolumeSerialNumber" );= else set "_get=" && goto %:^!

for /f usebackq^ ^Tokens^=^4^ ^Delims^=^<^> %%i in =;(call wmic LogicalDisk where &quot;DriveType=2 and VolumeName='%%_VolumeName%%'&quot; %%_get%% /format:xml ^| find &quot;VALUE&quot;);= do set "%get:* =%=%%~i" & goto %:^?

%:^! echo/1st for loop output: for %%G in =;( Caption,DeviceID,VolumeName,VolumeSerialNumber );= do call echo/%%~G=%%_%%~G%%

echo/ echo/2nd for loop output: for /f tokens^=^1*^ ^ delims^=^= %%i in =;( 'set _ ^| findstr "Capti Devi Volume"' );= do echo/VariableName %%~i == %%~j


This way, you’re pulling more detailed info in one go, like the DriveID, VolumeName, VolumeSerialNumber, and other useful properties. By filtering with DriveType=2 (removable drives) and checking for VolumeName='BOOTDISK', you can easily target the specific USB drive you’re interested in, getting all the key details at once.

  • Output:
1st for loop output:
Caption=D:
DeviceID=D:
VolumeName=BOOTDISK
VolumeSerialNumber=9ED49E6A

2nd for loop output: VariableName _Caption == D: VariableName _DeviceID == D: VariableName _VolumeName == BOOTDISK VariableName _VolumeSerialNumber == 9ED49E6A


  

  • "I have a USB drive which letter/path could differ, but its name never changes".
  • The volume name will always remain the same, and since this is a USB device, the drive type will consistently be 2. Therefore, I suggest using DriveType=2 and VolumeName='BOOTDISK' as conditions to get the relevant output/value.
wmic LogicalDisk where "DriveType=2 and VolumeName='BOOTDISK'" get driveID

         Numeric value that corresponds to the
     type of disk drive this logical disk represents. 

Name Value Description
Unknown 0 The type of drive is unknown.
NoRootDirectory 1 The drive does not have a root directory.
Removable 2 The drive is a removable storage device, such as a USB flash drive.
Fixed 3 The drive is a fixed disk.
Network 4 The drive is a network drive.
CDRom 5 The drive is an optical disc device, such as a CD or DVD-ROM.
Ram 6 The drive is a RAM disk.

 


Additional resources:

Io-oI
  • 9,237