6

I need to get connected USB device list from windows by using python or cmd.

for python i'm trying this.

import win32com.client
def get_usb_device():
    try:
        usb_list = []
        wmi = win32com.client.GetObject("winmgmts:")
        for usb in wmi.InstancesOf("Win32_USBHub"):
            print(usb.DeviceID)
            print(usb.description)
            usb_list.append(usb.description)

        print(usb_list)
        return usb_list
    except Exception as error:
        print('error', error)


get_usb_device()

as a result i get this:

['USB Root Hub (USB 3.0)', 'USB Composite Device', 'USB Composite Device']

but i don't get a meaning full name.

and for cmd i'm trying this also:

wmic path CIM_LogicalDevice where "Description like 'USB%'" get /value

and again i don't get any meaning full name for connected usb devices.

when I'm connect a mouse, keyboard, pen drive or printer through the usb i want this kind of name. like 'a4tech mouse' or even if i get 'mouse' only that's also be fine. This type of name appears in the device section of the settings of windows 10. but i get 'USB Root Hub (USB 3.0)', 'USB Composite Device', which means nothing actually. Is it possible with python?

If any one know this answer please help. Its very important for me.

SNA Nilim
  • 211
  • 1
  • 3
  • 8
  • ***"but i don't get a meaning full name."*** - What are you expecting? – Pedro Lobito Nov 14 '19 at 13:36
  • Does this answer your question? [Python: get name of a USB flash drive device \[windows\]](https://stackoverflow.com/questions/33784537/python-get-name-of-a-usb-flash-drive-device-windows) – Prudhvi Nov 14 '19 at 13:43

2 Answers2

6

Not sure if it's what you are looking for, but using Python 3 on Windows 10 with pywin32, you could use this to get all your drive letters and types:

import os
import win32api
import win32file
os.system("cls")
drive_types = {
                win32file.DRIVE_UNKNOWN : "Unknown\nDrive type can't be determined.",
                win32file.DRIVE_REMOVABLE : "Removable\nDrive has removable media. This includes all floppy drives and many other varieties of storage devices.",
                win32file.DRIVE_FIXED : "Fixed\nDrive has fixed (nonremovable) media. This includes all hard drives, including hard drives that are removable.",
                win32file.DRIVE_REMOTE : "Remote\nNetwork drives. This includes drives shared anywhere on a network.",
                win32file.DRIVE_CDROM : "CDROM\nDrive is a CD-ROM. No distinction is made between read-only and read/write CD-ROM drives.",
                win32file.DRIVE_RAMDISK : "RAMDisk\nDrive is a block of random access memory (RAM) on the local computer that behaves like a disk drive.",
                win32file.DRIVE_NO_ROOT_DIR : "The root directory does not exist."
              }

drives = win32api.GetLogicalDriveStrings().split('\x00')[:-1]

for device in drives:
    type = win32file.GetDriveType(device)
    
    print("Drive: %s" % device)
    print(drive_types[type])
    print("-"*72)

os.system('pause')

Your USB devices have the type win32file.DRIVE_REMOVABLE - so this is what you're looking for. Instead of printing all drives and types, you could insert an if condition to only process such removable devices.

Please note: SD-Cards and other removable storage media has the same drive type.

HTH!


Update, 13. July 2020:

To get further Inforrmations about connected Devices, have a look at the WMI Module for Python.

Check this example outputs, they list different informations about devices, including Manufacturer Descriptions, Serial Numbers and so on:

import wmi
c = wmi.WMI()

for item in c.Win32_PhysicalMedia():
    print(item)

for drive in c.Win32_DiskDrive():
    print(drive)

for disk in c.Win32_LogicalDisk():
    print(disk)

os.system('pause')

To access a specific Information listed in this output, use the displayed Terms for direct access. Example:

for disk in c.Win32_LogicalDisk():
    print(disk.Name)
xph
  • 937
  • 3
  • 8
  • 16
  • Can you please show how we can get the device id and the product for the removable devices? – S.I.J Jul 09 '20 at 19:02
  • @S.I.J: I've updated the answer, there is a module which might help you to get the informations you need. – xph Jul 13 '20 at 11:08
1

when I'm connect a mouse, keyboard, pen drive or printer through the usb i want this kind of name...

It's called "Friendly Name" and you can use:

import subprocess, json

out = subprocess.getoutput("PowerShell -Command \"& {Get-PnpDevice | Select-Object Status,Class,FriendlyName,InstanceId | ConvertTo-Json}\"")
j = json.loads(out)
for dev in j:
    print(dev['Status'], dev['Class'], dev['FriendlyName'], dev['InstanceId'] )

Unknown HIDClass HID-compliant system controller HID\VID_046D&PID_C52B&MI_01&COL03\9&232FD3F1&0&0002
OK DiskDrive WD My Passport 0827 USB Device USBSTOR\DISK&VEN_WD&PROD_MY_PASSPORT_0827&REV_1012\575836314142354559545058&0
...
Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268
  • when I'm connect a mouse, keyboard, pen drive or printer through the usb i want this kind of name. like 'a4tech mouse' or even if i get 'mouse' only that's also be fine. This type of name appears in the device section of the settings of windows 10. but i get 'USB Root Hub (USB 3.0)', 'USB Composite Device', which means nothing actually. maybe u know get what i actually want. and also added this on main qns section. sorry for my les explanation in first time. – SNA Nilim Nov 14 '19 at 17:51
  • You don't need to repeat the same text 4 times on this page, it's a waste of time and resources. You need the usb `friendly name`, which you can find on my answer. – Pedro Lobito Nov 14 '19 at 17:55
  • i get this using this code and FriendlyName ` 'USB':[ 'USB Root Hub (USB 3.0)', 'Intel(R) USB 3.0 eXtensible Host Controller - 1.0 (Microsoft)', 'USB Composite Device', 'USB Composite Device' ], 'Mouse': [ 'HID-compliant mouse', 'HID-compliant mouse', 'HID-compliant mouse' ], ` But in windows (settings->Bluetooth & other devices->Mouse, keyboard & pen) section I see: ** 2.4G Wireless Mouse USB Optical Mouse ** is it possible to get those name by this code or any other code? – SNA Nilim Nov 17 '19 at 06:06
  • Sure, change this line `dev['Status'], dev['Class'], dev['FriendlyName'], dev['InstanceId'] ` to `dev['FriendlyName']` – Pedro Lobito Nov 17 '19 at 21:46
  • If my answer helped you, please consider accepting it as the correct answer. Thank you! – Pedro Lobito Nov 17 '19 at 21:47
  • Yes i use this `dev['FriendlyName']` and get this `'USB':[ 'USB Root Hub (USB 3.0)', 'Intel(R) USB 3.0 eXtensible Host Controller - 1.0 (Microsoft)', 'USB Composite Device', 'USB Composite Device' ], 'Mouse': [ 'HID-compliant mouse', 'HID-compliant mouse', 'HID-compliant mouse' ],` which i mention earlier comment. But exactly i want this. `[2.4G Wireless Mouse, USB Optical Mouse]` which i see here **(settings->Bluetooth & other devices->Mouse, keyboard & pen) section** – SNA Nilim Nov 18 '19 at 05:50