21

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?

Nic Szerman
  • 1,834
  • 18
  • 25
errata
  • 5,695
  • 10
  • 54
  • 99
  • I'm assuming with Windows 10 security 'improvements' you would need to `Run as administrator` `devcon.exe`, not just be a member of the Administrators group. Additionally you'd need to ensure that `devcon.exe` is either along side the batch file, `"%~dp0devcon.exe"`, in `%PATH%`, `devcon` or be invoked using its full path, `"C:\Tools\devcon.exe"`. – Compo Nov 28 '17 at 11:53
  • Ah yes, `devcon.exe` is in the same folder as the batch script... Can you just clarify how to run `devcon.exe` as an admin in my case? – errata Nov 28 '17 at 12:05
  • Right click on the batch file and select `Run as administrator` from the context menu. – Compo Nov 28 '17 at 14:21
  • Shouldn't this be run "as an administrator" by default if my user IS administrator? How can I also tell Windows to run it "as an administrator" after rebooting?? If I run batch file "as an administrator", is `devcon.exe` going to be run "as an administrator" also??? Sorry for all those questions, but I don't quite understand this amazing Windows option :/ – errata Nov 28 '17 at 14:29
  • Since Windows Vista and its introduction of User Account Control it should generally be read as `Run as the user with the account name Administrator` not `Run as any user who holds membership of the Administrators group`. – Compo Nov 28 '17 at 15:14
  • Ok, so, apparently **the only way** to run this `as the user with the account name Administrator` is to right-click and choose that option from the context menu? Is that right? Meaning that there is no way to automate this process after each reboot?? – errata Nov 28 '17 at 15:16
  • I didn't say it was "the only way", there are other ways of running as Administrator. You can search for self-elevating batch files, which usually use a `powershell` or `wsh` helper function. Additionally you can use Task Scheduler and choose the appropriate triggers and account information. – Compo Nov 28 '17 at 15:52
  • Alright... Well, if you could turn your comments into an answer I will accept it :) – errata Nov 28 '17 at 16:02
  • 2
    pnputil /disable-device "" pnputil /enable-device "" pnputil /restart-device "" – Nic Szerman Mar 27 '21 at 14:26
  • Both `devcon` and `pnputil` were requiring a reboot for my device, which was a no-go for me. The only program that allowed me to disable/enable my device via command line without reboot was [DevManView](http://www.nirsoft.net/utils/device_manager_view.html)! See [this answer on SE](https://superuser.com/a/560454/327009). Life saver! – alelom Jan 29 '23 at 14:56

3 Answers3

28

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"
alelom
  • 2,130
  • 3
  • 26
  • 38
Cris
  • 281
  • 3
  • 2
  • and how do you *disable/enable* a device with pnputil? – Mark Sowul Oct 03 '20 at 14:43
  • 2
    The windows 10 version of PnPUtil (at least) includes enable/disable: https://learn.microsoft.com/de-de/windows-hardware/drivers/devtest/pnputil-command-syntax – SpaceDog Nov 06 '20 at 02:03
  • 3
    https://learn.microsoft.com/de-de/windows-hardware/drivers/devtest/pnputil-examples device instance ID: Device Manager - Double Click Item - Details - Device instance path – lovestackh343 Dec 04 '20 at 05:51
  • 1
    @MarkSowul: `pnputil /disable-device `. `/enable-device` and `restart-device` is also available. All commands with `pnputil /?`. – nicolaus-hee Nov 19 '21 at 14:17
  • this has been included since Vista, however the Windows 7 version of the utility has different usage and doesn't offer this functionality: https://artemis.wszib.edu.pl/~mdudek/win7/index.html?pnputil.html – parashep Jun 21 '22 at 23:24
  • Both `devcon` and `pnputil` were requiring a reboot for my device, which was a no-go for me. The only program that allowed me to disable/enable my device via command line without reboot was [DevManView](http://www.nirsoft.net/utils/device_manager_view.html)! See [this answer on SE](https://superuser.com/a/560454/327009). Life saver! – alelom Jan 29 '23 at 14:55
  • I dislike that this is the top answer. In no way, shape, or form did it answer OP's question. "Install a 3rd party program" is not a substitute for "This is the correct command to use in cmd/powershell/etc to do this..." – GroggyOtter Apr 25 '23 at 23:06
16

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

Community
  • 1
  • 1
Dominykas Mostauskis
  • 7,797
  • 3
  • 48
  • 67
  • When I use `devcon` to disable my device, I get `Disabled on reboot: Not all of 1 device(s) disabled, at least one requires reboot to complete the operation`. Am I out of luck, is there any other way to disable/enable the device without rebooting? – alelom Jan 27 '23 at 21:52
  • 1
    Both `devcon` and `pnputil` were requiring a reboot for my device, which was a no-go for me. The only program that allowed me to disable/enable my device via command line without reboot was [DevManView](http://www.nirsoft.net/utils/device_manager_view.html)! See [this answer on SE](https://superuser.com/a/560454/327009). Life saver! – alelom Jan 29 '23 at 14:55
8

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

Compo
  • 36,585
  • 5
  • 27
  • 39