Skip to main content

How to Disable or Enable a Device in Batch Script

Automating the management of hardware devices is a powerful administrative capability. You might need to disable a USB port for security, "bounce" a problematic network card by disabling and re-enabling it, or enable a piece of hardware as part of a setup script. While there is no simple, built-in batch command for this, Microsoft provides a dedicated, stand-alone command-line tool for this purpose: DevCon.exe.

This guide will explain what DevCon is, where to get it, and how to use it from a batch script to programmatically enable and disable hardware devices. You will learn how to find the critical Hardware ID of a device, which is essential for targeting your commands correctly.

What is DevCon? (and where to get it)

The Device Console Utility (DevCon.exe) is the command-line equivalent of the graphical Device Manager. It allows you to enable, disable, restart, update, and query hardware devices on the local machine.

DevCon is not included with Windows by default. You must download it from Microsoft.

  • It is included as part of the Windows Driver Kit (WDK).
  • You can find the WDK by searching for "Download the Windows Driver Kit" on the official Microsoft website.
  • Once installed, you can find devcon.exe in the tools subdirectory (e.g., C:\Program Files (x86)\Windows Kits\10\Tools\x64\devcon.exe).
  • For your script to find it, devcon.exe must be in your system's PATH or in the same directory as your batch script.

CRITICAL: Finding a Device's Hardware ID

Before you can control a device, you must have a way to uniquely identify it. A device's name (like "Intel(R) Wireless-AC 9560") can be long, change with driver updates, and is difficult to use in a script. The most reliable way to target a device is by its Hardware ID.

How to Find the Hardware ID

  1. Open Device Manager.
  2. Find the device you want to control (e.g., your Wi-Fi adapter under "Network adapters").
  3. Right-click the device and go to Properties.
  4. Go to the Details tab.
  5. From the "Property" dropdown, select Hardware Ids.

The list of values that appears contains the unique identifiers for the device. The first one is usually the most specific. For example, a Wi-Fi card might have an ID like PCI\VEN_8086&DEV_A370.

The Core Commands: DevCon Enable and DevCon Disable

The syntax for enabling and disabling devices is straightforward. You must provide the Hardware ID, enclosed in quotes, and preceded by an @ symbol.

To Disable a Device: devcon disable "@HardwareID"

To Enable a Device: devcon enable "@HardwareID"

These commands must be run with administrator privileges.

Basic Example: A Simple Enable/Disable Script

This script will disable, and then re-enable, a specific USB Root Hub, which can be useful for troubleshooting USB port issues.

@ECHO OFF
REM This script MUST be run as an Administrator.

REM The Hardware ID for a common Intel USB 3.0 controller.
SET "DeviceID=PCI\VEN_8086&DEV_A36D"

ECHO --- Disabling and Re-enabling a USB Hub ---
ECHO.
ECHO Target Hardware ID: %DeviceID%
PAUSE

ECHO Step 1: Disabling the device...
devcon disable "@%DeviceID%"

ECHO.
ECHO Step 2: Re-enabling the device...
devcon enable "@%DeviceID%"

ECHO.
ECHO --- Operation complete ---

How the devcon Command Works

The DevCon.exe utility is a direct interface to the Windows Plug and Play manager. When you run devcon disable, it programmatically sends a signal to the operating system to disable the device driver associated with that hardware, effectively turning the device off. devcon enable reverses this process. This is the exact same action that occurs when you right-click a device in the Device Manager and choose "Disable device."

Common Pitfalls and How to Solve Them

Problem: 'devcon' is not recognized...

This means devcon.exe is not in your system's PATH or in the same folder as your script. Solution: Download and install the Windows Driver Kit (WDK) as described before and ensure devcon.exe is accessible.

Problem: "Access is denied." (Administrator Privileges)

Disabling a hardware device is a highly privileged operation.

Example of error message:

Disabling device failed.

Solution: The script must be run from an elevated command prompt. Right-click your .bat file or cmd.exe and select "Run as administrator."

Problem: Targeting the Wrong Device

If you use a partial or incorrect Hardware ID, you might accidentally disable the wrong piece of hardware (like your primary hard drive controller, which could crash the system).

Solution: Be extremely careful and precise with your Hardware ID. Always copy it directly from the Device Manager. You can use wildcards (*) with the ID, but this should be done with caution. devcon disable "@PCI\VEN_8086*" (This would disable all Intel PCI devices, which could be dangerous).

Practical Example: A "Network Adapter Reset" Script

This is a more robust alternative to the netsh method for resetting a network card, as it reinitializes the hardware at a lower level.

@ECHO OFF
SETLOCAL
TITLE Network Adapter Hardware Reset
REM This script must be run as an Administrator.

REM --- Find this on your machine in Device Manager ---
SET "NetAdapterID=PCI\VEN_8086&DEV_A370"

ECHO --- Network Adapter Hardware Reset ---
ECHO This will disable and re-enable the network adapter.
ECHO Your network connection will be lost for a moment.
ECHO.
PAUSE

ECHO Step 1: Disabling the adapter...
devcon disable "@%NetAdapterID%"

ECHO.
ECHO Waiting for 5 seconds...
TIMEOUT /T 5 /NOBREAK > NUL

ECHO.
ECHO Step 2: Re-enabling the adapter...
devcon enable "@%NetAdapterID%"

ECHO.
ECHO --- Reset complete ---
ENDLOCAL

Conclusion

The DevCon.exe utility is the definitive tool for scripting hardware device management. It gives your batch scripts the power of the Device Manager.

Key takeaways for using it successfully:

  • DevCon is not included with Windows; you must download it as part of the WDK.
  • You must run your script as an Administrator.
  • The most reliable way to target a device is by using its unique Hardware ID.
  • Use devcon disable "@HardwareID" to disable and devcon enable "@HardwareID" to enable.