Welcome to MSDN Blogs Sign in | Join | Help

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

Published Saturday, January 10, 2009 9:54 AM by PowerShellTeam
Filed under:

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: Get-USB – Using WMI Association Classes in PowerShell

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

Saturday, January 10, 2009 8:32 PM by PowerShellTeam

# re: Get-USB – Using WMI Association Classes in PowerShell

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

Monday, January 12, 2009 1:11 PM by Zach Thieme

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker