Prof. Powershell

Can You Namespace That Class?

WMI and PowerShell go together like Batman and Robin. Tap their power together.

There's no doubt that WMI and PowerShell make a formidable pair. A common challenge for PowerShell beginners is discovering what you can do with WMI. Get-WMIObject returns all instances of a given WMI class or those that meet a WMI query. A class is a definition of some thing in your computer. It could be hardware like a motherboard, software like an operating system or some configuration element like a user account. An instance is an actual representation of a class.

These are created by vendors who supplied your hardware and software. All of this information is then stored in a special database or repository, usually referred to as the CIM Repository. The Windows Management Instrumentation (winmgmt) service handles your interaction with this database.

How you can find out the classes on your system? Simple. Ask:

PS C:\> get-wmiobject -list

You'll get a lot of information from the default namespace. Whoa. Namespace? What's that? WMI organizes related classes into namespaces. Often these align with Microsoft products or technologies like DNS, ActiveDirectory or MicrosoftExchangev2. The default namespace for Get-WMIObject is root\CIMv2. The classes that you will be most interested in all begin with win32_ so let's just get that list:

PS C:\> get-wmiobject -list | where {$_.name -match "win32"}

This will be a shorter list. But just because you see a class doesn't mean it is defined on your system.

Now what about the other namespaces? Getting a list of them is a little more complicated so I'll give you a script to use:

#Get-WMINamespace.ps1
Param ([string]$computername=$env:computername)

$namespace="\\$computername\root"
$scope = New-Object system.management.ManagementScope $namespace
$options = New-Object system.management.ObjectGetOptions
$options.UseAmendedQualifiers = $true
$mgmtpath=[Management.ManagementPath]'__Namespace'

$NameSpaces = New-Object System.Management.ManagementClass($scope,$mgmtpath,$options)
$NameSpaces.psbase.getinstances() | foreach { "root\$($_.name)"}

The script defaults to the local computername so all you need to run is:

PS C:\scripts\ .\get-wminamespace.ps1

I'll leave you with something to try on your own:

PS C:\> C:\scripts\posh\Get-WMINamespace.ps1 | foreach {get-wmiobject -namespace $_ -list | where {$_.name -notmatch "__*"} | Sort Name } | out-file c:\wmiclasses.txt

This will create a log of all non-system classes for all namespaces on your computer. Use this list to explore WMI further in PowerShell and put this dynamic duo to work.

About the Author

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, trainer and consultant. Jeff has written for numerous online sites and print publications, is a contributing editor at Petri.com, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular