Enabling/disabling a device in Windows 10 from command line

75.9k views Asked by At

I have a specific piece of hardware which I'd like to disable and re-enable each time my Windows restarts. I created a batch script which is supposed to do that, along with running my program afterwards:

cd %~dp0
devcon.exe disable "PCI\VEN_1002&DEV_687F"
timeout /t 3
devcon.exe enable "PCI\VEN_1002&DEV_687F"

runMyWindows.exe --totally-not-virus

I am not sure if devcon.exe is a proper application for this in the first place because I have no experience with writing Windows scripts at all.

However, I have noticed that those commands don't quite do the job because my runMyWindows.exe program doesn't work as it should until I go to Windows Device Manager and manually disable and re-enable this device.

I have only 1 user on this machine which is in "Administrator" group and I am not running this script in any special way except double-clicking the .bat file, or in case of the restart, it is run from the startup folder (C:\Users\oxxo\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup).

Is there a way to do this properly within my batch script which should be run automatically on Windows startup?

3

There are 3 answers

0
Compo On BEST ANSWER

Due to security 'improvements' in Windows 10 and certainly since Windows Vista and the introduction of User Account Control I assume you would need to Run as administrator, not just be a member of the Administrators group.

It should generally be read that Run as administrator means Run as the user with the account name Administrator not Run as any user who holds membership of the Administrators group.

To Run as administrator, right click on the batch file and select Run as administrator from the context menu.

There are other ways of running as Administrator too.

  • You can use a self-elevating batch file, which usually uses a PowerShell or WSH helper function.
  • You can use Task Scheduler and choose the appropriate triggers and account information, (possibly using the SYSTEM account).

Additionally you need to ensure that DevCon.exe is either:

  • Along side the batch file, "%~dp0DevCon.exe" Disable "PCI\VEN_1002&DEV_687F*"
  • At a location defined within %PATH%, DevCon Disable "PCI\VEN_1002&DEV_687F*"
  • Invoked using its full path, "C:\Tools\DevCon.exe" Disable "PCI\VEN_1002&DEV_687F*"

In all cases above please note the asterisk which is missing from your examples

7
Cris On

PnPUtil do this job also and no SDK or anything else related required to download. Included in Windows since Vista:

https://learn.microsoft.com/en-us/windows-hardware/drivers/devtest/pnputil

Examples

Disables device specified by device instance ID:

pnputil /disable-device "USB\VID_045E&PID_00DB\6&870CE29&0&1"

Enables device specified by device instance ID:

pnputil /enable-device "USB\VID_045E&PID_00DB\6&870CE29&0&1"
2
Dominykas Mostauskis On

Most people who'll be reading this thread won't find the other answer very useful, because it's mostly about how to run the script in the question with administrator privileges. I'll attempt to answer the implicit questions here:

Enable/disable a device via the command line

I found it easiest to use devcon.exe (6mb), like in the question:

set HARDWARE_ID="PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61"
devcon disable %HARDWARE_ID%
timeout /t 3
devcon enable %HARDWARE_ID%

devcon.exe requires administrator privileges.

Where to get devcon?

It's part of the Windows driver development toolkit. Unfortunately, the official resources ask you to download a 1gb SDK. I was able to get around that by following one of the answers here: https://superuser.com/questions/1002950/quick-method-to-install-devcon-exe

Once you have it, make sure devcon.exe is on your %PATH%. I put mine in C:\Windows\System32\.

Find the hardware ID of the device you want to manipulate

Open a Command Prompt with administrator privileges and do devcon hwids *, which will print all the devices and their corresponding IDs. That will produce a lot of output. Use Command Prompts search function to find what you need. Here's the section I was interested in:

PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61\4&6AB551C&0&00E1
    Name: Intel(R) Wireless WiFi Link 4965AGN
    Hardware IDs:
        PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61
        PCI\VEN_8086&DEV_4229&SUBSYS_11018086
        PCI\VEN_8086&DEV_4229&CC_028000
        PCI\VEN_8086&DEV_4229&CC_0280
    Compatible IDs:
        PCI\VEN_8086&DEV_4229&REV_61
        PCI\VEN_8086&DEV_4229
        PCI\VEN_8086&CC_028000
        PCI\VEN_8086&CC_0280
        PCI\VEN_8086
        PCI\CC_028000
        PCI\CC_0280

Pick a specific enough ID and check if it works by doing:

devcon find "PCI\VEN_8086&DEV_4229&SUBSYS_11018086&REV_61"

If that finds only 1 device, and it's the one you want, you're good. Notice that often you'll want to escape the hardware ID with quotes.

Bonus: running a .bat script at startup or power on

In my case, I also needed to run this script when computer has booted after shutdown or sleep. I gave the above script sensible permissions and used Task Scheduler to run it on login and on startup, in its terminology: https://www.sevenforums.com/tutorials/67503-task-create-run-program-startup-log.html?ltr=T