Tag: USB

  • PowerShell knows about your USB devices

    One of many things you can do with the Management assembly is to get the USB devices that is currently connected to your computer. To load the Management assembly, use this line of code (no line break here):

    [System.Reflection.Assembly]::Load("System.Management, Version=2.0.0.0,
    Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")

    The class you want to create an instance from is called ManagementClass, and to its constructor you pass the management path that you want to use. USB devices are located in the Win32_USBHub path, so that is the argument that I pass to the constructor.

    $Man = New-Object System.Management.ManagementClass("Win32_USBHub")

    The GetInstances function returns a collection of objects, each object representing a device that actually is connected. Just for being crazy, I will make my target variable type safe.

    [System.Management.ManagementObjectCollection]$Devs = $Man.GetInstances()

    Now I can use ForEach-Obect cmdlet to iterate through the collection. For each device, I will write out the name of the device, the system ID and a blank line.

    $Devs | ForEach-Object {
    	Write-Output $_.GetPropertyValue("Name").ToString()
    	Write-Output $_.GetPropertyValue("DeviceID").ToString()
    	Write-Output ""
    }

    Finally, I have to free the resouces I have used.

    $Devs.Dispose()
    $Man.Dispose()

    When you run this code, you should see a list of devices that is registered as connected to your machine, including the USB hubs. If you connect a device and run the script again, that device will show up in the list. With this code as base, you could easily write programs that detects changes to the list. That is, programs that detects when a new device is added or removed. The last item should be the device you inserted while your computer was running, and the first items are the devices that were available at system startup.

  • Keep track on USB devices

    Microsoft provides a library for talking to USB devices as part of the .NET Framework. This is just one thing you can do with the versatile System.Management namespace. If you want to keep track on the units that are connected or removed from a computer, you can use this library for regular checks. This code, lists the name and ID of all connected devises. The code requires a reference to the System.Management namespace:

    Using Mn As New System.Management.ManagementClass("Win32_USBHub")
       Using Devs As System.Management.ManagementObjectCollection _
          = Mn.GetInstances()
          For Each Dev As System.Management.ManagementObject In Devs
             Console.WriteLine(Dev.GetPropertyValue("Name").ToString())
             Console.WriteLine(Dev.GetPropertyValue("DeviceID").ToString())
          Next
       End Using
    End Using

    My code is written in an console application. The result will be a list of connected USB device names and their unique ID. Have fun!