{"id":12261,"date":"2011-10-28T00:01:00","date_gmt":"2011-10-28T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2011\/10\/28\/query-wmi-classes-by-using-powershell-and-wildcard-patterns\/"},"modified":"2011-10-28T00:01:00","modified_gmt":"2011-10-28T00:01:00","slug":"query-wmi-classes-by-using-powershell-and-wildcard-patterns","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/query-wmi-classes-by-using-powershell-and-wildcard-patterns\/","title":{"rendered":"Query WMI Classes by Using PowerShell and Wildcard Patterns"},"content":{"rendered":"<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><strong>Summary:<\/strong> Learn how to query WMI classes using Windows PowerShell and wildcard character patterns by using a free Scripting Guy module.<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are finally settling down to a normal routine here in Charlotte, North Carolina, in the United States. Of course, that settling down will not last too long because next week we head out to California. I am speaking at a Microsoft internal conference for the premier field engineers (PFE) at a conference called Geek Week (Geek Ready). I am making two presentations to each of the three sessions, as well as sitting on a panel discussion. In addition, for session 1 I am having a special side meeting with the PFE Windows PowerShell special interest group. It will be lots of fun. Following that week, we head up to Orange County, California, where I will be conducting a special five day Windows PowerShell workshop for Microsoft Premier customers. So we will spend two weeks in sunny Southern California. Ah, the life of someone who knows Windows PowerShell.<\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">Anyway, I do need to mention something that happened to me on Monday. I changed the BIOS settings on my new laptop, and when I saved the changes, <\/span><a href=\"http:\/\/technet.microsoft.com\/en-us\/library\/cc766015(WS.10).aspx\"><span style=\"font-family: Segoe\">BitLocker Recovery Password Viewer<\/span><\/a><span style=\"font-family: Segoe\"> for Active Directory Users and Computers. A quick call to the help desk, and I was back up and running. It was cool!<\/span><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I am continuing to work on my <b>HSGWMIModule<\/b> and today I am up to version 5. <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><span style=\"font-family: Segoe\"><b>Note<\/b>&nbsp; &nbsp;This is part five of a multipart article on developing a WMI helper module. <\/span>On <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/24\/a-powershell-wmi-helper-module-described.aspx\"><span>Monday<\/span><\/a>, 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 <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>Tuesday<\/span><\/a>, I added a function that returns the key property of a WMI class. This is useful when working with WMI instance methods. On <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/26\/use-a-powershell-function-to-get-wmi-key-property-values.aspx\"><span>Wednesday<\/span><\/a>, I created a function that will return the values of those key properties. <a href=\"http:\/\/blogs.technet.com\/b\/heyscriptingguy\/archive\/2011\/10\/28\/query-wmi-classes-by-using-powershell-and-wildcard-patterns.aspx\"><span>Yesterday<\/span><\/a>, I added a <b>HasWMIValue<\/b> filter to the module.<span style=\"font-family: Segoe\"> Today, I add two functions. The first is a function that will query WMI classes using a wildcard character pattern. The second function is an <b>Out-TempFile<\/b> function that accepts piped input and displays results in a temporary text file in Notepad.&nbsp;<\/span><\/span><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-size: small\"><b><span style=\"font-family: Segoe\">Anoter Note<\/span><\/b><span style=\"font-family: Segoe\"> &nbsp;&nbsp;The module for today&rsquo;s blog post is on the <\/span><a href=\"http:\/\/gallery.technet.microsoft.com\/scriptcenter\/Scripting-Guys-WMI-helper-bf3cc6d6\"><span style=\"font-family: Segoe\">Copy-Modules function from my Windows PowerShell ISE module<\/span><\/a><span style=\"font-family: Segoe\">. Using this technique, all you need to do is go to the Scripting Guys Script Repository, copy the module to the Clipboard, save it in your scratch directory, and use the <b>Copy-Modules<\/b> function to copy it to your modules folder. If your scratch directory is c:\\fso, it is completely automatic. If it is not, I recommend you edit the <b>Copy-Modules<\/b> function to use your scratch directory, which makes it really easy to use. The command and associated output are shown in the following figure.<\/span><\/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\/6523.hsg-10-28-11-01.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of command and associated output\" alt=\"Image of command and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/6523.hsg-10-28-11-01.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">When the <b>HSGWMIModuleV5<\/b> is installed, all of the functions and filters contained within the module become available when I import the module. I can do this in either the Windows PowerShell console or the Windows PowerShell ISE. I am going to use the Windows PowerShell console for now. I do not need to type the entire module name because the <b>Import-Module<\/b> cmdlet accepts wildcard characters. After I import the module for the first time, I like to see which commands the module supports, so I use the <b>Get-Command<\/b> cmdlet to display the commands. The two commands are shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Import-Module hsg*5<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-Command -Module hsg*5<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The two commands and their 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\/2705.hsg-10-28-11-02.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of two commands and associated output\" alt=\"Image of two commands and associated output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2705.hsg-10-28-11-02.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The <b>Get-WmiClassesAndQuery<\/b> function accepts wildcard characters for the WMI class name. It then searches for WMI classes that match the pattern. Upon finding the classes, it determines which WMI classes are not abstract and it queries them. The portion of the function that returns a list of WMI classes that match a wildcard character pattern is shown here (supply the wildcard character pattern to the <b>$class<\/b> variable):<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WmiObject -List $class -Namespace $namespace -ComputerName $computer<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I use the same logic to determine dynamic classes I used in other functions in the <b>HSGWMIModule*<\/b>. I examine the class qualifiers to find the <b>abstract<\/b> qualifier. If the <b>abstract<\/b><i> <\/i>qualifier appears, I skip the class. Here is that portion of the logic:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">ForEach-Object { <\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp; $abstract = $false<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp; [wmiclass]$class = &#8220;\\\\{0}\\{1}:{2}&#8221; -f $computer,$namespace,$_.name<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp; Foreach($q in $_.Qualifiers)<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp;&nbsp; { if ($q.name -eq &#8216;Abstract&#8217;) {$abstract = $true} }<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">&nbsp; If(!$abstract)<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">After all that work, it is a straightforward WMI query using the <b>Get-WMIObject<\/b> cmdlet. I used parameter decorations to make the <i>class <\/i>parameter mandatory in the first position, and accept pipeline input. Here is that parameter attribute:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">[Parameter(Mandatory=$true,Position=0,valueFromPipeline=$true)]<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">I can use the function by directly passing a wildcard character to it, or I can use the parameters. These techniques are shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WmiClassesAndQuery *disk*<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WmiClassesAndQuery -class *disk* -namespace root\\wmi<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">To run a query remotely you must have administrator rights. If you run the command with alternate credentials, you will need to supply the path to the module when you import it because the <b>Copy-Modules<\/b> function installs into the current user profile location. Of course, if you run the Windows PowerShell ISE with alternate credentials when you use the <b>Copy-Modules<\/b> function to perform the installation, you will already have the module in your alternate profile. The workaround is to manually install the module in the all users\/all hosts profile location. <\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The <b>Get-WmiClassesAndQuery<\/b> function really shines when combined with the <b>HasWmiValue<\/b> filter and the <b>Out-TempFile<\/b> function. The command is shown here:<\/span><\/p>\n<p style=\"padding-left: 30px\"><span style=\"font-family: Segoe;font-size: small\">Get-WmiClassesAndQuery *adapter* | HasWmiValue | Out-TempFile<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">The previous command queries every dynamic WMI class that contains the pattern <b>adapter<\/b><i> <\/i>in the name. This includes <b>Win32_NetworkAdapter<\/b> and <b>Win32_NetworkAdapterConfiguration<\/b> as well as an associative class. The <b>HasWmiValue<\/b> greatly reduces the amount of information that is displayed by removing empty properties, and the <b>Out-TempFile<\/b> function makes it easy to peruse the returned data. The temp file is 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\/4682.hsg-10-28-11-03.png\"><img decoding=\"async\" style=\"border: 0px\" title=\"Image of temp file\" alt=\"Image of temp file\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/4682.hsg-10-28-11-03.png\" \/><\/a><\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">&nbsp;<\/span><\/p>\n<p><span style=\"font-family: Segoe;font-size: small\">That is all there is to using the <b>HSGWmiModuleV5<\/b>. WMI Week will continue tomorrow when I will add some aliases and other things to the <b>HSGWmiModule<\/b>. Until tomorrow! <\/span><\/p>\n<p><span style=\"font-size: small\"><span style=\"font-family: Segoe\">I invite you to follow me on <\/span><a href=\"http:\/\/bit.ly\/scriptingguystwitter\" target=\"_blank\"><span style=\"font-family: Segoe\">Facebook<\/span><\/a><span style=\"font-family: Segoe\">. If you have any questions, send email to me at <\/span><a href=\"mailto:scripter@microsoft.com\" target=\"_blank\"><span style=\"font-family: Segoe\">Official Scripting Guys Forum<\/span><\/a><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: Learn how to query WMI classes using Windows PowerShell and wildcard character patterns by using a free Scripting Guy module. &nbsp; Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are finally settling down to a normal routine here in Charlotte, North Carolina, in the United States. Of course, that settling [&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-12261","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 query WMI classes using Windows PowerShell and wildcard character patterns by using a free Scripting Guy module. &nbsp; Microsoft Scripting Guy Ed Wilson here. Well, the Scripting Wife and I are finally settling down to a normal routine here in Charlotte, North Carolina, in the United States. Of course, that settling [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12261","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=12261"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/12261\/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=12261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=12261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=12261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}