Home > Back-end >  Get-WmiObject not showing desired results while filtering for PNPDeviceID
Get-WmiObject not showing desired results while filtering for PNPDeviceID

Time:03-22

I have been trying to get a script working that identifies whether the graphics card currently installed in a Windows 10 system is an Nvidia card or not via the hardwareID. But for whatever reason I can't get it to grab only the PNPDeviceID line that I want. I have setup 2 different test scripts in an attempt to get this working:

$VideoCardID = Get-WmiObject -ClassName Win32_VideoController | Where-Object { $_.PNPDeviceID -like "PCI\VEN_10DE*"}
Write-Host $VideoCardID
Write-Host "Press any key to exit..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit

Which outputs: \\DESKTOP-UPTIVUP\root\cimv2:Win32_VideoController.DeviceID="VideoController1" Press any key to exit...

And the other one:

$VideoCardID = Get-WmiObject -Class CIM_VideoController PNPDeviceID | Where-Object { $_."PCI\VEN_10DE*"}
Write-Host $VideoCardID
Write-Host "Press any key to exit..."
$host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
exit

Which just outputs Press any key to exit...

The desired result would be that it only detects the hardwareID (aka the PNPDeviceID) of the grafics card an then through and if/else statement figures out if its an Nvidia model or not.

I have been trying for some days now to figure out what's going wrong, but at this point I have given up, hence I am here to ask for advice. This might be quite simple to solve for people with alot of experince with Powershell, although I'm still a script kiddie at best.

CodePudding user response:

For your first snippet, you just need to use dot notation to reference the PNPDeviceID property of the object $VideoCardID:

Write-Host $VideoCardID.PNPDeviceID

The reason why you see the _PATH property is because Write-Host is stringifying your object, something similar to what would happen if we call the .ToString() method on this object:

PS /> $VideoCardID.ToString()
\\computername\root\cimv2:Win32_VideoController.DeviceID="VideoController1"

On your second snippet, your Where-Object statement is incorrect:

Where-Object { $_."PCI\VEN_10DE*"}

Should be the same as your first example:

Where-Object { $_.PNPDeviceID -like "PCI\VEN_10DE*"}

As for how you can approach the code with an if / else statement:

if((Get-CimInstance -Class CIM_VideoController).PNPDeviceID -like "PCI\VEN_10DE*") {
    "It's NVIDIA!"
}
else {
    "It's not NVIDIA!"
}

As aside, it is recommended to use the CIM cmdlets instead of WMI.

  • Related