Link to home
Start Free TrialLog in
Avatar of Andrew Theodoulou
Andrew Theodoulou

asked on

Powershell Query to show serial number for USB Attached Devices

I require assistance on a 'powershell' query to obtain the serial number of a USB attached device.

I have used a freeware program, developed by 'Nirsoft', named 'USBDeview'.

I've used the following syntax

gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Sort Manufacturer,Description,DeviceID | Ft -GroupBy Manufacturer Description,Service,DeviceID

however i'm having difficulty to get the serial number being included and displayed alongside this query.
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Right, i've done some further investigating.

I wish to combine the following two properties into a PowerShell Query.

The USB Printing Support is the (parent) property, and the 'Star TSP100 Cutter' is the child.

If anybody can shed light in how to link these two string values in a powershell query, then this would be much appreciated.

I have attached a picture as to which device i'm trying to get in showing it's serial number,

Any input is greatly appreciated, as i've exhausted in researching articles online.
USBDeview-SerialNumber.JPG
Parent---Child-Properties.JPG
Avatar of ITguy565
ITguy565
Flag of United States of America image

You could try something like this: * I unfortunately can't test this right now*

gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Where-Object {($_.Description -like '*TSP*')} | Sort Description,DeviceID | ft Description,DeviceID –auto

Open in new window

Avatar of Andrew Theodoulou
Andrew Theodoulou

ASKER

Hello ITguy565,

That's near to exactly as to what i wish to achieve.

However i wish to get the serial number of the item. Which is as shown in the 'Parent-Child' photo, under 'USB Printing Support Properties> Value> USB\VID_0519&PID_0003\2550617040600699 (this is the serial number.)

Where or what would need to be added to your possible solution to get this displayed?

The property value is 'Device instance path'.

