Hal
Hal

Reputation: 7

How to get just a portion of the results from Select-Object

I have a simple question, I hope someone can help...

if I run this command;

get-pnpDevice -class display

I get the expected results in a table format. It displays the Status, Class, FriendlyName, and InstanceID.

Next, I want to get just the InstanceID, which I can do with this command;

Get-PnpDevice -Class display | Select-Object -Property instanceID

But what I am stuck on is that I just want to get the inner portion of the result, not the full result. In other words, on my system, if I run the above command, I get the following result;

PCI\VEN_8086&DEV_5912&SUBSYS_872F1043&REV_04\3&11583659&0&10
PCI\VEN_10DE&DEV_1B81&SUBSYS_85971043&REV_A1\4&35D4F288&0&0008

But what I am trying to get is the result without the PCI\ and anything after and including the ampersand, for example, I dont want to see the &REV_04\3&11583659&0&10 or &REV_A1\4&35D4F288&0&0008

Any suggestions?

I tried the suggested calculated property and it returns something close to what I am trying to do, but not completely. I don't understand the calculated property, so please bear with me.... the result is as follows;

InstanceID


VEN_8086
VEN_10DE

It seems to cut-off the results. I had a look through the Microsoft Select-Object documentation and it describes a little bit about calculated properties, but it doenst say anything about the -replace parameter, so I am a little lost about how to work with this and fix it for my use. Is there a better documentation you can point me to learn more about this?

Upvotes: 0

Views: 589

Answers (1)

AdminOfThings
AdminOfThings

Reputation: 25001

You will likely need a calculated property with Select-Object.

Get-PnpDevice -Class display |
    Select-Object @{n='InstanceID';e={$_.InstanceId -replace '^PCI\\(.*?)&REV.*$','$1'}}

You can add other properties (comma-separated) along with the calculated property.

The -replace operator uses regex matching. The example here uses a capture group (.*?), which is named capture group 1 (referenced later with $1) to capture all characters between PCI\ and the first &REV. ^ is the start of a string. $ is the end of the string. .* greedily matches zero or more characters. Since \ is a special regex character, \ is used to escape it resulting in \\.

The e or expression part of the calculated property contains a script block (code within {}). The script block contents perform a string replacement on each InstanceId value ($_.InstanceId).

Upvotes: 1

Related Questions