Use PowerShell to Easily Find the Key Property of a WMI Class

Summary: Learn how to find the key property of a WMI class by using a Windows PowerShell function.


Microsoft Scripting Guy Ed Wilson here. Well, I received a decent amount of feedback about version 1 of my HSGWMIhelper Windows PowerShell module. Today, I want to add a needed feature to the module: I need the ability to find a WMI key property from a WMI class. I added the Get-WMIKey function to the Scripting Guys Script Repository.

One thing I often need is to be able to do is find the key property of a WMI class. For example, after I have imported my HSGWMImoduleV2 module, I can use the Get-WmiKey function to retrieve the key property from the Win32_Process WMI class. The handle property is the key property from the class. This is shown here:

PS C:\> Import-Module hsg*v2

PS C:\> Get-WmiKey win32_process

I now start an instance of Notepad, and use the Get-Process cmdlet to retrieve information about the Notepad process as shown here:

PS C:\> notepad

PS C:\> Get-Process notepad


Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName

-------  ------    -----      ----- -----   ------     -- -----------

     78       9     4272       8948    86     0.03   6872 notepad

I can then use the id property value (equals the handle property from WMI) with the [WMI] instance accelerator to retrieve that specific instance of the Win32_Process class. The following is the syntax for this command:


The command and associated output are shown in the following figure.

Image of command and associated output

One thing that is great about the WMI instance accelerator is that instance methods are immediately available. This technique is shown in the following code:


The command and associated output are shown in the following figure.

Image of command and associated output

In the Get-WmiKey function, the first thing I do is create help by using comment-based help (all the functions in my HSGWMIModuleV2 module have comment-based help). The help portion of the function is shown here.

function Get-WmiKey




    This function returns the key property of a WMI class


    This function returns the key property of a WMI class


    Get-WMIKey win32_bios

    Returns the key properties for the Win32_bios WMI class in root\ciimv2


    Get-WmiKey -class Win32_product

    Returns the key properties for the Win32_Product WMI class in root\cimv2


    Get-WmiKey -class systemrestore -namespace root\default

    Gets the key property from the systemrestore WMI class in the root\default

    WMI namespace.

   .Parameter Class

    The name of the WMI class

   .Parameter Namespace

    The name of the WMI namespace. Defaults to root\cimv2

   .Parameter Computer

    The name of the computer. Defaults to local computer


    NAME:  Get-WMIKey

    AUTHOR: ed wilson, msft

    LASTEDIT: 10/18/2011 17:38:20

    KEYWORDS: Scripting Techniques, WMI

    HSG: HSG-10-24-2011



 #Requires -Version 2.0


Next, I create the input parameters. I use parameter attributes to make the class parameter mandatory, and I assign it to the first position. The remainder of the parameters use techniques I have written about in the past.


   [Parameter(Mandatory = $true,Position = 0)]


   [string]$namespace = "root\cimv2",

   [string]$computer = $env:computername


Now, I do something pretty cool (I use this technique in other functions in the HSGWmiModuleV2 module). I cast the string in the $class variable to be an instance of a management object. I use parameter substitution to put together a complete path: computer name, WMI namespace, and WMI class name:

[wmiclass]$class = "\\{0}\{1}:{2}" -f $computer,$namespace,$class

Now I obtain a collection of the properties of the class, and I expand the qualifiers associated with each property. I then look for the name that is equal to “key” and I print out the property names. It is important to note that some WMI classes have multiple keys, such as Win32_product. This portion of the function is shown here:

  $class.Properties |

      Select-object @{Name="PropertyName";Expression={$}} `

        -ExpandProperty Qualifiers |

      Where-object {$_.Name -eq "key"} |

      ForEach-Object {$_.PropertyName}

} #end GetWmiKey


I included the Get-WMiKey function in the second version of my HSGWmiModuleV2 module. You will find the complete module on the Scripting Guys Script Repository.


That’s it for now. See you tomorrow when I will add another really cool WMI function to my WMI module.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy


Comments (1)

  1. Klaus Schulte says:

    Hi Ed,

    this maybe another important aspect of WMI … a kind of "key experience" 🙂

    But: I don't know if you are like me and not so deeply involved in the world of WMI Qualifiers …

    The first question I had to answer for myself is: What is the meaning of the "key" qualifier = why should I bother?

    Well, it looks like I'm going to find a kind of "primary key" ( Database jargon ) property that unambiguously identifies an instance of a special class needed to operate on this very special instance and no other similar , maybe even equaly named, instance. So if I want to distinguish my 10 running notepad.exe processes, I have to find the key property "handle" and use it to address a special notepad instance.

    Other questions I had: "Can there be more than one key for a class?" ( YES, similar to primary indexes in DB systems )

    "Are there classes without a key?" ( YES … e.g. Singletons )

    So I concluded that the key property maybe something, I might want to know in cases where many instances of a class have been created.


Skip to main content