Skip to main content

How to Scan for Hardware Changes in Batch Script

After installing a new piece of hardware, or when a device is not being detected correctly, the first troubleshooting step is often to open the graphical Device Manager and click "Scan for hardware changes." This action forces the Windows Plug and Play (PnP) manager to re-scan the system's hardware buses to detect any new or changed devices. For automation, especially in server environments or for scripting hardware installations, you need a way to trigger this scan from the command line.

This guide will introduce you to DevCon.exe, the official Microsoft command-line equivalent of the Device Manager. You will learn where to get this tool, how to use its rescan command, and the critical prerequisites required for it to work.

What is DevCon? (and where to get it)

The Device Console Utility (DevCon.exe) is a powerful command-line tool that allows you to perform many of the actions of the graphical Device Manager, such as enabling/disabling devices, listing hardware, and, most importantly, scanning for new hardware.

DevCon is not included with Windows by default. It is a professional tool that you must download 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.

The Core Command: devcon rescan

The rescan command is the specific function within DevCon that initiates a system-wide scan for new hardware.

Syntax: devcon rescan

This command is simple, takes no other arguments, and must be run with administrator privileges.

Basic Example: A Simple Hardware Rescan

This script executes the rescan command. It's the direct equivalent of clicking the button in the Device Manager.

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

ECHO --- Scanning for new or changed hardware ---
ECHO This may take a moment...
ECHO.

devcon rescan

ECHO.
ECHO --- Scan complete ---

In the output, DevCon will report the results of its scan.

--- Scanning for new or changed hardware ---
This may take a moment...

Scanning for new hardware.
Scanning completed.

--- Scan complete ---
note

If a new device is found, it will typically report that "1 device(s) were installed."

How the devcon rescan Command Works

The devcon rescan command is a direct interface to the Windows Plug and Play (PnP) manager. It sends a signal that is identical to the one sent by the Device Manager GUI. This causes the PnP manager to query all the system's hardware buses (like PCI Express, USB, etc.) and check for any devices that have been added or removed since the last scan. If it finds a new device, it will attempt to identify it and install the appropriate driver.

Common Pitfalls and How to Solve Them

Problem: 'devcon' is not recognized...

This is the most common issue. It means devcon.exe is not accessible to your script. Solution: Download and install the Windows Driver Kit (WDK) as described. Ensure devcon.exe is either in your system's PATH or you are running your script from the same directory where devcon.exe is located.

Problem: The Command Fails or Does Nothing (Administrator Privileges)

Initiating a hardware scan is a system-level operation.

Example of error message:

devcon 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: A New Device is Detected but Has an Error

The rescan command only tells Windows to look for new hardware; it doesn't guarantee that a working driver can be found for it.

Solution: This is not a failure of the devcon command but a driver issue. After running a rescan, the new device might appear in Device Manager with a yellow exclamation mark. Your script can follow up with a WMIC command to check for devices in an error state.

ECHO Checking for devices with errors...
WMIC PATH Win32_PnPEntity WHERE "Status='Error'" GET Caption

Practical Example: A "Post-Install" Hardware Detection Script

This script is designed for a scenario where a technician has just physically installed a new piece of hardware (like a network card or a sound card) and needs to force Windows to detect it and install the drivers.

@ECHO OFF
SETLOCAL
TITLE Hardware Installation Finalizer
REM This script must be run as an Administrator.

ECHO --- Finalizing New Hardware Installation ---
ECHO.
ECHO This script will now scan for new hardware and report the system status.
PAUSE

ECHO Step 1: Scanning for new hardware...
devcon rescan
ECHO Scan complete.
ECHO.

ECHO Step 2: Checking for any devices reporting an error state...
WMIC PATH Win32_PnPEntity WHERE "Status<>'OK'" GET Caption, Status /FORMAT:LIST

ECHO.
ECHO --- Script finished ---
ECHO Please check the list above for any devices that require attention.
PAUSE
ENDLOCAL

Conclusion

The DevCon.exe utility is the definitive tool for scripting hardware management tasks, and devcon rescan is its simplest and one of its most useful commands.

Key takeaways:

  • DevCon is not included with Windows; you must download it as part of the WDK.
  • You must run your script as an Administrator.
  • The command devcon rescan is the direct command-line equivalent of the "Scan for hardware changes" button in the Device Manager.
  • The command only initiates the scan; it does not guarantee that a working driver will be found for any new hardware.