none
WQL Query to get all devices connected to a specific hub RRS feed

  • Question

  • What is the WMI / WQL Query syntax to get all devices connected to a specific USB Hub?

    I can open up a PS terminal and enumerate all USB Hub Devices, as such:

    gwmi Win32_UsbHub | fl *

    The output will return a list of all USB Hub device on the system. Here is a sampling of one such device.

        Device ID: USB\VID_0451&PID_DD01\6&16FAF918&1&2, PNP Device ID: USB\VID_0451&PID_DD01\6&16FAF918&1&2, Description: Generic SuperSpeed USB Hub
        	Availability
        	Caption=Generic SuperSpeed USB Hub
        	ClassCode
        	ConfigManagerErrorCode=0
        	ConfigManagerUserConfig=False
        	CreationClassName=Win32_USBHub
        	CurrentAlternateSettings
        	CurrentConfigValue
        	Description=Generic SuperSpeed USB Hub
        	DeviceID=USB\VID_0451&PID_DD01\6&16FAF918&1&2
        	ErrorCleared
        	ErrorDescription
        	GangSwitched
        	InstallDate
        	LastErrorCode
        	Name=Generic SuperSpeed USB Hub
        	NumberOfConfigs
        	NumberOfPorts
        	PNPDeviceID=USB\VID_0451&PID_DD01\6&16FAF918&1&2
        	PowerManagementCapabilities
        	PowerManagementSupported
        	ProtocolCode
        	Status=OK
        	StatusInfo
        	SubclassCode
        	SystemCreationClassName=Win32_ComputerSystem
        	SystemName=JMR-ENG-SARAH
        	USBVersion

    Using say the `PNPDeviceID` for this hub, I would like to get all connected devices, if any to it.

    I am totally green with PS and WMI, but I thought of using `ASSOCIATES OF` as follows:

        Get-WmiObject Win32_PnPEntity | select "ASSOCIATORS OF {Win32_UsbHub.DeviceID='USB\VID_0451&PID_DD01\6&16FAF918&1&2'}"

    Annoyingly, enumerations do not list parent and children, though Device Manager does list things nicely and has a Parent.

    Device Manager view of a `Generic USB Hub` and `Generic SuperSpeed USB Hub` Showing Children

    Maybe just the way that I phrased things. Many other ways threw an error, so I did research and saw this syntax. I originally thought of using a `gwmi` command. In any event, I know from `Device Manager` that there are connected devices, but the above command yielded a blank screen.

    `WMI Explorer` was not much help.

    WMI Explorer

    What would be the `SELECT` statement to use?

    Friday, August 17, 2018 9:08 PM

All replies

  • Parent to child with PowerShell:

    gwmi Win32_UsbHub|%{$_.GetRelated('Win32_PnPEntity')} | select Name, DeviceID


    \_(ツ)_/

    • Proposed as answer by jrv Friday, August 17, 2018 10:43 PM
    • Unproposed as answer by Bill_StewartModerator Monday, July 29, 2019 7:25 PM
    Friday, August 17, 2018 9:17 PM
  • @jrv thanks for the quick response, but what about a specific USB Hub. I thought of the following based on your answer, but got nothing.

    gwmi Win32_UsbHub|%{$_.GetRelated('Win32_PnPEntity')} | select Name, DeviceID | where Win32_UsbHub.DeviceID='USB\VID_0451&PID_DD01\6&16FAF918&1&2'

    Friday, August 17, 2018 9:24 PM
  • Also, how does your response look as a formal SQL statement?

    My end goal is to port to C#, where I will use code like the following:

    ManagementObjectCollection mbsList = null;
                ManagementObjectSearcher mbs = new ManagementObjectSearcher("Select * From Win32_USBHub");
                mbsList = mbs.Get();
    
                foreach (ManagementObject mo in mbsList)
                {
                    Console.WriteLine("USBHub device Friendly name:{0}", mo["Name"].ToString());
                }

    I would replace what you gave with with the SELECT statement shown.


    Friday, August 17, 2018 9:33 PM
  • I have to try this link, but I believe that I found the answer based on your response. A premature yippee, but yippee, I hope.

    MSDN .GetRelated

    using System;
    using System.Management;
       
    public class Sample
    {
        public static void Main(string[] args) 
        {
            ManagementObject o = 
                new ManagementObject("Win32_Service='Alerter'");
        
            foreach (ManagementObject b in
                o.GetRelated("Win32_Service"))
    
                Console.WriteLine(
                    "Service related to the Alerter service {0} is {1}",
                    b["Name"], b["State"]);
        }
    }


    Friday, August 17, 2018 9:40 PM
  • Cannot mark as answer, as the question was not answered. I want the a specific node, not all of them.
    Friday, August 17, 2018 10:50 PM
  • Just get a specific hub by name.

    help Get-WmiObject -online

    Get-WmiObject Win32_UsbHub -Filter "DeviceID='USB\\VID_0451&PID_DD01\\6&16FAF918&1&2'"|
        ForEach-Object{$_.GetRelated('Win32_PnPEntity')} | 
        Select-Object Name, DeviceID

    Thimk!


    \_(ツ)_/


    • Edited by jrv Friday, August 17, 2018 11:16 PM
    • Marked as answer by Sarah Weinberger Friday, August 17, 2018 11:18 PM
    • Unmarked as answer by Sarah Weinberger Saturday, August 18, 2018 12:38 AM
    Friday, August 17, 2018 10:55 PM
  • PS C:\WINDOWS\system32> Get-WmiObject Win32_UsbHub -Filter "DeviceID='USB\VID_0451&PID_DD01\6&16FAF918&1&2'"| ForEach-Object{$_.GetRelated('Win32_PnPEntity')} |  Select-Object Name, DeviceID
    Get-WmiObject : Invalid query "select * from Win32_UsbHub where DeviceID='USB\VID_0451&PID_DD01\6&16FAF918&1&2'"
    At line:1 char:1
    + Get-WmiObject Win32_UsbHub -Filter "DeviceID='USB\VID_0451&PID_DD01\6 ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (:) [Get-WmiObject], ManagementException
        + FullyQualifiedErrorId : GetWMIManagementException,Microsoft.PowerShell.Commands.GetWmiObjectCommand
    Friday, August 17, 2018 11:08 PM
  • Sorry - try it again.


    \_(ツ)_/

    Friday, August 17, 2018 11:17 PM
  • The devices id has to be fixed in WMI because of the WMI escape character.

    $devid = 'USB\VID_0451&PID_DD01\6&16FAF918&1&2' -replace '\\','\\'
    Get-WmiObject Win32_UsbHub -Filter "DeviceID=''"|
        ForEach-Object{$_.GetRelated('Win32_PnPEntity')} | 
        Select-Object Name, DeviceID
    


    \_(ツ)_/

    Friday, August 17, 2018 11:19 PM
  • Actually, that code does not work, either in PS or in C#. I should have noticed it right off the bat, oh well.

    PS C:\WINDOWS\system32> Get-WmiObject Win32_UsbHub -Filter "DeviceID='USB\\VID_0451&PID_DD01\\6&16FAF918&1&2'"|
    >>     ForEach-Object{$_.GetRelated('Win32_PnPEntity')} |
    >>     Select-Object Name, DeviceID
    
    Name                       DeviceID
    ----                       --------
    Generic SuperSpeed USB Hub USB\VID_0451&PID_DD01\6&16FAF918&1&2

    Device, USB\\VID_0451&PID_DD01\\6&16FAF918&1&2, is a USB Hub, onto which I have 2 hard drives plugged into it two of the available ports. As such, the correct answer would have been that the GetRelated call enumerate not the device itself, notice the ID is the same as the parent, but rather there should be my hard drives.

    The system setup is that I plugged in a powered USB hub into a USB outlet on my desktop. Into the hub, I plugged in 2 hard drives. I could have plugged in a many other things, but that is what I have inserted at the moment.

    Saturday, August 18, 2018 12:41 AM
  • Then just get the related for the subdevices.  A PnP device can have multiple subdevices.

    On my system this enumerates the hub devices.  Some hubs may be more complex causing you to enumerate more deply.

    "GetRelated"  is the same as "AssociatorsOf" but easier to use.


    \_(ツ)_/

    Saturday, August 18, 2018 1:18 AM
  • When I do this on most systems I get the disks, printers of other devices.

    Name                    DeviceID
    ----                    --------
    USB Root Hub (USB 3.0)  USB\ROOT_HUB30\4&310E2BCD&0&0
    USB Composite Device    USB\VID_064E&PID_930A\HF1026-T8
    USB Mass Storage Device USB\VID_05DC&PID_0080\_________
    

    Some hubs may require a second level of discovery to get from the "root" to the peripheral.  I have audio/video devices that have three of more child devices like microphone, headphones and command channels.


    \_(ツ)_/

    Saturday, August 18, 2018 1:23 AM
  • I did that, placed the code in a recursive loop, but only 1-device showed.

    public static List<UsbDeviceInfo> GetUsbChildren(String usbPnpDevId)
    {
    	List<UsbDeviceInfo> devices = new List<UsbDeviceInfo>();
    	try
    	{
    		Byte levelDeep = 0;
    		using (ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", $"SELECT * FROM Win32_UsbHub WHERE DeviceID='{usbPnpDevId}'"))
    		{
    			foreach (ManagementObject queryObject in searcher.Get())
    				foreach (ManagementObject oDevice in queryObject.GetRelated("Win32_PnPEntity"))
    					Wmi.GetUsbChildProperties(oDevice, ref devices, ref levelDeep);
    		}
    
    		return devices;
    	}
    
    	catch (Exception)
    	{
    	}
    
    	return devices;
    }
    
    private static Boolean GetUsbChildProperties(ManagementObject oDevice, ref List<UsbDeviceInfo> devices, ref Byte levelDeep)
    {
    	try
    	{
    		string devProperties = string.Empty;
    		foreach (PropertyData item in oDevice.Properties)
    		{
    			devProperties += $"\t{item.Name}";
    			if (null != item.Value)
    				devProperties += $"={item.Value.ToString()}";
    			devProperties += $"{Environment.NewLine}";
    		}
    		devices.Add(new UsbDeviceInfo(
    			(string)oDevice["DeviceID"],
    			(string)oDevice["PNPDeviceID"],
    			(string)oDevice["Description"],
    			devProperties,
    			String.Empty,
    			levelDeep
    		));
    
    		foreach (ManagementObject oDeviceChild in oDevice.GetRelated("Win32_PnPEntity"))
    		{
    			levelDeep++;
    			Wmi.GetUsbChildProperties(oDeviceChild, ref devices, ref levelDeep);
    		}
    
    		return true;
    	}
    
    	catch (Exception)
    	{
    	}
    
    	return false;
    }

    It is not the recursive method, which I originally wanted, but the point is made here. The inner-most-for-loop never has a count of zero.

    Is your second level of discovery not a GetRelated from the 'root' but some sort of new ManagemenObjectSearcher call? I would be open to that, however the PNPDeviceID of the child is exactly the same as the parent.



    Saturday, August 18, 2018 1:25 AM