Powershell – Get the drive letter of USB drive in PowerShell

powershellusbwmi

I've seen articles in C# and some other languages that explain how to achieve what I'm looking for but I don't know how to convert them.

Best Answer

Try:

gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"} | %{gwmi -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($_.DeviceID.replace('\','\\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition"} |  %{gwmi -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition"} | %{$_.deviceid}

Tested with one and more than one USB device plugged-in.