{"id":68003,"date":"2006-02-08T10:54:00","date_gmt":"2006-02-08T10:54:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/02\/08\/how-can-i-determine-whether-a-computer-has-any-usb-2-0-ports\/"},"modified":"2006-02-08T10:54:00","modified_gmt":"2006-02-08T10:54:00","slug":"how-can-i-determine-whether-a-computer-has-any-usb-2-0-ports","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-whether-a-computer-has-any-usb-2-0-ports\/","title":{"rendered":"How Can I Determine Whether a Computer Has Any USB 2.0 Ports?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Using a script, is there any way to determine whether a computer has any USB 2.0 ports?<BR><BR>&#8212; RD<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, RD. Are you familiar with the movie <I>Freaky Friday<\/I>, where the mom and the daughter switch bodies (and roles)? Well, we have sort of a <I>Freaky Friday<\/I> kind of thing going on with this question. After all, in this column the Scripting Guys are supposed to help you. But this question actually helped one of the Scripting Guys. He had been trying &#8211; unsuccessfully &#8211; to test a USB device, and had pretty much written the thing off as a piece of junk. Upon reading your question, though, he thought to himself, \u201cOh, yeah: USB 1.1 and USB 2.0\u201d As soon as he tried the device in a USB 1.1 slot &#8211; as opposed to a 2.0 slot &#8211; it worked just fine.<\/P>\n<P>Hey, no one ever said this particular Scripting Guy was a genius. <I>(Editor\u2019s Note: I\u2019ll vouch for that.)<\/I><\/P>\n<P>So we owe you one, RD; how can we ever repay you? Good point: maybe we <I>could<\/I> start by answering your question. OK, here\u2019s a script that will <I>probably<\/I> return the number of USB 2.0 ports on a computer. Let\u2019s show you the code, then we\u2019ll explain why we said it will \u201cprobably\u201d return the number of ports:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;\ni = 0<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colControllers = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_USBController&#8221;)<\/p>\n<p>For Each objController in colControllers\n    If Instr(objController.Name, &#8220;Enhanced&#8221;) Then\n        i = i + 1\n    End If\nNext<\/p>\n<p>Wscript.Echo &#8220;No. of USB 2.0 Ports: &#8221; &amp; i\n<\/PRE>\n<P>This script starts out by assigning values to two variables. The variable strComputer is assigned the value dot (.), which &#8211; in WMI-speak &#8211; represents the local computer. Meanwhile, the counter variable i is assigned the value 0; we\u2019ll be using this variable to keep a running tally of all the USB 2.0 ports we find.<\/P>\n<P>After connecting to the WMI service we then use this query to return all the instances of the <B>Win32_USBController<\/B> class:<\/P><PRE class=\"codeSample\">Set colControllers = objWMIService.ExecQuery _\n    (&#8220;Select * From Win32_USBController&#8221;)\n<\/PRE>\n<P>From there we set up a For Each loop to walk through each item in the collection (the collection, of course, represents all the USB ports on the computer). Unfortunately, the Win32_USBController class doesn\u2019t have a property (something like, say, Version) that can tell us the USB version; in fact, there\u2019s no obvious way to look at the properties of a USB slot and know whether it\u2019s version 1.1 or version 2.0. However, most 2.0 ports include the word <I>Enhanced<\/I> somewhere in their name. We can\u2019t guarantee that all of them do, which is why we say that this script will <I>probably<\/I> return the number of USB 2.0 ports on a computer. In a few random tests around the office, however, this approach seemed to work just fine; any time we found a 2.0 slot it had a name similar to this:<\/P><PRE class=\"codeSample\">VIA USB Enhanced Host Controller\n<\/PRE>\n<P>So how does that help us? Well, that means we can use the <B>InStr<\/B> function to see whether the word <I>Enhanced<\/I> appears anywhere in the <B>Name<\/B> of each slot:<\/P><PRE class=\"codeSample\">If Instr(objController.Name, &#8220;Enhanced&#8221;) Then\n<\/PRE>\n<P>If InStr returns the value True (technically, if it returns a value greater than 0), that means the word <I>Enhanced<\/I> was found somewhere in the name. In that case we increment the value of i by 1:<\/P><PRE class=\"codeSample\">i = i + 1\n<\/PRE>\n<P>And what if InStr returns a 0, meaning that the word <I>Enhanced<\/I> could <I>not<\/I> be found? Well, in that case we simply loop around and try the next item in the collection, leaving the value of i as-is.<\/P>\n<P>We continue along these lines, incrementing i each time we find a USB slot with the word <I>Enhanced<\/I> in its name. When we\u2019re all done we simply echo back the value of i, which tells us the number of USB 2.0 ports on the computer.<\/P>\n<P>It\u2019s not necessarily a foolproof approach, but it seems to work. And besides, it\u2019s not like you ever did anything for us, you know.<\/P>\n<P>Oh, right; we forgot about that. Um, you like chocolate by any chance?<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Using a script, is there any way to determine whether a computer has any USB 2.0 ports?&#8212; RD Hey, RD. Are you familiar with the movie Freaky Friday, where the mom and the daughter switch bodies (and roles)? Well, we have sort of a Freaky Friday kind of thing going on with [&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,721,3,5],"class_list":["post-68003","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-hardware","tag-ports-and-slots","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Using a script, is there any way to determine whether a computer has any USB 2.0 ports?&#8212; RD Hey, RD. Are you familiar with the movie Freaky Friday, where the mom and the daughter switch bodies (and roles)? Well, we have sort of a Freaky Friday kind of thing going on with [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68003","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=68003"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68003\/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=68003"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68003"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68003"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}