« Showing USB Devices | Home | More on printing on W… »

Detecting drive letter for an USB Stick

Some people have been waiting for an answer about this for a long time. Here is a way for Windows to find the drive letter of USB Sticks. This way you can build list which USB Device is which drive letter and with our USB classes. All you need here is the WindowsWMIMBS class. We simply show you how we get from device to drive letter by using our query example.

So first you run example and do a query like this one:
select PNPDeviceID, DeviceID, Model from Win32_DiskDrive where InterfaceType='USB'
This queries for PNPDeviceID, DeviceID and Model properties in Win32_DiskDrive array where interface is USB. This gives you a couple of USB Devices with disks:

Using the PNP Device ID and Model, you should be able to identify your stick. Also with DeviceID you get link to partition list. So we run next query to find associated partition for this device ID:
Associators of {Win32_DiskDrive.DeviceID='\\.\PHYSICALDRIVE1'} where AssocClass=Win32_DiskDriveToDiskPartition
PLease replace the device ID value in this string with the one you got above.

Now we query again with the new Device ID for the matching logical disk:
Associators of {Win32_DiskPartition.DeviceID='Disk #1, Partition #0'} where AssocClass=Win32_LogicalDiskToPartition
This gives us now the DeviceID with "E:". This is the drive letter of the pen drive I used:

And that's it. If you have multiple devices, you may need to query several times. All the code to query and get values is in the example. The biggest plugin in space...
24 02 12 - 13:47