Note: WMI can be unreliable. If you need it to work in a great variety of different systems/OS's/conditions, I would avoid using WMI!
Save this as USB.vbs and run it by typing CSCRIPT USB.VBS.
The first bit of code lists all USB drives.
The second bit lists the USB drives and some details
The third segment looks for the drive letters of any 'KINGSTON' USB drives
The fourth segment lists the drive letter associated with each USB drive
The output will be similar to:
USB Device: Kingston DataTraveler G2 USB Device
USB Device: Alcor Flash Disk USB Device
---------------
Device 1:Kingston DataTraveler G2 USB Device FWARE:PMAP IFACE_TYPE:USB MEDIA_TYPE:Removable Media SIZE:4013936640
Device 2:Alcor Flash Disk USB Device FWARE:8.07 IFACE_TYPE:USB MEDIA_TYPE:Removable Media SIZE:518192640
---------------
USB Drive(s) mounted at J:
---------------
Alcor Flash Disk USB Device Disk #2, Partition #0 = E:
Kingston DataTraveler G2 USB Device Disk #1, Partition #0 = J:
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDevices = objWMIService.ExecQuery ("Select * From Win32_USBControllerDevice")
For Each objDevice in colDevices
strDeviceName = objDevice.Dependent
'msgbox strDeviceName
strQuotes = Chr(34)
strDeviceName = Replace(strDeviceName, strQuotes, "")
arrDeviceNames = Split(strDeviceName, "=")
strDeviceName = arrDeviceNames(1)
Set colUSBDevices = objWMIService.ExecQuery ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
For Each objUSBDevice in colUSBDevices
y = objUSBDevice.Caption
if instr(1,y,"USB Device") then x = x & "USB Device: " & y & vbcrlf
Next
Next
Wscript.echo x
Wscript.echo "---------------"
'Get USB details for DISKS
y=""
for i = 0 to 10
DiskIndex=i
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
' WMI Query to the Win32_OperatingSystem
x = "\\\\.\\PHYSICALDRIVE" & DiskIndex 'for a query we must use \\ for a single \
x = "Select * from Win32_DiskDrive where InterfaceType = 'USB' AND DeviceID = '" & x & "'"
Set colItems = objWMIService.ExecQuery(x)
For Each DD In colItems
y = y & vbcrlf & "Device " & DiskIndex & ":" & DD.Model
y = y & " FWARE:" & DD.FirmwareRevision
y = y & " IFACE_TYPE:" & DD.InterfaceType 'USB
y = y & " MEDIA_TYPE:" & DD.MediaType
if not IsNull(DD.Size) then y = y & " SIZE:" & DD.size
Next
Next
Wscript.echo y
Wscript.echo "---------------"
'Find USB drives (or a specific drive model - in this case KINGSTON USB drives)
'If you want all USB drives listed, comment out with a ' the If line and the End If line
strComputer = "."
TargetPath = ""
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDiskDrives = objWMIService.ExecQuery("SELECT * FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each objDrive In colDiskDrives
If Instr(1,ucase(objDrive.Caption), "KINGSTON") > 0 Then
strDeviceID = Replace(objDrive.DeviceID, "\", "\\")
Set colPartitions = objWMIService.ExecQuery ("ASSOCIATORS OF {Win32_DiskDrive.DeviceID=""" & strDeviceID & """} WHERE AssocClass = " & "Win32_DiskDriveToDiskPartition")
For Each objPartition In colPartitions
Set colLogicalDisks = objWMIService.ExecQuery("ASSOCIATORS OF {Win32_DiskPartition.DeviceID=""" & objPartition.DeviceID & """} WHERE AssocClass = " & "Win32_LogicalDiskToPartition")
For Each objLogicalDisk In colLogicalDisks
TargetPath = TargetPath & objLogicalDisk.DeviceID & vbtab
Next
Next
End If
Next
Wscript.Echo "USB Drive(s) mounted at " & TargetPath
Wscript.echo "---------------"
'Show drive letters associated with each
ComputerName = "."
Set wmiServices = GetObject ( _
"winmgmts:{impersonationLevel=Impersonate}!//" _
& ComputerName)
' Get physical disk drive
Set wmiDiskDrives = wmiServices.ExecQuery ( "SELECT Caption, DeviceID FROM Win32_DiskDrive WHERE InterfaceType = 'USB'")
For Each wmiDiskDrive In wmiDiskDrives
' x = wmiDiskDrive.Caption & Vbtab & " " & wmiDiskDrive.DeviceID
'Use the disk drive device id to
' find associated partition
query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='" & wmiDiskDrive.DeviceID & "'} WHERE AssocClass = Win32_DiskDriveToDiskPartition"
Set wmiDiskPartitions = wmiServices.ExecQuery(query)
For Each wmiDiskPartition In wmiDiskPartitions
'Use partition device id to find logical disk
Set wmiLogicalDisks = wmiServices.ExecQuery ("ASSOCIATORS OF {Win32_DiskPartition.DeviceID='" _
& wmiDiskPartition.DeviceID & "'} WHERE AssocClass = Win32_LogicalDiskToPartition")
x = ""
For Each wmiLogicalDisk In wmiLogicalDisks
x = x & wmiDiskDrive.Caption & " " & wmiDiskPartition.DeviceID & " = " & wmiLogicalDisk.DeviceID
Wscript.echo x
Next
Next
Next
getdno.vbs
========
DL=Wscript.Arguments(0)
DL = mid(DL,1,1)
set wmi = getobject("winmgmts://./root/cimv2")
wql = "select Antecedent , Dependent from Win32_LogicalDiskToPartition"
set results = wmi.execquery(wql)
for each result in results
partition = split(result.Antecedent,"=")(1)
drive = split(result.Dependent,"=")(1)
d = split(drive,":") (0)
d = mid(d,2)
n = split(partition,"#") (1)
n = mid(n,1,1)
'if d = DL then wscript.echo "DRIVE " & DL & " = DISK " & n & " (" & partition & ")"
if d = DL then wscript.echo "SET DN=" & n
next
If you are using the vbscript from a .bat or .cmd file, then call it like this:
cscript /nologo getdno.vbs L: > tt.cmd
call tt.cmd
DN will have the drive number.