I’m trying to query disk information on Windows using PowerShell. I need to return Drive Letter, friendly name and PNPDeviceID. I’ve written the following but in some scenarios the wrong PNPDeviceID is returned for SD cards, it seems Win32_DiskDrive finds the generic storage device rather than the accutal device ID of the SD Card. Is there a way for me to get all the information I need without using Win32_DiskDrive
$USBDevices = @()
$Win32DiskDrives = Get-CimInstance win32_diskdrive | Where { $_.MediaType -ne 'Fixed hard disk media' -and $null -ne $_.Size }
ForEach ($Win32DiskDrive in $Win32DiskDrives ) {
$DriveLetter = Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskDrive.DeviceID=`"$($Win32DiskDrive.DeviceID.replace('','\'))`"} WHERE AssocClass = Win32_DiskDriveToDiskPartition" |`
ForEach-Object { Get-WmiObject -Query "ASSOCIATORS OF {Win32_DiskPartition.DeviceID=`"$($_.DeviceID)`"} WHERE AssocClass = Win32_LogicalDiskToPartition" } | ForEach-Object { $_.deviceid }
If ($null -ne $DriveLetter -and $DriveLetter -ne $env:SystemDrive) {
$USBDevices += [PSCustomObject] [Ordered] @{
DriveLetter = $DriveLetter
PNPDeviceID = $($Win32DiskDrive.PNPDeviceID).replace('&', '&&')
Caption = $Win32DiskDrive.Caption
}
}
}