Warm tip: This article is reproduced from stackoverflow.com, please click
powershell scripting device instanceid

How to use "get-PnPdevice" to get InstanceID of a device

发布于 2020-04-11 11:48:23

I want to get the instanceID of a device just as a string and assign it to a variable

I only know the device name, so when I do:

Get-PnpDevice -FriendlyName 'TSP100'

It displays:

Status     Class           FriendlyName       InstanceId                                                              
------     -----           ------------      ------------                                                               
OK         PrintQueue      TSP100            SWD\PRIN...                                                               

so ideally it would look something like this:

$env:tsp100id = (Get-PnpDevice -FriendlyName 'TSP100' *some stuff*)
Questioner
CYber GeNik
Viewed
84
postanote 2020-02-02 13:17

Why not just ask for the property, like this...

# Assign the first instanceId of the target device to a variable
$env:tsp100id = Get-PnpDevice -FriendlyName 'Generic USB Hub' | 
Select-Object -Property InstanceId | 
Select-Object -First 1
$env:tsp100id

# Results
<#
@{InstanceId=USB\VID_05E3&PID_0610\8&26FFBCBB&0&1}
#>

# Assign and output to the screen
($env:tsp100id = (Get-PnpDevice -FriendlyName 'Generic USB Hub').InstanceId[0])

# Results
<#
USB\VID_05E3&PID_0610\8&26FFBCBB&0&1
#>

Also, just curious. Why are you assigning this as an environment entry?

As for ...

Also how would I go about removing the USB\VID_05E3&PID_0610\ and just getting the 8&26FFBCBB&0&1

The simplest way in this case is just split on the backslash. For example:

(($env:tsp100id = (Get-PnpDevice -FriendlyName 'Generic USB Hub').InstanceId[0]) -split '\\')[-1]
# Results
<#
8&26FFBCBB&0&1
#>

This just says split on the backslash and take action on the last one first.