Skip to main content

How to List All Connected USB Devices in Batch Script

Getting a list of all the Universal Serial Bus (USB) devices connected to a computer is a common task for system inventory, security auditing, and troubleshooting. You might need to verify that a specific piece of hardware (like a security key or a webcam) is connected, or generate a report of all peripherals attached to a machine.

This guide will teach you how to use the built-in WMIC (Windows Management Instrumentation Command-line) utility to query the system for a list of all detected USB devices. You will learn the correct WMI class to use and how to create a simple, readable report from your batch script.

The Core Command: WMIC PATH Win32_USBHub

The standard way to get hardware information is with WMIC. While you might expect to query a class like Win32_USBDevice, the most reliable and comprehensive class to query is actually Win32_USBHub. This class represents the USB hubs (both internal on the motherboard and external), but by querying what is associated with them, we can find all the connected devices.

Syntax: WMIC PATH Win32_USBHub GET DeviceID, Description

However, a more powerful query uses the ASSOC keyword to find devices associated with the hubs. WMIC PATH Win32_USBHub ASSOCIATORS OF

This is a more advanced WMI query, but WMIC provides a simpler alias for listing all Plug and Play devices, which we can then filter.

The most reliable method is to query all Plug and Play devices and filter for those whose DeviceID indicates they are on the USB bus.

WMIC PATH Win32_PnPEntity WHERE "DeviceID LIKE 'USB%'" GET Caption
  • Win32_PnPEntity: The WMI class for all Plug and Play devices.
  • WHERE "DeviceID LIKE 'USB%'": The crucial filter. It selects only devices whose hardware ID starts with USB, effectively listing all USB-connected hardware.

Key USB Device Properties Explained

  • Caption or Name: The user-friendly name of the device (e.g., "Logitech USB Input Device", "USB Mass Storage Device").
  • Description: A more detailed description.
  • DeviceID: The unique Plug and Play identifier for the device (e.g., USB\VID_046D&PID_C077\5&1B8A48&0&2).
  • Manufacturer: The name of the device's manufacturer.
  • Status: The current health status of the device (e.g., "OK", "Error").

Basic Example: Displaying All USB Device Details

You can run this command directly to see a list of all USB devices connected to your machine.

@ECHO OFF
ECHO --- Querying All Connected USB Devices ---
ECHO.
WMIC PATH Win32_PnPEntity WHERE "DeviceID LIKE 'USB%'" GET Caption, Manufacturer, DeviceID

The output is a table listing each detected USB device.

--- Querying All Connected USB Devices ---

Caption DeviceID Manufacturer
USB Mass Storage Device USB\VID_0781&PID_5583\4C531001460408116342 SanDisk
Logitech USB Input Device USB\VID_046D&PID_C077\5&1B8A48&0&2 Logitech
USB Composite Device USB\VID_04F2&PID_B2E1\6&2E8B9A&0&5 (Standard USB Host Controller)
...

How to Capture the Information in a Script

To use this data, you should have WMIC format the output as CSV and then 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 All USB Devices ---
ECHO.

REM 'skip=1' ignores the header. 'tokens=2-4' grabs the three data columns.
FOR /F "skip=1 tokens=2,3,4 delims=," %%A IN (
'WMIC PATH Win32_PnPEntity WHERE "DeviceID LIKE 'USB%%'" GET Caption,DeviceID,Manufacturer /FORMAT:CSV'
) DO (
ECHO Found USB Device:
ECHO Caption: "%%~A"
ECHO Manufacturer: "%%~C"
ECHO Device ID: "%%~B"
ECHO.
)

ENDLOCAL
note

The %% in the LIKE clause needs to be escaped ('USB%%') when used inside a FOR /F command.

How the WMIC Method Works

The WMIC command interfaces with the WMI service, which gets its information from the Windows Plug and Play manager. The Win32_PnPEntity class contains a record for every piece of hardware the system knows about. By filtering this massive list with WHERE "DeviceID LIKE 'USB%'", we are asking WMI to return only the records whose unique hardware ID indicates they are connected to the USB bus. This is a very reliable and comprehensive way to find all USB peripherals.

Common Pitfalls and How to Solve Them

Problem: The List Includes Hubs and Internal Devices

The query will return everything on the USB bus, not just user-facing peripherals. This includes:

  • USB Root Hubs (the ports on your motherboard).
  • Internal devices that use a USB connection (like some webcams or fingerprint readers on laptops).
  • USB Composite Devices (which are often wrappers for other devices).

Solution: This is expected behavior. If you need to find a specific device, it's better to filter by a more specific part of its DeviceID, such as the Vendor ID (VID) and Product ID (PID). ... WHERE "DeviceID LIKE '%VID_0781&PID_5583% (This would find only the SanDisk device from the example).

Problem: The Output is Hard to Parse

The default table format of WMIC can be difficult to parse reliably.

Solution: Always use the /FORMAT:CSV switch when you intend to capture WMIC output in a script. It makes parsing with FOR /F simple and robust.

Practical Example: A "Connected Peripherals" Report

This script creates a simple report listing all connected USB devices and saves it to a text file.

@ECHO OFF
SETLOCAL
SET "ReportFile=%USERPROFILE%\Desktop\USB_Devices_Report.txt"

ECHO --- USB Device Inventory Script ---
ECHO Creating report at: "%ReportFile%"

(
ECHO USB Device Report for: %COMPUTERNAME%
ECHO Generated on: %DATE% at %TIME%
ECHO ------------------------------------
ECHO.
) > "%ReportFile%"

REM Use /FORMAT:LIST for a clear, readable report format.
WMIC PATH Win32_PnPEntity WHERE "DeviceID LIKE 'USB%%'" GET Caption,Manufacturer,Status,DeviceID /FORMAT:LIST >> "%ReportFile%"

ECHO.
ECHO [SUCCESS] Report created.
START "" "%ReportFile%"
ENDLOCAL

Conclusion

The WMIC command is the standard, built-in method for retrieving detailed information about connected USB devices from a batch script.

Key takeaways:

  • Query the Win32_PnPEntity WMI class for the most comprehensive list.
  • Use the crucial WHERE clause: WHERE "DeviceID LIKE 'USB%%'" to filter for only USB devices.
  • For scripting, always use a machine-readable format like /FORMAT:CSV and parse it with a FOR /F loop.
  • Be aware that the list will include all devices on the USB bus, including internal hubs.