- 1 year ago
How to get connected USB device list from windows by using python or cmd
To retrieve a list of connected USB devices in Windows using Python, you can utilize the pywinusb
library. Here's an example:
pythonimport pywinusb.hid as hid
def get_usb_devices():
devices = hid.HidDeviceFilter().get_devices()
usb_devices = []
for device in devices:
usb_device = {
'vendor_id': device.vendor_id,
'product_id': device.product_id,
'serial_number': device.serial_number,
'vendor_name': device.vendor_name,
'product_name': device.product_name,
}
usb_devices.append(usb_device)
return usb_devices
# Retrieve USB devices
devices = get_usb_devices()
# Print the list of USB devices
for device in devices:
print(device)
Make sure you have the pywinusb
library installed. You can install it using pip:
pip install pywinusb
This example uses the HidDeviceFilter
class from pywinusb
to filter and retrieve HID devices, which include USB devices. It iterates over the devices, extracts relevant information like vendor ID, product ID, serial number, vendor name, and product name, and stores them in a list of dictionaries.
You can modify or extend the code based on your specific requirements or the type of USB devices you are interested in.
If you prefer to use the command-line interface (CMD) to retrieve the connected USB devices, you can use the wmic
command. Open the command prompt and run the following command:
arduinowmic path Win32_USBControllerDevice get Dependent
This command will display the list of connected USB devices, including their details such as device IDs, descriptions, and other information.
Both the Python approach and the CMD approach allow you to retrieve the list of connected USB devices in Windows. Choose the one that best fits your needs and programming environment.