Share via


Powershell WMI - Get USB Storage Devices Only

Question

Sunday, November 20, 2011 10:12 PM

:| having a nightmare getting "only USB storage devices" & information returned from a WMI Query.

Hoping a powershell wiz can give me a kick in the right direction

All replies (21)

Wednesday, November 23, 2011 1:47 PM ✅Answered

This works when I've got one USB drive plugged in ...

gwmi win32_volume | Where-Object {$_.DriveLetter -eq (Get-WmiObject -Query "Select * From Win32_LogicalDisk" | ? { $_.driveType -eq 2 }).DeviceID}

Should be able to expand that for multiple devices.

$Devices = @(Get-WmiObject -Query "Select * From Win32_LogicalDisk" | ? { $_.driveType -eq 2 })

ForEach ($Device in $Devices){
    gwmi win32_volume | Where-Object {$_.DriveLetter -eq ($Device.DeviceID)} | Select-Object DriveLetter,SerialNumber
}

Wednesday, November 23, 2011 2:23 PM ✅Answered | 2 votes

using that, its just combining all the data... found a scripting guys blog

from back in the day in VBS, converting it and shrinking it..

 

$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}
$letters = $diskdrive | %{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}
 
gwmi win32_volume | ? {$letters -contains ($_.name -replace "\\")} | select 
name,serialnumber

 

could probably be cleaned more, but it does it.

ps: thanks bigteddy

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, November 23, 2011 2:54 PM ✅Answered

lol, opps

 

$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Friday, November 25, 2011 1:58 PM ✅Answered

This works for me:

gwmi win32_volume | ? {$letters  -contains ($_.name -replace '\\')} | select name,serialnumber 

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Monday, November 21, 2011 11:26 AM

Hi,

You might want to check out "gwmi Win32_USBControllerDevice" .. in particual look the "dependant device" ... (gwmi Win32_USBControllerDevice |ft Dependent)

You might want to check out the powersehll forum ;o) http://social.technet.microsoft.com/Forums/en/winserverpowershell/threads

 

J


Monday, November 21, 2011 4:50 PM

This will get all 'removable disks', not necessarily all USB drives, because some external USB-powered drives are seen as 'local drives' (driveType = 3).

 

Get-WmiObject -Class win32_logicaldisk | ? { $_.driveType -eq 2 }

 

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Wednesday, November 23, 2011 12:40 AM

Hey Grant,

I Executed the following command:

Get-WmiObject -Query "Select * From Win32_LogicalDisk" | ? { $_.driveType -eq 2 }

and its almost perfect, but it only returned the following attributes.

 

  • DeviceID     :
  • DriveType     :
  • ProviderName :
  • FreeSpace     :
  • Size         :
  • VolumeName   :

I was hoping for the VolumeSerialNumber as well, 

Am I correct in presuming this also be retrieved using the LogicDisk class query and if so have you come across this problem before?

 

 


Wednesday, November 23, 2011 12:44 AM

Hi John

 

I've already tried this class, and it seems to return every USB device and I couldn't get it to grep USB Storage devices only.

 

PS.. couldn't find the thread you was talking about


Wednesday, November 23, 2011 5:39 AM

Hey Grant,

I Executed the following command:

Get-WmiObject -Query "Select * From Win32_LogicalDisk" | ? { $_.driveType -eq 2 }

and its almost perfect, but it only returned the following attributes.

 

  • DeviceID     :
  • DriveType     :
  • ProviderName :
  • FreeSpace     :
  • Size         :
  • VolumeName   :

I was hoping for the VolumeSerialNumber as well, 

Am I correct in presuming this also be retrieved using the LogicDisk class query and if so have you come across this problem before?

 

 

By default, only a few properties are returned. Pipe it to Format-List * to see all the properties.

So that would be:

Get-WmiObject -Query "Select * From Win32_LogicalDisk" | ? { $_.driveType -eq 2 } | Format-List *

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Wednesday, November 23, 2011 1:14 PM

a logical disk doesn’t have a serial number, its logical... physical disks

have serial numbers, if you check win32_diskdrive you'll see it has a serial

number field, but when I checked none of my USB devices reported a serial

number and my primary drive is an array and has "array" populated in the

