Share via


Disable a PnP device using the Disable() method of Win32_PNPEntity Class

Question

Monday, July 6, 2015 1:13 PM

Hi,

I am trying to detect the presence of an Yellow Bang for my Plug and Play device in the control Panel and if present, then disable and enable that device using PowerShell scripting. I am using the following command for detecting any device which is in erroneous state - 

$computer = "LocalHost"

$namespace = "root\CIMV2"

Get-WMIObject -class Win32_PNPEntity -computername $computer -namespace $namespace | Where{$_.Status -ne "OK" -and ![string]::IsNullOrEmpty($_.Status) -and $_.DeviceID.Contains('WXYZ')}

Can someone kindly suggest me how to use the Disable() and the Enable() methods of the Win32_PNPEntity class? I have tried using the following ways; but end up with the corresponding errors

Get-WMIObject -class Win32_PNPEntity -computername $computer -namespace $namespace | Where{$_.Status -ne "OK" -and ![string]::IsNullOrEmpty($_.Status) -and $_.DeviceID.Contains('WXYZ')} | Disable() - "An expression was expected after '('. "

(Get-WMIObject -class Win32_PNPEntity -computername $computer -namespace $namespace | Where{$_.Status -ne "OK" -and ![string]::IsNullOrEmpty($_.Status) -and $_.DeviceID.Contains('WXYZ')}).Disable() - "Method invocation failed because [System.Management.ManagementObject] does not contain a method named 'Disable'. "

What is the exact syntax for using the Disable() and Enable() methods? Also, is there any parameter that must be passed to these methods?

All replies (10)

Tuesday, July 7, 2015 11:19 AM ✅Answered

Hi Braham20,

I, myself had figured it out this morning. I am sorry for not sharing this information with you on time.

But, I really appreciate your help regarding this matter.

Thanks a lot!!

Swastik.


Monday, July 6, 2015 2:18 PM

What OS are you using? According to this page the method is only available on Windows 10 

Microsoft


Monday, July 6, 2015 2:37 PM

I was trying on Windows 8.1 on PowerShell 3

If it is so, then is there any solution present for Windows 8.1? Or do I need to update the PowerShell?


Monday, July 6, 2015 2:43 PM

Updating Powershell won't make any difference I'm afraid, the method isn't available in any earlier version of the OS. You will probably need to use something like devcon - 

Weekend Scripter


Monday, July 6, 2015 2:53 PM

Oh i see...

Well as per your previous reply I tried the same command i.e.

$computer = "LocalHost"

$namespace = "root\CIMV2"

Get-WMIObject -class Win32_PNPEntity -computername $computer -namespace $namespace | Where{$_.Status -ne "OK" -and ![string]::IsNullOrEmpty($_.Status) -and $_.DeviceID.Contains('WXYZ')}).Disable()

on Windows 10 Preview and it gave an error saying **-  "**Exception Calling "Disable" : Generic Failure"

I am new to using Powershell, can you kindly help me with this. How  can I call the Disable() and Enable() methods?

What parameter do I need to pass exactly?


Monday, July 6, 2015 3:00 PM

You could try $true or $false, I have no access to a Windows 10 machine at the moment so I can't do any testing myself. 

You could also try 

$f = Get-WMIObject -class Win32_PNPEntity | Where {$_.Status -ne "OK" -and ![string]::IsNullOrEmpty($_.Status) -and $_.DeviceID.Contains('WXYZ')}
Invoke-WmiMethod -name "Disable" -InputObject $f

Monday, July 6, 2015 3:14 PM

Hi Braham20,

In response to your first suggestion i.e. $true or $false I got an error as - "Cannot find an overload for 'Disable' and the argument count: '1' "

And, in response to your second suggestion I am getting the same error as above i.e. - "InvokeWmiMethod : Generic Failure"

Can you tell me the meaning of these failures? May be it might help me down the road....

As per the MSDN documents, the Enable and the Disable methods has one boolean parameter i.e. [out]. But still it is suggesting that I am  "overloading" that function...


Tuesday, July 7, 2015 10:23 AM

Ok, I just got chance to test this. You need to be running Powershell as an administrator (Right Click - Run as Administrator).


Wednesday, October 7, 2015 8:34 PM

Would you mind sharing how you did it?


Thursday, October 8, 2015 8:18 AM

You need to be running Powershell as an administrator - (Right Click - Run as Administrator).

[string](0..21 | % {([char][int](22+
("5275778524449275827587
42505554247789249585").Substring(($_*2),2)))})`
-replace " "

LinkedIn:   

Note: Posts are provided “AS IS” without warranty of any kind, either expressed or implied, including but not limited to the implied warranties of merchantability and/or fitness for a particular purpose.