I've done this with WMI: using the device type to get to the drive letter. in simplified form (real script has logging and error handling). I initially obtained $deviceCaption
from Win32_PnpEntity
and Device Manager:
$objs = @(Get-WmiObject -Query "select Caption,__RELPATH from Win32_PnpEntity where caption=""$deviceCaption""")
if ($objs.Length -eq 0) {
throw "MP3 Player is not connected"
} elseif ($objs.Length -gt 1) {
throw "Seem to be multiple MP3 players connected"
}
$relPath = $objs[0];
$objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_DiskDrive")
$relPath = $objs[0].__RelPath;
$objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_DiskPartition")
$relPath = $objs[0].__RelPath;
$objs = @(Get-WmiObject -Query "ASSOCIATORS OF {$relPath} where resultclass=Win32_LogicalDisk")
$relPath = $objs[0].__RelPath;
Write-Debug "RelPath #4: $($objs[0].__RelPath), drive: $($objs[0].DeviceID)"
$objs[0].DeviceID
That final expression returns the drive name, something like: Q:
(it does include to colon).
Note this assumes the device has a single disk with a single partition.