serial field.

 

if you want to get adventurous you can try this

 

$list = gwmi -list  | ?{$_.name -notlike "win32_perf*"}

..wait....

$list | %{ if(($_.properties | %{$_.name}) -contains "serialnumber")

{write-host "class: $($_.name)"}}

 

this part was way faster than I thought...

 

class: CIM_PhysicalElement

class: CIM_PhysicalPackage

class: CIM_Card

class: Win32_BaseBoard

class: Win32_PhysicalMemoryArray

class: CIM_PhysicalFrame

class: CIM_Chassis

class: Win32_SystemEnclosure

class: CIM_Rack

class: CIM_PhysicalComponent

class: CIM_Chip

class: CIM_PhysicalMemory

class: Win32_PhysicalMemory

class: CIM_PhysicalMedia

class: Win32_PhysicalMedia

class: Win32_OnBoardDevice

class: CIM_PhysicalConnector

class: CIM_Slot

class: Win32_SystemSlot

class: Win32_PortConnector

class: CIM_PhysicalLink

class: CIM_SoftwareElement

class: CIM_BIOSElement

class: Win32_BIOS

class: Win32_SoftwareElement

class: CIM_VideoBIOSElement

class: Win32_OperatingSystem

class: Win32_DiskDrive

class: Win32_CDROMDrive

class: Win32_Volume

 

the win32_volume looked interesting..

 

gwmi win32_volume | select name,serialnumber

 

looks like we got it..

 

<<adds to stack of blog posts to be written>>

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, November 23, 2011 1:22 PM

Nice work there, Justin.  Just one thing:  The OP want all USB drives only.  Does win32_volume have a property to establish if it's a USB drive?

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Wednesday, November 23, 2011 2:02 PM

problem I had with that is that the drivetype 2 is not all of my USB

devices. I have an external drive on my USB port that doesn’t show up under

that, but if I do

 

gwmi win32_diskdrive | ? {$_.interfacetype -eq 'USB'}

 

it will show up

 

problem is, going from Disk to Volume is tricky... working on it now..

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Wednesday, November 23, 2011 2:12 PM | 1 vote

Have you looked at

Get-WmiObject -Query "Select * From Win32_LogicalDiskToPartition"

?


Wednesday, November 23, 2011 2:38 PM

Yeha I see what you mean .... nice :o)


Wednesday, November 23, 2011 2:52 PM

using that, its just combining all the data... found a scripting guys blog

from back in the day in VBS, converting it and shrinking it..

 

$letters = $diskdrive | %{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}

 

gwmi win32_volume | ? {"$letters\ -contains $_.name} | select

name,serialnumber

 

could probably be cleaned more, but it does it.

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

What's $diskdrive?([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Wednesday, November 23, 2011 3:06 PM

lol, opps

 

$diskdrive = gwmi win32_diskdrive | ?{$_.interfacetype -eq "USB"}

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

Tried it, but where it should have given me a serial number, I got nothing.([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Wednesday, November 23, 2011 4:11 PM

well that I cant help you with, you'll have to blame the vendor :)

 

works for my external drive (havent tried a thumb drive)

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Friday, November 25, 2011 1:47 PM

That is some good WMI work there, Justin!  It works properly on my 2 USB drives, even though one is seen as fixed disk, and one is seen as removable.  Full marks there!

For some reason the last line doesn't work for me.  $letters is correctly populated, and it should work, but returns blank, as if the where-object clause found nothing.  Strange.

([string](0..9|%{[char][int](32+("39826578846355658268").substring(($_*2),2))})).replace(' ','')


Friday, November 25, 2011 2:13 PM

wow, what on earth was I thinking...

 

I swear that worked, but I don’t see how it could.. figured it out, only

have one valid drive to check, no array, the "$letters\ works cuz it’s a

string...

 

I'll update the code post to reflect your update.

 

thanks!

 

 

Justin Rich
http://jrich523.wordpress.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.


Thursday, May 24, 2012 5:54 AM

I'm getting something like this, when I try to get the serial number of me pendrive.. Any way to convert it to readable format...


Thursday, May 24, 2012 5:55 AM

I'm getting something like this, when I try to get the serial number of me pendrive.. Any way to convert it to readable format...