How to Install a Driver in a Batch Script
Automating driver installation is a critical task for system administrators, especially when deploying new computers or setting up servers. Manually clicking through the Device Manager GUI is slow and not repeatable. A batch script can automate this process, ensuring that the correct drivers are installed consistently every time.
This guide will teach you how to install a driver from its .inf file using the modern, standard, and built-in Windows utility: pnputil.exe (Plug and Play Utility). You will learn the correct command to stage and install a driver, how to install all drivers from a single folder, and the critical importance of running the script with full administrator privileges.
CRITICAL WARNING: Installing drivers is a low-level system operation. Installing an incorrect, unsigned, or corrupt driver can cause hardware to malfunction, system instability, or even prevent Windows from booting (the "Blue Screen of Death"). Always use trusted drivers from the official hardware manufacturer. This script must be run with full administrator privileges.
The Core Command: pnputil.exe
The pnputil.exe utility is the primary, built-in command-line tool for managing the Windows Driver Store. The Driver Store is a secure location where Windows keeps a collection of trusted driver packages. The process of installing a driver with pnputil is typically a two-step action done with a single command:
- Staging: The driver package is added to the Windows Driver Store.
- Installation: Windows applies the newly staged driver to any currently connected, matching hardware.
Understanding the .inf File
When you download a driver, the package usually contains several files (.sys, .cat, .dll, etc.). The most important file for installation is the one with the .inf extension. This is the Information file; it is a plain text file that acts as the "instruction manual" for Windows, telling it which files to copy, what registry entries to create, and which hardware IDs the driver is compatible with. The pnputil command works by pointing it at this .inf file.
Basic Example: Staging and Installing a Single Driver
This is the most direct way to install a single driver from its .inf file.
Syntax: pnputil /add-driver "C:\Path\to\driver.inf" /install
/add-driver: The main switch to add a driver package to the store./install: A crucial second switch. After the driver is successfully added to the store, this tellspnputilto immediately scan for any applicable hardware and install the driver.
@ECHO OFF
REM This script MUST be run as an Administrator.
SET "DriverInf=C:\Drivers\Intel_Chipset\ichipset.inf"
ECHO --- Installing a Single Driver ---
ECHO INF File: "%DriverInf%"
ECHO.
pnputil /add-driver "%DriverInf%" /install
IF %ERRORLEVEL% EQU 0 (
ECHO [SUCCESS] Driver installed successfully.
) ELSE (
ECHO [FAILURE] An error occurred. Errorlevel: %ERRORLEVEL%
)
The Best Practice: Installing All Drivers from a Folder
A more powerful and common scenario is to install all the drivers from a folder at once. For example, after a clean Windows install, you might have a C:\Drivers folder containing subfolders for Chipset, Audio, Network, etc. pnputil can handle this with a single command.
Syntax: pnputil /add-driver "C:\Drivers\*.inf" /subdirs /install
*.inf: A wildcard that tells it to find all.inffiles./subdirs: This switch is the key. It tellspnputilto search for.inffiles recursively through all subdirectories.
Key pnputil Parameters Explained
/add-driver: Adds a driver package to the store./delete-driver: Removes a driver package from the store./export-driver: Exports a driver package from the store to a directory./install: Installs the specified driver on any matching devices./subdirs: Searches for driver packages in subdirectories./reboot: Reboots the system if it is required to complete the operation.
Common Pitfalls and How to Solve Them
-
"Access is denied." (Error 5): This is the number one reason for failure. Solution: You must run the script from an elevated command prompt ("Run as administrator").
-
Unsigned Drivers: Modern 64-bit versions of Windows enforce "Driver Signature Enforcement," which means they will refuse to install any driver that does not have a valid digital signature.
- The Error:
Adding the driver package failed: The third-party INF does not contain digital signature information. - Solution: Only use official, signed drivers from the hardware manufacturer. There is no simple script-based workaround for this security feature.
- The Error:
-
Finding the Right
.infFile: A driver package might contain multiple.inffiles.- Solution: You don't need to figure out which one is correct. The
/subdirsmethod is the best practice. Simply pointpnputilto the root folder of your drivers, and it will intelligently find and use the correct.inffiles for the hardware present on your system.
- Solution: You don't need to figure out which one is correct. The
Practical Example: An Automated Driver Installation Script for a New PC
This script is designed to be run after a fresh Windows installation. It assumes you have copied all the necessary driver packages into a single C:\Drivers directory.
@ECHO OFF
TITLE Automated Driver Installer
REM This script MUST be run as an Administrator.
SET "DriverRepo=C:\Drivers"
ECHO --- Automated Driver Installation ---
ECHO.
ECHO This script will recursively install all drivers from:
ECHO "%DriverRepo%"
ECHO.
ECHO This process may take several minutes.
PAUSE
ECHO.
IF NOT EXIST "%DriverRepo%\" (
ECHO [ERROR] Driver repository not found. Aborting.
PAUSE
GOTO :EOF
)
ECHO Installing drivers...
pnputil /add-driver "%DriverRepo%\*.inf" /subdirs /install
ECHO.
ECHO --- Driver Installation Complete ---
ECHO.
ECHO It is highly recommended to reboot the system now.
CHOICE /C YN /M "Do you want to reboot now?"
IF ERRORLEVEL 1 IF NOT ERRORLEVEL 2 shutdown /r /t 5 /c "Driver installation complete."
Conclusion
The pnputil.exe command is the modern and definitive tool for automating driver installations from a batch script.
For reliable driver management:
- Always run your script as an Administrator.
- Use the command
pnputil /add-driver "driver.inf" /installto stage and install a driver. - For mass installations, the most powerful and effective command is
pnputil /add-driver "C:\Drivers\*.inf" /subdirs /install. - Be aware that a reboot may be required for some drivers to become fully functional.