Prof. Powershell

WMI CIM-plified Part 6: Tips and Tricks

Over the course of the last few lessons we've exploring the new world of the CIM cmdlets introduced in PowerShell 3.0.

Over the course of the last few lessons we've exploring the new world of the CIM cmdlets introduced in PowerShell 3.0. If you've missed any of them, I encourage you to take a few minutes to get caught up. Ready now? In today's lesson I want to offer up some tips and tricks that might come in handy when working with CIM.

One of PowerShell's strength's is discovery and that still applies with respect to the CIM cmdlets. Want to discover all of the Win32* class names in the default namespace, use Get-CimClass.

PS C:\> get-cimclass win32*
Or get a specific class and explore it.
PS C:\> $os = get-cimclass win32_operatingsystem
PS C:\> $os | fl


CimSuperClassName   : CIM_OperatingSystem
CimSuperClass       : ROOT/cimv2:CIM_OperatingSystem
CimClassProperties  : {Caption, Description, InstallDate, Name...}
CimClassQualifiers  : {Locale, UUID, dynamic, provider...}
CimClassMethods     : {Reboot, Shutdown, Win32Shutdown,Win32ShutdownTracker...}
CimSystemProperties : Microsoft.Management.Infrastructure.CimSystemProperties
CimClassName        : Win32_OperatingSystem
Perhaps you want to see the property names:
PS C:\> $

You could get the same information by retrieving an instance of the class and piping to Get-Member. But this is a fast technique. Once you know the property names you can tell PowerShell to only return that information.

PS C:\> get-ciminstance win32_operatingsystem -Property freephysicalmemory


Status                                    :
Name                                      :
FreePhysicalMemory                        : 967844
FreeSpaceInPagingFiles                    :
FreeVirtualMemory                         :

You still get the complete object but only the properties you requested will be populated so the response is quick.

Key Only
In fact, there may be times when you only need to retrieve instances of WMI object by its key property.

PS C:\> get-ciminstance win32_service -KeyOnly | select name


Not sure what property is key for a class? Ask:

PS C:\> (get-cimclass win32_service).cimclassproperties | where {$_.flags -match "key"}

Name               : Name
Value              :
CimType            : String
Flags              : Property, Key, ReadOnly, NullValue
Qualifiers         : {read, key}
ReferenceClassName :

The last tip is to demonstrate how easy it is to find WMI associations using Get-CimAssociatedInstance. I find it easiest to retrieve the object I'm interested in and then pipe it to Get-CimAssociatedInstance.

PS C:\> get-ciminstance win32_share -filter "name='work'" | Get-CimAssociatedInstance

Name             Hidden          Archive         Writeable       LastModified
----             ------          -------         ---------       ------------
c:\work          False           False           True            2/1/2013 8:...

Caption        : Security settings of Work
Description    : Security settings of Work
SettingID      :
ControlFlags   : 32772
Name           : Work
PSComputerName :

It is hard to tell from the output but there are several different types of objects here. One is a Win32_Directory object and the other is a Win32_LogicalShareSecuritySetting. These are referred to as ResultClasses so I can ask for just the class I'm interested in:

PS C:\> get-ciminstance win32_share -filter "name='work'" | Get-CimAssociatedInstance -ResultClass Win32_Directory | fl


Hidden                : False
Archive               : False
EightDotThreeFileName : c:\work
FileSize              :
Name                  : c:\work
Compressed            : False
Encrypted             : False
Readable              : True

Some associations are defined as WMI classes, and if you know the name you can ask for that association as well.

PS C:\> get-ciminstance win32_service -filter "name='spooler'" | Get-CimAssociatedInstance -Association Win32_DependentService

ProcessId    Name             StartMode    State        Status      ExitCode
---------    ----             ---------    -----        ------      --------
724          RpcSs            Auto         Running      OK          0

DisplayName : HTTP Service
Name        : HTTP
State       : Running
Status      : OK
Started     : True

0            Fax              Disabled     Stopped      OK          1077

Here I retrieved the Win32_DependentService class with shows associated services that have some sort of dependency relationship with the Spooler service. Want to find these association classes? Ask:

PS C:\> get-cimclass win32* | where {$ -contains "association"} | sort CimClassname

I'll admit this is an advanced WMI topic which I'm not going to dive into but if you need to get this type of information I think you'll find the CIM cmdlets pretty easy to use.

Ultimately, the best way to understand all of this CIM magic is to open a PowerShell 3.0 prompt and give them a spin.

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, and a frequent speaker at technology conferences and user groups.

comments powered by Disqus
Most   Popular