Skip to content

Connected Devices In Windows

A while back I had check and see if a device was connected and ready to continue with some automation. That’s when I wrote the function below.

With this function you can get the list of all the connected devices by name, by ID with the optional search in Windows using AutoIT.

;Function Name: Get (Connected) Devices
;Written By: Amin Babaeipanah
;Usage: _getDevices(1,'')
;_getDevices("search for", flag)
;"search for": can be set to empty to list everything
;flag:
;    1 = List Device Name(s)
;    2 = List Device ID(s)
;    3 = List Device Name(s) @ Device ID(s)
;Example 1:
;    Code below will list all the connected devices by name
;        _getDevices('',1)
;    Code below will list all the connected devices by ID
;        _getDevices('',2)
;    Code below will list all the connected devices by name that has the word "COM"
;        _getDevices('COM',1)

Func _getDevices($name,$type)
    Local $objWMIService = ObjGet('winmgmts:\\localhost\root\CIMV2')
    Local $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_PnPEntity WHERE Name LIKE '%"&$name&"%'", "WQL", 48)
    If IsObj($colItems) Then
        If $type = 1 Then
            For $objItem In $colItems
                ConsoleWrite($objItem.Name&@LF)
            Next
        ElseIf $type = 2 Then
            For $objItem In $colItems
                ConsoleWrite($objItem.PNPDeviceID&@LF)
            Next
        ElseIf $type = 3 Then
            For $objItem In $colItems
                ConsoleWrite($objItem.Name&'@'&$objItem.PNPDeviceID&@LF)
            Next
        EndIf
    EndIf
EndFunc