title: Verify USB devices are connected to particular USB Ports
body: <p>I'm trying to run the below Visual Basic Script, which is executed via CMD. The script works great, however it only covers a very basic purpose. I'm trying to verify that USB1 port has a particular device connected to it, either by checking the device name, or VID/PID. Could someone help me with a script that could verify certain USB ports have particular devices connected? </p>
  
  <p>The environment is identical on the multiple machines this will be ran on, and for example, I want to verify that USB Port 1 contains a device with "Scanner" in its name, or by checking the ID of the device. Is this possible?</p>
  
  <pre><code>Option Explicit
  Dim oWMISrv, collDvcs, collUSBDvcs, iUSBDvc , iDvc, sDvcID, sPID, sVID
  
  Function ReplaceX(ByVal sValue, ByVal sPattern, ByVal sNValue)
      Dim oReg : Set oReg = New RegExp
      oReg.Pattern = sPattern
      ReplaceX = oReg.Replace(sValue, sNValue)
      Set oReg = Nothing
  End Function
  
  Set oWMISrv = GetObject("winmgmts:\\.\root\cimv2")
  Set collDvcs = oWMISrv.ExecQuery("Select * From Win32_USBControllerDevice")
  
  For Each iDvc In collDvcs
      If InStr(iDvc.Dependent, "VID_") Then ' Except keychain drives
  
          sDvcID = ReplaceX(iDvc.Dependent, ".*""(.*)""", "$1")
          sPID = ReplaceX(sDvcID, ".*PID_([^\\]*).*", "$1")
          sVID = ReplaceX(sDvcID, ".*VID_([^&amp;]*).*", "$1")
  
          Set collUSBDvcs = oWMISrv.ExecQuery _
          ("Select * From Win32_PnPEntity Where DeviceID = '" &amp; sDvcID &amp; "'")
          For Each iUSBDvc in collUSBDvcs
              Wscript.Echo "Name : "&amp; iUSBDvc.Description
              Wscript.Echo "VID : "&amp; sVID
              Wscript.Echo "PID : "&amp; sPID
              Wscript.Echo String(50, "-")
          Next
          Set collUSBDvcs = Nothing
  
      End If
  Next
  
  Set collDvcs = Nothing
  Set oWMISrv = Nothing
  </code></pre>