Using VBS to determine whether the computer has USB 2.0 port code

Time:2021-4-13

Q:
Hello, script expert! Is there a way to use scripts to determine whether the computer has a USB ﹣ 2.0 ﹣ port?
— RD
A:
Hello, Rd. Are you familiar with the movie “Freaky Friday”, in which mother and daughter exchange bodies (and roles)? Well, there’s something like crazy Friday in our question. After all, in this column, it should have been the “script expert” who helped you. However, this problem actually helps a “script expert”. He had been trying (unsuccessfully) to test USB devices, and had almost regarded the work as meaningless. But when he read your question, he thought to himself, “Oh, yes: USB ﹣ 1.1 ﹣ and USB ﹣ 2.0.”. As soon as he tried to test the device in ﹣ USB ﹣ 1.1 ﹣ slot (compared with ﹣ 2.0 ﹣ slot), the problem was solved immediately.
Hey, no one said this particular “script expert” was a genius. (editor’s note: I certify this.)
We owe you a debt, RD; how can we repay you? Good idea: maybe we can start by answering your question. OK, here’s a script that most likely returns the number of USB ﹣ 2.0 ﹣ ports on the computer. Let’s look at the code first, and then explain why we say it “probably” returns the number of ports:

Copy codeThe code is as follows:
strComputer = “.” 
i = 0 
Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”) 
Set colControllers = objWMIService.ExecQuery _ 
    (“Select * From Win32_USBController”) 
For Each objController in colControllers 
    If Instr(objController.Name, “Enhanced”) Then 
        i = i + 1 
    End If 
Next 
Wscript.Echo “No. of USB 2.0 Ports: ” & i 

The script first assigns values to two variables. The variable strcomputer is given a dot (.), which represents the local computer. At the same time, assign a value of 0 to the counter variable ﹣ I ﹣ which we will use as the active counter for all found ﹣ USB ﹣ 2.0 ﹣ ports.
After connecting to the ﹣ WMI ﹣ service, we use this query to return all ﹣ Win32_ Instance of usbcontroller class:
Set colControllers = objWMIService.ExecQuery _
    (“Select * From Win32_USBController”)
Here we set up a “for each” loop to traverse each item in the collection (which, of course, represents all the “USB” ports on the computer). Unfortunately, Win32_ The usbcontroller class does not contain properties that can tell us the version of USB (such as version). In fact, there is no clear way to view the properties of the USB slot and know whether it is version 1.1 or version 2.0. However, most ﹣ 2.0 ﹣ ports contain the word ﹣ enhanced ﹣ somewhere in the name. We can’t guarantee that all ports are the same, which is why we say this script is likely to return the number of USB ﹣ 2.0 ﹣ ports on the computer. However, in random tests in the office, this method seems to work; it always has a name similar to the following when it finds a ﹣ 2.0 ﹣ slot:
VIA USB Enhanced Host Controller
So how does that help us? Well, this means that we can use the “instr” function to see if the word “enhanced” appears in the “name” of each slot
If Instr(objController.Name, “Enhanced”) Then
If ﹣ instr ﹣ returns a value of ﹣ true (technically, if it returns a value larger than ﹣ 0 ﹣ then the word ﹣ enhanced ﹣ is found somewhere in the name. In this case, we will increase the value of ﹣ I ﹣ by ﹣ 1:
i = i + 1
What if “instr” returns “0”, indicating that the word enhanced “cannot be found? Well, in this case, let’s go to the next loop and try the next term in the set, with the I * value unchanged.
We continue to query and increase the value of “I” every time we find a “USB” slot with “enhanced” in the name. When all is done, we just need to echo the “I” value, which will tell us the number of “USB” 2.0 “ports on the computer.
It’s not necessarily a foolproof method, but it seems to work. And, you know, it’s not the same as what you do for us.
Oh, yes; we forgot one thing. Well, maybe you like chocolate?