How to Get Keyboard and Mouse Details in Batch Script
While less common than querying the CPU or hard drive, retrieving information about connected input devices like keyboards and mice can be a useful diagnostic task. You might need to check if a specific type of device is connected, list all pointing devices for an inventory, or troubleshoot driver issues.
This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to query the system for details about connected keyboards and pointing devices (mice, trackpads, etc.). You will learn the correct WMI classes to query and how to create a simple inventory report from your batch script.
The Core Command: WMIC PATH
For hardware that doesn't have a simple alias like CPU or BIOS, we must use the more general WMIC PATH command to query a specific WMI class directly. WMI is the underlying framework that stores detailed information about all hardware and software on the system.
The WMI Classes for Input Devices
There are two primary WMI classes we will use:
Win32_Keyboard: This class represents all keyboard devices connected to the system.Win32_PointingDevice: This class represents all pointing devices, which includes standard mice, trackpads on laptops, trackballs, and other similar hardware.
Basic Example: Listing Keyboards and Pointing Devices
This script runs the WMIC commands to get a simple, descriptive list of all detected keyboards and mice.
@ECHO OFF
ECHO --- Listing All Connected Input Devices ---
ECHO.
ECHO --- KEYBOARDS ---
WMIC PATH Win32_Keyboard GET Description, DeviceID
ECHO.
ECHO --- MICE AND POINTING DEVICES ---
WMIC PATH Win32_PointingDevice GET Manufacturer, Description, DeviceID
The output will be a table for each category, listing all the devices Windows has detected.
--- KEYBOARDS ---
Description DeviceID
Standard PS/2 Keyboard ACPI\PNP0303\4&1AAA61&0
--- MICE AND POINTING DEVICES ---
Description DeviceID Manufacturer
HID-compliant mouse HID\VID_046D&PID_C077\7&1B8A48&0 Logitech
Synaptics SMBus TouchPad ACPI\SYN1E00\4&1AAA61&0 Synaptics
Key Device Properties Explained
Description: A user-friendly string that describes the device (e.g., "HID-compliant mouse").DeviceID: The unique Plug and Play identifier for the device. This is similar to a Hardware ID.Manufacturer: The name of the device's manufacturer (e.g., "Logitech", "Microsoft").Status: The current status of the device (e.g., "OK", "Error").
How to Capture the Information in a Script
To use this information, you should format the WMIC output as CSV and parse it with a FOR /F loop. This is the most reliable way to handle device names that contain spaces.
@ECHO OFF
SETLOCAL
ECHO --- Capturing a List of Keyboards ---
ECHO.
REM 'skip=1' ignores the header. 'tokens=2-3' grabs the two data columns.
FOR /F "skip=1 tokens=2,3 delims=," %%A IN (
'WMIC PATH Win32_Keyboard GET Description,DeviceID /FORMAT:CSV'
) DO (
ECHO Found Keyboard:
ECHO Description: "%%~A"
ECHO Device ID: "%%~B"
ECHO.
)
ENDLOCAL
The ~ modifier (%%~A) is used to remove the quotes that the CSV format adds.
Common Pitfalls and How to Solve Them
- Generic Names: Many modern USB devices will simply report a generic name like "HID-compliant mouse" or "HID Keyboard Device" instead of a specific product name. This is normal and depends on the information provided by the device's driver. The
DeviceID, which contains the Vendor ID (VID) and Product ID (PID), is often a more unique identifier. - Multiple Devices: The commands will list all devices that Windows detects. This can include virtual mouse drivers installed by remote desktop software (like VMWare or RDP). This is expected behavior.
- Parsing the Output: As with all
WMICcommands, the default table format can be difficult to parse. Always use the/FORMAT:CSVswitch in your scripts to ensure the output is clean and predictable.
Practical Example: A Peripheral Devices Report
This script creates a simple report listing all keyboards and pointing devices and saves it to a text file.
@ECHO OFF
SETLOCAL
SET "ReportFile=%USERPROFILE%\Desktop\Peripherals_Report.txt"
ECHO --- Peripheral Device Inventory ---
ECHO Creating report at: "%ReportFile%"
(
ECHO Peripheral Report for: %COMPUTERNAME%
ECHO Generated on: %DATE% at %TIME%
ECHO ------------------------------------
ECHO.
ECHO --- KEYBOARDS ---
ECHO.
) > "%ReportFile%"
REM --- Get Keyboard data and append it to the report ---
WMIC PATH Win32_Keyboard GET Description, DeviceID, Status /FORMAT:LIST >> "%ReportFile%"
(
ECHO.
ECHO --- MICE AND POINTING DEVICES ---
ECHO.
) >> "%ReportFile%"
REM --- Get Pointing Device data and append it ---
WMIC PATH Win32_PointingDevice GET Manufacturer, Description, Status /FORMAT:LIST >> "%ReportFile%"
ECHO.
ECHO [SUCCESS] Report created.
START "" "%ReportFile%"
ENDLOCAL
This example uses /FORMAT:LIST which is another script-friendly format that is easy to read in a log file.
Conclusion
The WMIC command is the standard, built-in tool for querying detailed hardware information, including for peripherals like keyboards and mice.
Key takeaways:
- Use
WMIC PATH Win32_Keyboard GET ...to get information about keyboards. - Use
WMIC PATH Win32_PointingDevice GET ...to get information about mice, trackpads, etc. - For scripting, always use a machine-readable format like
/FORMAT:CSVor/FORMAT:LISTto make parsing reliable. - Use a
FOR /Floop to capture and process the information for each device found.