Windows USB WMI Scripting using VBScript

I’m looking to release one more Windows WMI Script for getting USB information from a Windows PC.

I don’t recall where I first got this script or what specific problem I was trying to solve but this Window WMI VBScript loops through all the USB devices on a PC.

I’ve modified this script a bit to look for something called “SymbolicName” and fire off a message when that value is found. This was something I must have been troubleshooting with respect to USB devices working or not and if drivers are properly installed.

WMI VBScript

' Get the serial number of a usb device

'USB_REGKEY sets the regkey where displays are found. Don't change except for debugging
'I only change it when I am looking at a .REG file that someone sent me saying that the
'code doesn't work.
Const USB_REGKEY="HKLM\SYSTEM\CurrentControlSet\Enum\USB\"
Const USB_REGKEY2="SYSTEM\CurrentControlSet\Enum\USB\"

'if set to 1 then output debug info to DEBUGFILE (also writes debug to screen if running under cscript.exe)

'simple function to provide a wmi registry provider
'to all the other registry functions (regenumkeys, reggetstringvalue, etc...)
Function GetWMIRegProvider()
	strComputer = "."
	Set GetWMIRegProvider=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
End Function

'if debugmode=1 the writes dubug info to the specified
'file and if running under cscript also writes it to screen.
Sub DebugOut(strDebugInfo)
	if DEBUGMODE=0 then
		exit sub
	end if

	if strCurrScriptHost="cscript.exe" then 
		wscript.echo "Debug: " & strDebugInfo
		set objDebugFSO=CreateObject("Scripting.FileSystemObject")
		set objDebugStream=objDebugFSO.OpenTextFile(DEBUGFILE,AppendFileMode,True,False)
		objDebugStream.writeline strDebugInfo
		set objDebugStream=Nothing
		set objDebugFSO=Nothing
	end if
End Sub 

'function to parse the specified hive
'from the registry functions above
'to all the other registry functions (regenumkeys, reggetstringvalue, etc...)
Function SetHive(RegKey)
	if strHive="HKCR\" or strHive="HKR\" then
	end if
	if strHive="HKCU\" then
	end if
	if strHive="HKCC\" then
	end if
	if strHive="HKLM\" then
	end if
	if strHive="HKU\" then
	end if
End Function

'simple function to provide an
'easier interface to the wmi registry functions
Function RegEnumKeys(RegKey)
	set objReg=GetWMIRegProvider()
	strKeyPath = right(RegKey,len(RegKey)-instr(RegKey,"\"))
	objReg.EnumKey Hive, strKeyPath, arrSubKeys
End Function

'This function returns an array of all subkeys of the 
'regkey defined by DISPLAY_REGKEY
'(typically this should be "HKLM\SYSTEM\CurrentControlSet\Enum\USB")
Function GetAllUSBDevicesInReg()
	dim arrResult()
	redim arrResult(0)
	dim arrResult2()
	redim arrResult2(0)	
	dim arrResult3()
	redim arrResult3(0)		
	if vartype(arrtmpkeys)<>8204 then
		debugout "Display=Can't enum subkeys of display regkey"
		for tmpctr=0 to ubound(arrtmpkeys)
			arrtmpkeys2 = RegEnumKeys(USB_REGKEY& arrtmpkeys(tmpctr))
			for tmpctr2 = 0 to ubound(arrtmpkeys2)
				arrtmpkeys3 = RegEnumKeys(USB_REGKEY& arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2))
				redim preserve arrResult(intArrResultIndex)
				arrResult(intArrResultIndex)=USB_REGKEY & arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2)
				debugout "USB=" & arrResult(intArrResultIndex)
				'WScript.Echo "From #1 USB=" & arrResult(intArrResultIndex)
				for tmpctr3 = 0 to ubound(arrtmpkeys3)
					redim preserve arrResult2(intArrResultIndex2)
					arrResult2(intArrResultIndex2)=USB_REGKEY & arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2) & "\" & arrtmpkeys3(tmpctr3)
					debugout "USB=" & arrResult2(intArrResultIndex2)
					'WScript.Echo "From #2 USB=" & arrResult2(intArrResultIndex2)
					if InStr(arrResult2(intArrResultIndex2),"Device Parameters") > 0 then
						WScript.Echo arrResult2(intArrResultIndex2)
						'WScript.Echo arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2) & "\" & arrtmpkeys3(tmpctr3)
						GetRegValues(arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2) & "\" & arrtmpkeys3(tmpctr3))
					end if
					'for tmpctr4 = 0 to ubound(arrtmpkeys4)
					'	intArrResultIndex3=intArrResultIndex3+1
					'	redim preserve arrResult3(intArrResultIndex3)
					'	arrResult2(intArrResultIndex2)=USB_REGKEY & arrtmpkeys(tmpctr) & "\" & arrtmpkeys2(tmpctr2) & "\" & arrtmpkeys3(tmpctr3)
					'	debugout "USB=" & arrResult2(intArrResultIndex2)
					'	WScript.Echo "From #2 USB=" & arrResult2(intArrResultIndex2)
	end if
End Function

' Constants (taken from WinReg.h)
Sub GetRegValues(strKeyPath)
const HKLM = &H80000002

dim keyPath
keyPath = "SYSTEM\CurrentControlSet\Enum\USB\" & strKeyPath

dim reg

dim valueNames, types
dim value
dim i

  set reg = getObject( "Winmgmts:root\default:StdRegProv" )

  if reg.enumValues( HKLM, keyPath, valueNames, types ) = 0 then
    if isArray( valueNames ) then
      for i = 0 to UBound( valueNames )
        reg.getStringValue HKLM, keyPath, valueNames(i), value
		If InStr(ValueNames(i),"SymbolicName") > 0 then
			msgBox("I found you! " & valueNames(i) & "=" & value )
		end if
    end if
  end if

End Sub


Here is a screenshot of the code on line 160. If we see a value that has the text “SymbolicName”, the script will stop and display that value in a message box.

List all USB Devices on a Window PC using VBScript

I’m going through some old scripts I’ve written or stolen from my years as a solutions engineer / developer and have come across one I thought was worthy of sharing.

The intent of this script was to loop through a list of USB devices found on a given PC.

I might store the results in a database as part of an device tracking inventory system or use the data to do something else in the code logic if I had found a specific USB device attached.

Example: VBScript to List all USB Devices

Get the code here on GitHub

'Win32_PnPEntity info here: 
strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colDevices = objWMIService.ExecQuery _
    ("Select * From Win32_USBControllerDevice")

For Each objDevice in colDevices
    strDeviceName = objDevice.Dependent
    strQuotes = Chr(34)
    strDeviceName = Replace(strDeviceName, strQuotes, "")
    arrDeviceNames = Split(strDeviceName, "=")
    strDeviceName = arrDeviceNames(1)
    Set colUSBDevices = objWMIService.ExecQuery _
        ("Select * From Win32_PnPEntity Where DeviceID = '" & strDeviceName & "'")
    For Each objUSBDevice in colUSBDevices
        Wscript.Echo objUSBDevice.Description & ": " & objUSBDevice.Manufacturer

If you run this script on a remote computer on a network domain instead of your own, you will probably have to run this script with Administrator rights depending on the who the network is configured.

You can check out my article on how to run VBScript as an administrator if you need help with that.

Hope this helps somebody! 🙂

~Cyber Abyss