Get-USB – Using WMI Association Classes in PowerShell

Last Thursday, we had our first meeting of PowerShell Script Club on the Microsoft Campus. Script Clubs are really cool. They’re kind of like a hands on lab with no set topic or teacher. You bring an idea for a script, and ask your fellow PowerShell users about help getting the script written.  Leave a comment if you’d like to set up a local script club.

One of the questions I helped answer involved using a WMI association class, which is a class in WMI that links two other classes together. Association classes are pretty common, and pretty useful, once you get the hang of them.

One association class is Win32_USBControllerDevice. Here’s a quick function that resolves the association class and returns all USB devices from WMI. The function is one line, and the inline help is 8 lines.

Synopsis:

Gets USB devices attached to the system

Detailed Description:

Uses WMI to get the USB Devices attached to the system

Examples:

    -------------------------- EXAMPLE 1 --------------------------
Get-USB    
    -------------------------- EXAMPLE 2 --------------------------
Get-USB | Group-Object Manufacturer

Here’s Get-USB:

function Get-USB {
           
    #.Synopsis
    #    Gets USB devices attached to the system
    #.Description
    #    Uses WMI to get the USB Devices attached to the system
    #.Example
    #    Get-USB
    #.Example
    #    Get-USB | Group-Object Manufacturer  
    Get-WmiObject Win32_USBControllerDevice | Foreach-Object { [Wmi]$_.Dependent }

}
    

Automatically generated with Write-CommandBlogPost