I've tried a few combinations, but can't work it out still :(
Tested-Query---No-Serial-Number-show.JPG
Try this :

$tsp = gwmi Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {($_.Description -like '*TSP*')} | Sort Description, DeviceID | Select Description, DeviceID
$tsp |Add-Member -MemberType NoteProperty -Name Serial -value $null
$tsp.serial = $tsp.deviceID.split("\")|select -index 2

$tsp

Open in new window

Hello ITguy565

Please find screenshot of new query, sadly it's still not showing the serial number.

Like yourself, i've tried 'piping' different instances, but can't work out how to interlink the the USB Printing Properties with the actual printer properties to show the serial number. As the serial number resides on the printer, but under 'Last Known Parent'. I hope that gives you as much input as possible?
Result-of-new-query.JPG
-Last-Know-Parent--Value-of-Child-Ob.JPG
Maybe I am missing something but doesn't the $tsp.serial value have the serialnumber?
Post the output of the following command into the thread :

gwmi Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {($_.Description -like '*tsp*')}

Open in new window

Hello ITguy565,

Please find output of properties 'screenshot' attached for your perusal.
Output-of-following-command-as-reque.JPG
Thanks,

Please also give me a screen capture of the following :

gwmi Win32_USBControllerDevice |%{[wmi]($_.Dependent)} | Where-Object {($_.Description -like "**")} | Sort Description,DeviceID | Select Description,DeviceID

Open in new window

Hello ITguy565,

Please find additional screenshot showing output of printer that shows the serial number and is under 'USB Printing Support'.

I hope that this gives you a more better insight to the values?
USB-Printing-Support-Output.JPG
Thanks that was what I needed give me a moment..
Okay, thanks!

I'll shall not post an output of your previous response as requested.

I hope that it's more clearer in trying to interlink two values one from the parent (USB Printing Support) and the other being the child (StarTSP100 Cutter)

I kindly await your next response. :)
Try this and tell me what your output looks like, I believe this will give you what you are looking for:


Sorry for the confusion, doing 100 things at once this morning.

$object = ""|Select Description, DeviceID, SerialNumber
$WMIInfo = gwmi Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {(($_.Description -like "*TSP*") -or (($_.Description -like "*USB Printing Support*"))) }

$object.Description = ($wmiinfo |? {$_.description -like "*TSP*"}).Description
$object.DeviceID = ($wmiinfo |? {$_.description -like "*TSP*"}).deviceID
$object.SerialNumber = (($wmiinfo |? {$_.Description -like "*USB Printing Support*"}).deviceID).split("\")|select -index 2
$object

Open in new window

Hi ITguy565,

I appreciate your input, unfortunately, this didn't work :(
Output-of-latest-script.JPG
Display the contents of the object

$object
sorry, i understand what i'd missed, d'oh on my part!! It's the hostname of a device
Invoke-Command -ComputerName c9093tm-9610001 -ScriptBlock{
((Get-PnpDevice -FriendlyName "*star*" -Status "ok" -Class "printer"| Get-PnpDeviceProperty |  Where-Object {$_.keyna
me -like "*parent*"}).data).split("\")[2]
}

this is what i'm currently using
Please find output of your latest script.
Output-of-latest-script.JPG
Created a function for you to run :

function get-SerialFromPrinter($computer){

$object = ""|Select Description, DeviceID, SerialNumber
$WMIInfo = gwmi -computername $computer Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {(($_.Description -like "*TSP*") -or (($_.Description -like "*USB Printing Support*"))) }

$object.Description = ($wmiinfo |? {$_.description -like "*TSP*"}).Description
$object.DeviceID = ($wmiinfo |? {$_.description -like "*TSP*"}).deviceID
$object.SerialNumber = (($wmiinfo |? {$_.Description -like "*USB Printing Support*"}).deviceID).split("\")|select -index 2
$object
}

Open in new window


. get-SerialFromPrinter -computer yourcomputername
pardon my ignorance, where do i put the hostname of the device in the brackets or after the { ?
Copy and paste the following into your PowerShell window:

function get-SerialFromPrinter($computer){

$object = ""|Select Description, DeviceID, SerialNumber
$WMIInfo = gwmi -computername $computer Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {(($_.Description -like "*TSP*") -or (($_.Description -like "*USB Printing Support*"))) }

$object.Description = ($wmiinfo |? {$_.description -like "*TSP*"}).Description
$object.DeviceID = ($wmiinfo |? {$_.description -like "*TSP*"}).deviceID
$object.SerialNumber = (($wmiinfo |? {$_.Description -like "*USB Printing Support*"}).deviceID).split("\")|select -index 2
$object
}

Open in new window



then on the next line run :

. get-SerialFromPrinter -computer yourcomputername 

Open in new window

replacing yourcomputername with your local machine name like hostname123


example :

 . get-SerialFromPrinter -computer hostname123

Open in new window

function get-SerialFromPrinter (c9093tm-9610001){

$object = ""|Select Description, DeviceID, SerialNumber
$WMIInfo = gwmi -computername $computer Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {(($_.Description -like "*TSP*") -or (($_.Description -like "*USB Printing Support*"))) }

$object.Description = ($wmiinfo |? {$_.description -like "*TSP*"}).Description
$object.DeviceID = ($wmiinfo |? {$_.description -like "*TSP*"}).deviceID
$object.SerialNumber = (($wmiinfo |? {$_.Description -like "*USB Printing Support*"}).deviceID).split("\")|select -index 2
$object
}

Sorry, still not clear, i've put in a hostname where you'd possibly mentioned. if incorrect, please show example :(

Thank you for your patience and co-operation on this by the way.
The code that I Have below creates a commandline "Reusable" Function take it and paste I into a powershell window.

function get-SerialFromPrinter($computer){

$object = ""|Select Description, DeviceID, SerialNumber
$WMIInfo = gwmi -computername $computer Win32_USBControllerDevice | % {[wmi]($_.Dependent)} | Where-Object {(($_.Description -like "*TSP*") -or (($_.Description -like "*USB Printing Support*"))) }

$object.Description = ($wmiinfo |? {$_.description -like "*TSP*"}).Description
$object.DeviceID = ($wmiinfo |? {$_.description -like "*TSP*"}).deviceID
$object.SerialNumber = (($wmiinfo |? {$_.Description -like "*USB Printing Support*"}).deviceID).split("\")|select -index 2
$object
}

Open in new window


in order to run this code you need to run the following command :


. get-SerialFromPrinter -computer hostname123

Open in new window


Change Hostname 123 to your computername

in yourcase:


. get-SerialFromPrinter -computer c9093tm-9610001

Open in new window

Sorry, i'm being ignorant and having a brainfart.

Could you please paste your whole script in here as to where the hostname i gave as an example goes specifically??

as

function get-SerialFromPrinter($computer){      (where does hostname go into this command line exactly??)
or is function not needed at the front??
EXPERT CERTIFIED SOLUTION
Avatar of ITguy565
ITguy565
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Mr. ITguy565,

Many thanks for your time, effort, collaboration and input in regards to this!!!!

If you were local, i'll gladly get you a beer :)