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
PowershellPrinters and Scanners* USB

Avatar of undefined
Last Comment
Andrew Theodoulou

8/22/2022 - Mon
ITguy565

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

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
ITguy565

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

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
Andrew Theodoulou

ASKER
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
ITguy565

Maybe I am missing something but doesn't the $tsp.serial value have the serialnumber?
ITguy565

Post the output of the following command into the thread :

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

Open in new window

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Andrew Theodoulou

ASKER
Hello ITguy565,

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

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

Andrew Theodoulou

ASKER
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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
ITguy565

Thanks that was what I needed give me a moment..
Andrew Theodoulou

ASKER
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. :)
ITguy565

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

Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Andrew Theodoulou

ASKER
Hi ITguy565,

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

Display the contents of the object

$object
Andrew Theodoulou

ASKER
sorry, i understand what i'd missed, d'oh on my part!! It's the hostname of a device
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
Andrew Theodoulou

ASKER
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
Andrew Theodoulou

ASKER
Please find output of your latest script.
Output-of-latest-script.JPG
ITguy565

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Andrew Theodoulou

ASKER
pardon my ignorance, where do i put the hostname of the device in the brackets or after the { ?
ITguy565

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

Andrew Theodoulou

ASKER
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.
Your help has saved me hundreds of hours of internet surfing.
fblack61
ITguy565

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

Andrew Theodoulou

ASKER
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??)
Andrew Theodoulou

ASKER
or is function not needed at the front??
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
EXPERT CERTIFIED SOLUTION
ITguy565

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER CERTIFIED SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Andrew Theodoulou

ASKER
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 :)