Get-USB – Using WMI Association Classes in PowerShell

Get-USB – Using WMI Association Classes in PowerShell

  • Comments 3

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

Leave a Comment
  • Please add 7 and 4 and type the answer here:
  • Post
  • This is a pretty good command sequence:

    get-usb |sort service,caption |ft -group service caption,man*,desc* -auto

    Enjoy!

    Jeffrey Snover [MSFT]

    Windows Management Partner Architect

    Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell

    Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

  • Hi - i'm interested in joining the script club.

  • Hi, I need to lock  and unlock USB ports... is possible with power shell?

Page 1 of 1 (3 items)