Powershell Query to show serial number for USB Attached Devices

Andrew Theodoulou
Andrew Theodoulou used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
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 TheodoulouEUS System Operations Engineer

Author

Commented:
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

OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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..
Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
Hi ITguy565,

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

$object
Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
sorry, i understand what i'd missed, d'oh on my part!! It's the hostname of a device
Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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 TheodoulouEUS System Operations Engineer

Author

Commented:
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
Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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

Andrew TheodoulouEUS System Operations Engineer

Author

Commented:
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 TheodoulouEUS System Operations Engineer

Author

Commented:
or is function not needed at the front??
You aren't having a brainfart or ignorant or anything else you are learning!


Open a powershell window when that window is open :

copy the code I have here :

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



Paste this code into a that open powershell window.


Once you have pasted it into the powershell window.

Copy this code and run it:

. get-SerialFromPrinter -computer c9093tm-9610001

in this example

-computer Hostname
No, when you take my code above and you paste it into the powershell window it is going to appear that it did nothing, but in reality it is going to open up to you a command called get-serialfromcomputer and you can then use the syntax above to execute that command
Were you able to get the output that you were looking for?
Andrew TheodoulouEUS System Operations Engineer

Author

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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial