{"id":12291,"date":"2011-10-25T00:01:00","date_gmt":"2011-10-25T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/25\/use-powershell-to-easily-find-the-key-property-of-a-wmi-class\/"},"modified":"2011-10-25T00:01:00","modified_gmt":"2011-10-25T00:01:00","slug":"use-powershell-to-easily-find-the-key-property-of-a-wmi-class","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-easily-find-the-key-property-of-a-wmi-class\/","title":{"rendered":"Use PowerShell to Easily Find the Key Property of a WMI Class"},"content":{"rendered":"<p><strong>Summary:<\/strong> Learn how to find the key property of a WMI class by using a Windows PowerShell function.<\/p>\n<p>&nbsp;<\/p>\n<p>Microsoft Scripting Guy Ed Wilson here. Well, I received a decent amount of feedback about version 1 of my <b>HSGWMIhelper<\/b> 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 <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/WMI-Helper-Module-V2-3d598505\">Get-WMIKey function to the Scripting Guys Script Repository<\/a>.<\/p>\n<p>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 <b>HSGWMImoduleV2<\/b> module, I can use the <b>Get-WmiKey<\/b> function to retrieve the key property from the <b>Win32_Process<\/b> WMI class. The <b>handle<\/b><i> <\/i>property is the key property from the class. This is shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Import-Module hsg*v2<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-WmiKey win32_process<\/p>\n<p>I now start an instance of Notepad, and use the <b>Get-Process<\/b> cmdlet to retrieve information about the Notepad process as shown here:<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; notepad<\/p>\n<p style=\"padding-left: 30px\">PS C:\\&gt; Get-Process notepad<\/p>\n<p style=\"padding-left: 30px\">&nbsp;<\/p>\n<p style=\"padding-left: 30px\">Handles&nbsp; <span class=\"Apple-tab-span\"> <\/span>NPM(K)&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>PM(K)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>WS(K) <span class=\"Apple-tab-span\"> <\/span>VM(M)&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>CPU(s)&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>Id <span class=\"Apple-tab-span\"> <\/span>ProcessName<\/p>\n<p style=\"padding-left: 30px\">&#8212;&#8212;-&nbsp; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8212;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8211;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8211; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8211;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8212;&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>&#8212; <span class=\"Apple-tab-span\"> <\/span>&#8212;&#8212;&#8212;&#8211;<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; 78&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>9&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>4272&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=\"Apple-tab-span\"> <\/span> 8948&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>86&nbsp;&nbsp;&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>0.03&nbsp;&nbsp; <span class=\"Apple-tab-span\"> <\/span>6872 <span class=\"Apple-tab-span\"> <\/span>notepad<\/p>\n<\/p>\n<p>I can then use the <b>id<\/b><i> <\/i>property value (equals the <b>handle<\/b><i> <\/i>property from WMI) with the <b>[WMI]<\/b> instance accelerator to retrieve that specific instance of the <b>Win32_Process<\/b> class. The following is the syntax for this command:<\/p>\n<p style=\"padding-left: 30px\">[wmi]&#8221;win32_process.handle=6872&#8243;<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4477.hsg-10-25-11-1.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4477.hsg-10-25-11-1.png\" alt=\"Image of command and associated output\" title=\"Image of command and associated output\" \/><\/a><\/p>\n<p>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:<\/p>\n<p style=\"padding-left: 30px\">([wmi]&#8221;win32_process.handle=6872&#8243;).terminate()<\/p>\n<p>The command and associated output are shown in the following figure.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6735.hsg-10-25-11-2.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6735.hsg-10-25-11-2.png\" alt=\"Image of command and associated output\" title=\"Image of command and associated output\" \/><\/a><\/p>\n<p>In the <b>Get-WmiKey<\/b> function, the first thing I do is create help by using comment-based help (all the functions in my <b>HSGWMIModuleV2<\/b> module have comment-based help). The help portion of the function is shown here.<\/p>\n<p style=\"padding-left: 30px\">function Get-WmiKey<\/p>\n<p style=\"padding-left: 30px\">{<\/p>\n<p style=\"padding-left: 30px\">&nbsp; &lt;#<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Synopsis<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function returns the key property of a WMI class<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Description<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; This function returns the key property of a WMI class<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-WMIKey win32_bios<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Returns the key properties for the Win32_bios WMI class in root\\ciimv2<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-WmiKey -class Win32_product<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Returns the key properties for the Win32_Product WMI class in root\\cimv2<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Example<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Get-WmiKey -class systemrestore -namespace root\\default<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; Gets the key property from the systemrestore WMI class in the root\\default<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; WMI namespace.<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter Class<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The name of the WMI class<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter Namespace<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The name of the WMI namespace. Defaults to root\\cimv2<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Parameter Computer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; The name of the computer. Defaults to local computer<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Notes<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; NAME:&nbsp; Get-WMIKey<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; AUTHOR: ed wilson, msft<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; LASTEDIT: 10\/18\/2011 17:38:20<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; KEYWORDS: Scripting Techniques, WMI<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp; HSG: HSG-10-24-2011<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; .Link<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp; Http:\/\/www.ScriptingGuys.com<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#Requires -Version 2.0<\/p>\n<p style=\"padding-left: 30px\">&nbsp;#&gt;<\/p>\n<p>Next, I create the input parameters. I use parameter attributes to make the <i>class<\/i> parameter mandatory, and I assign it to the first position. The remainder of the parameters use techniques I have written about in the past.<\/p>\n<p style=\"padding-left: 30px\">Param(<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; [Parameter(Mandatory = $true,Position = 0)]<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; [string]$class,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; [string]$namespace = &#8220;root\\cimv2&#8221;,<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp; [string]$computer = $env:computername<\/p>\n<p style=\"padding-left: 30px\">&nbsp;)<\/p>\n<p>Now, I do something pretty cool (I use this technique in other functions in the <b>HSGWmiModuleV2<\/b> module). I cast the string in the <b>$class<\/b> 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:<\/p>\n<p style=\"padding-left: 30px\">[wmiclass]$class = &#8220;\\\\{0}\\{1}:{2}&#8221; -f $computer,$namespace,$class<\/p>\n<p>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 &ldquo;key&rdquo; and I print out the property names. It is important to note that some WMI classes have multiple keys, such as <b>Win32_product<\/b>. This portion of the function is shown here:<\/p>\n<p style=\"padding-left: 30px\">&nbsp; $class.Properties |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select-object @{Name=&#8221;PropertyName&#8221;;Expression={$_.name}} `<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -ExpandProperty Qualifiers |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Where-object {$_.Name -eq &#8220;key&#8221;} |<\/p>\n<p style=\"padding-left: 30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ForEach-Object {$_.PropertyName}<\/p>\n<p style=\"padding-left: 30px\">} #end GetWmiKey<\/p>\n<p>&nbsp;<\/p>\n<p>I included the <b>Get-WMiKey<\/b> function in the second version of my <b>HSGWmiModuleV2<\/b> module. You will find the complete module on the <a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/WMI-Helper-Module-V2-3d598505\">Scripting Guys Script Repository<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<p>That&rsquo;s it for now. See you tomorrow when I will add another really cool WMI function to my WMI module.<\/p>\n<p>I invite you to follow me on <a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\">Twitter<\/a> and <a href=\"http:\/\/bit.ly\/scriptingguysfacebook\">Facebook<\/a>. If you have any questions, send email to me at <a href=\"mailto:scripter@microsoft.com\" target=\"_blank\">scripter@microsoft.com<\/a>, or post your questions on the <a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\">Official Scripting Guys Forum<\/a>. See you tomorrow. Until then, peace.<\/p>\n<\/p>\n<p><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/p>\n<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Learn how to find the key property of a WMI class by using a Windows PowerShell function. &nbsp; 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 [&hellip;]<\/p>\n","protected":false},"author":596,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[3,4,45,6],"class_list":["post-12291","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell","tag-wmi"],"acf":[],"blog_post_summary":"<p>Summary: Learn how to find the key property of a WMI class by using a Windows PowerShell function. &nbsp; 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 [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12291","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/596"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=12291"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12291\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=12291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}