{"id":70233,"date":"2005-03-15T16:21:00","date_gmt":"2005-03-15T16:21:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/03\/15\/how-can-i-determine-which-usb-devices-are-connected-to-a-computer\/"},"modified":"2005-03-15T16:21:00","modified_gmt":"2005-03-15T16:21:00","slug":"how-can-i-determine-which-usb-devices-are-connected-to-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-which-usb-devices-are-connected-to-a-computer\/","title":{"rendered":"How Can I Determine Which USB Devices are Connected to a Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I determine which USB devices are connected to a computer?<BR><BR>&#8212; WM<\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, WM. If you watch a lot of TV &#8211; um, not that the Scripting Guys do; we spend our spare time reading the works of Shakespeare, solving differential equations, and otherwise tending to our many intellectual pursuits. As we\u2019ve been <I>told<\/I>, however, any time there\u2019s a major crisis on a TV show one of the main characters grabs a bullhorn and tries to calm the crowd. \u201cStay calm!\u201d he or she tells everyone. \u201cThere\u2019s no reason to be alarmed. Everything\u2019s going to be just fine.\u201d<\/P>\n<P>There are times when we Scripting Guys wish we had a bullhorn, and this is one of them. Can you determine which USB devices are connected to a computer? Yes, although you might start to panic a bit when you see <I>how<\/I> you have to do this. Stay calm. There\u2019s no reason to be alarmed. <\/P>\n<P>You\u2019re right: we <I>should<\/I> have our own TV show, shouldn\u2019t we? <\/P>\n<P>The problem we have here is that there isn\u2019t a Dynamic class devoted to USB devices: you can\u2019t return a collection of USB devices the same way you can use Win32_Services to return a collection of all the services on a computer. Instead, you have to use a WMI Association class (Win32_USBControllerDevice) that associates a USB controller with a USB device. With that information you can identify the dependent entity in the association (which happens to be the device associated with a USB controller) and can then query the Win32_PNPEntity to get information about the device itself. (Unfortunately, Win32_USBControllerDevice returns nothing about the device except the somewhat-cryptic device ID.) <\/P>\n<P>Confused? Don\u2019t feel bad; we\u2019re not 100% sure we understand it. So let\u2019s make a deal: why don\u2019t we show you the script, and then you can decide for yourself if you want to tackle the explanation of how the script works:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colDevices = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_USBControllerDevice&#8221;)<\/p>\n<p>For Each objDevice in colDevices\n    strDeviceName = objDevice.Dependent\n    strQuotes = Chr(34)\n    strDeviceName = Replace(strDeviceName, strQuotes, &#8220;&#8221;)\n    arrDeviceNames = Split(strDeviceName, &#8220;=&#8221;)\n    strDeviceName = arrDeviceNames(1)\n    Set colUSBDevices = objWMIService.ExecQuery _\n        (&#8220;Select * From Win32_PnPEntity Where DeviceID = &#8216;&#8221; &amp; strDeviceName &amp; &#8220;&#8216;&#8221;)\n    For Each objUSBDevice in colUSBDevices\n        Wscript.Echo objUSBDevice.Description\n    Next    \nNext\n<\/PRE>\n<P>Oh, good: you\u2019re still with us. As you can see, the script starts off by querying the Win32_USBControllerDevice; this returns a collection of all the USB controllers and the devices currently associated with them. That\u2019s the good news. The bad news is this: we need to isolate the dependent entity for each item in the collection, an entity that represents a USB device. Why is that bad news? Well, it\u2019s bad news because the Dependent property value is going to look something like this:<\/P><PRE class=\"codeSample\">\\\\TOMSERVO\\root\\cimv2:Win32_PnPEntity.DeviceID=&#8221;USB\\\\VID_045E&amp;PID_0029\\\\5&amp;236EE205&amp;0&amp;2&#8243;\n<\/PRE>\n<P>Yuck. What we need to do is somehow isolate the actual device ID; in this case, <B>USB\\\\VID_045E&amp;PID_0029\\\\5&amp;236EE205&amp;0&amp;2<\/B>. When we have <I>that<\/I> value, we can then query the Win32_PNPEntity class for information about the device. <\/P>\n<P>So how do we isolate the device ID? Well, there are probably a million different ways to do this; we chose one we thought was reasonably easy to follow. (Which ought to give you some indication of what the <I>other<\/I> methods were like.)<\/P>\n<P>We begin by storing the value of the Dependent property in a variable named strDeviceName. We then use the VBScript Replace function to remove the double quotes &#8211; which we indicate by using Chr(34) &#8211; from the string. That leaves us with a value that looks like this:<\/P><PRE class=\"codeSample\">\\\\TOMSERVO\\root\\cimv2:Win32_PnPEntity.DeviceID=USB\\\\VID_045E&amp;PID_0029\\\\5&amp;236EE205&amp;0&amp;2\n<\/PRE>\n<P>Believe it or not, removing those double quotes puts us on the home stretch. If you examine the remaining value closely, you\u2019ll notice that it\u2019s basically divided into two parts, with the two parts separated by an equal sign. On the left of the equal sign we have the WMI path to the class and property; on the right of the equal sign we have the device ID. Why does that matter to us? Because now we can use the VBScript Split function to split the value on the equal sign, and create an array consisting of two items. Item 1 will be the WMI path, and item 2 will be &#8211; bingo! &#8211; the device ID. These two lines of code create the array, and then assign the value of item 2 in our array (the device ID) to the variable strDeviceName:<\/P><PRE class=\"codeSample\">arrDeviceNames = Split(strDeviceName, &#8220;=&#8221;)\nstrDeviceName = arrDeviceNames(1)\n<\/PRE>\n<P>We now have the device ID for the first USB device connected to the computer. With the device ID in hand we can query the Win32_PNPEntity class for information about the USB device that\u2019s been assigned that device ID. That\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set colUSBDevices = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_PnPEntity Where DeviceID = &#8216;&#8221; &amp; strDeviceName &amp; &#8220;&#8216;&#8221;)\n<\/PRE>\n<P>This query will return information about the actual device itself. In this script we do nothing more than echo back the value of the Description property for each device, although we could just as easily echo back values for any of the properties found in the Win32_PNPEntity class. We then loop around and repeat this process for each item in the Win32_USBControllerDevice collection.<\/P>\n<P>When we run the script we\u2019ll get back information similar to this (depending, of course, on the devices currently connected to the computer):<\/P><PRE class=\"codeSample\">USB Root Hub\nMicrosoft USB IntelliMouse Web\nMicrosoft USB IntelliMouse Web\nUSB Mass Storage Device\nDisk drive\nGeneric volume\nUSB Root Hub\nUSB Root Hub\n<\/PRE>\n<P>It\u2019s not perfect; USB Mass Storage Device, Disk drive, and Generic volume all refer to the same USB keychain drive. But at least it gives you some idea of what devices are (or are not) attached to a computer.<\/P>\n<P>Just like we said: No reason to be alarmed. Everything is just fine.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine which USB devices are connected to a computer?&#8212; WM Hey, WM. If you watch a lot of TV &#8211; um, not that the Scripting Guys do; we spend our spare time reading the works of Shakespeare, solving differential equations, and otherwise tending to our many intellectual pursuits. As [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[34,35,3,5],"class_list":["post-70233","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hardware","tag-peripherals-and-devices","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I determine which USB devices are connected to a computer?&#8212; WM Hey, WM. If you watch a lot of TV &#8211; um, not that the Scripting Guys do; we spend our spare time reading the works of Shakespeare, solving differential equations, and otherwise tending to our many intellectual pursuits. As [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70233","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70233\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}