{"id":12281,"date":"2011-10-26T00:01:00","date_gmt":"2011-10-26T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/26\/use-a-powershell-function-to-get-wmi-key-property-values\/"},"modified":"2011-10-26T00:01:00","modified_gmt":"2011-10-26T00:01:00","slug":"use-a-powershell-function-to-get-wmi-key-property-values","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-a-powershell-function-to-get-wmi-key-property-values\/","title":{"rendered":"Use a PowerShell Function to Get WMI Key Property Values"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Microsoft Scripting Guy Ed Wilson discusses his version 3 of a Windows PowerShell WMI helper module.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Microsoft Scripting Guy Ed Wilson here. I have really enjoyed working on my HSGWMIModule project this week. On <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/24\/a-powershell-wmi-helper-module-described.aspx\"><span style=\"color: #0000ff\">Monday<\/span><\/a><\/span><span style=\"font-family: Segoe\">, I created the base WMI module, and included a couple of really great functions that query the WMI schema and return WMI class methods and properties. On <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/25\/use-powershell-to-easily-find-the-key-property-of-a-wmi-class.aspx\"><span style=\"color: #0000ff\">Tuesday<\/span><\/a><\/span><span style=\"font-family: Segoe\">, I added a function that returns the key property of a WMI class. This is useful when working WMI instance methods. Today, I create a function that will return the values of those key properties. There are times when I only need to know the key property of a WMI class, and there are other times when I want to see the path to all the possible instances of a WMI class. Today&rsquo;s function addresses these situations. <\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Note<\/span><\/b><span style=\"font-family: Segoe\">&nbsp;&nbsp; <\/span><\/span>As I did in the two previous Hey, Scripting Guy! Blog posts, I have uploaded the current iteration, version 3, of the WMI module to the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/WMI-helper-module-version-3-b72b9e5e\"><span style=\"color: #0000ff\">Scripting Guys Script Repository<\/span><\/a><\/span><span style=\"font-family: Segoe\">. <\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">To use <b>HSGWMImoduleV3<\/b>, I first must &ldquo;install&rdquo; it. Installation is simple; I use my <b>Copy-Modules<\/b> function from the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Windows-PowerShell-ISE-ff235827\"><span style=\"color: #0000ff\">Windows PowerShell ISE Profile and Modules project<\/span><\/a><\/span><span style=\"font-family: Segoe\"> I wrote. I discussed this on <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/21\/create-a-really-cool-powershell-ise-profile.aspx\"><span style=\"color: #0000ff\">Friday, October 21, 2011<\/span><\/a><\/span><span style=\"font-family: Segoe\">. <\/span><\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Note<\/span><\/b><span style=\"font-family: Segoe\">&nbsp;&nbsp; If you need more information about Windows PowerShell modules, see <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/tags\/windows+powershell\/modules\/\"><span style=\"color: #0000ff\">this collection of Hey, Scripting Guy! posts<\/span><\/a><\/span><span style=\"font-family: Segoe\">.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After you have installed the module, import the module by using the <b>Import-Module<\/b> cmdlet. It is not necessary to use the complete module name; all that is required is to use wildcard characters that uniquely identify the module name. I use the command seen here to load the module into my Windows PowerShell ISE:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Import-Module hsg*wmi*3<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After it&#8217;s loaded (imported), all the functions contained in the module load onto the function drive. As shown in the following figure, I use the <b>Get-WMIKeyvalue<\/b> function to find the path to instances of the <b>Win32_Process<\/b> WMI class. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-10-26-11-1.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of using Get-WMIKeyvalue function\" alt=\"Image of using Get-WMIKeyvalue function\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3225.hsg-10-26-11-1.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Most of the lines in the <b>Get-WMIKeyValue<\/b> function are comment-based help. Therefore, when I use the <b>Get-Help<\/b> cmdlet, as shown in the following figure, nicely formatted help is displayed in the output pane. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1348.hsg-10-26-11-2.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of using Get-Help cmdlet\" alt=\"Image of using Get-Help cmdlet\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/1348.hsg-10-26-11-2.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The main portion of the <b>Get-WMIKeyValue<\/b> function consists of three parameters, a <b>Get-WMIObject<\/b> command, and a <b>Select-Object<\/b> command: <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Param(<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp; [Parameter(Mandatory=$true)]<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp; [string]$class,<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp; [string]$computername = $env:COMPUTERNAME,<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp;&nbsp; [string]$namespace = &#8220;root\\cimv2&#8221;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp; Get-WmiObject -Class $class -ComputerName $computername -Namespace $namespace | <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp; Select __PATH<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">} #end function get-WmiKeyvalue<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I call the <b>Get-WMIKeyValue<\/b> function by passing at least the WMI class name. The <b>Get-WmiObject<\/b> cmdlet retrieves the class, and pipes the management object to the <b>Select-Object<\/b> cmdlet where I choose the <b>__Path<\/b> system property. The <b>__Path<\/b> system property returns to the caller of the function. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Suppose I want to use the <b>Invoke-WMIMethod<\/b> cmdlet to terminate an instance of Notepad.exe. I use the <b>Get-WMiKeyValue<\/b> function to return instances of the <b>Win32_Process<\/b> WMI class. This command is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WmiKeyvalue win32_process<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I use the <b>Get-Process<\/b> cmdlet to ensure I get the right instance. The value in the ID property returned by <b>Get-Process<\/b> matches the value in the <b>handle<\/b><i> <\/i>WMI property of the <b>Win32_Process<\/b> class. I copy the path, and paste it into the <b>path<\/b><i> <\/i>property of the <b>Invoke-WMIMethod<\/b> class. I then call the <b>terminate<\/b><i> <\/i>method. The <b>Invoke-WMIMethod<\/b> command is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Invoke-WmiMethod -Path &#8216;\\\\MRED\\root\\cimv2:Win32_Process.Handle=&#8221;4652&#8243;&#8216; -name terminate<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The commands and associated output are shown in the following figure.<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5672.hsg-10-26-11-3.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of commands and associated output\" alt=\"Image of commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/5672.hsg-10-26-11-3.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Well, that is about all there is to the <b>Get-WmiKeyValue<\/b> function. Download version 3 of the WMI helper function module from the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/WMI-helper-module-version-3-b72b9e5e\"><span style=\"color: #0000ff\">Scripting Guys Script Repository<\/span><\/a><\/span><span style=\"font-family: Segoe\">, and let me know what you think. If there are things you would like to see, just let me know. See you tomorrow. <\/span><\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"color: #0000ff\">Twitter<\/span><\/a><\/span><span style=\"font-family: Segoe\"> and <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingguysfacebook\"><span style=\"color: #0000ff\">Facebook<\/span><\/a><\/span><span style=\"font-family: Segoe\"><span style=\"font-family: arial,helvetica,sans-serif\">.<\/span> If you have any questions, send email to me at <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"color: #0000ff\">scripter@microsoft.com<\/span><\/a><\/span><span style=\"font-family: Segoe\">, or post your questions on the <\/span><span style=\"font-family: arial,helvetica,sans-serif\"><a href=\"http:\/\/bit.ly\/scriptingforum\" target=\"_blank\"><span style=\"color: #0000ff\">Official Scripting Guys Forum<\/span><\/a><\/span><span style=\"font-family: Segoe\">. See you tomorrow. Until then, peace.<\/span><\/span><\/p>\n<\/p>\n<p><span style=\"font-size: small\"><b>Ed Wilson, Microsoft Scripting Guy<\/b><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\"><\/span>&nbsp;<\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy Ed Wilson discusses his version 3 of a Windows PowerShell WMI helper module. &nbsp; Microsoft Scripting Guy Ed Wilson here. I have really enjoyed working on my HSGWMIModule project this week. On Monday, I created the base WMI module, and included a couple of really great functions that query the WMI [&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-12281","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: Microsoft Scripting Guy Ed Wilson discusses his version 3 of a Windows PowerShell WMI helper module. &nbsp; Microsoft Scripting Guy Ed Wilson here. I have really enjoyed working on my HSGWMIModule project this week. On Monday, I created the base WMI module, and included a couple of really great functions that query the WMI [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12281","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=12281"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12281\/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=12281"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12281"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12281"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}