header image

Migrating to CIM–part 1

Posted by: | March 10, 2012 | No Comment |

I have created many WMI posts on this blog over the years. With PowerShell v3 a new API has been introduced and new cmdlets are available for working with WMI. The new functionality offers many improvements and advantages over using the WMI cmdlets so I thought that a series of articles showing the evolution of a piece of WMI based PowerShell might be useful.

Much of our work with WMI is based on retrieving data e.g.

Get-WmiObject -Class Win32_ComputerSystem

In PowerShell v3 this could become

Get-CimInstance -ClassName Win32_ComputerSystem


OK that seems simple but what about using the WMI type accelerators

$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
$reg = [wmiclass]'\\.\root\default:StdRegprov'            
$subkeys = $reg.EnumKey($HKLM, $key)            

These don’t exist in the CIM cmdlets.  What we do have is Invoke-CimMethod so we can do this

[uint32]$HKLM = 2147483650            
$key = "SYSTEM\CurrentControlSet\Services"            
Invoke-CimMethod -Namespace "root\default" -ClassName StdRegProv -MethodName EnumKey  -Arguments @{hDefKey=$HKLM; sSubKeyName =$key} |            
select -ExpandProperty sNames

There are a few important points we need to note:

  • The value to define the hive has to be an unsigned integer
  • we have to supply the correct argument name
  • The arguments are provided in a hash table
  • ClassName is used instead of class
  • The results are a collection that we have to expand

So how do we find this information – see part 2

under: PowerShell and WMI, PowerShell V3