{"id":2479,"date":"2013-12-05T00:01:00","date_gmt":"2013-12-05T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2013\/12\/05\/getting-started-with-powershell-the-basics-of-wmi\/"},"modified":"2013-12-05T00:01:00","modified_gmt":"2013-12-05T00:01:00","slug":"getting-started-with-powershell-the-basics-of-wmi","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/getting-started-with-powershell-the-basics-of-wmi\/","title":{"rendered":"Getting Started with PowerShell: The Basics of WMI"},"content":{"rendered":"<p><strong>Summary<\/strong>: Microsoft Scripting Guy, Ed Wilson, talks about querying WMI in this excerpt of his book, Windows PowerShell 3.0 First Steps.<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. Today I have the last excerpt from my new Microsoft Press book, <a href=\"http:\/\/www.amazon.com\/Windows-PowerShell-3-0-First-Steps\/dp\/0735681007\/ref=la_B001ILFMZ8_1_2_title_1_pap?s=books&amp;ie=UTF8&amp;qid=1383673887&amp;sr=1-2\" target=\"_blank\">Windows PowerShell 3.0 First Steps<\/a>.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0825.First%20Steps%20book.jpg\"><img decoding=\"async\" title=\"Image of book cover\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/0825.First%20Steps%20book.jpg\" alt=\"Image of book cover\" \/><\/a><\/p>\n<p class=\"Normalunindented\">In most situations, when you use WMI, you are performing some sort of query. Even when you are going to set a particular property, you still need to execute a query to return a dataset that enables you to perform the configuration. (A dataset includes the data that come back to you as the result of a query, that is, it is a set of data.) There are several steps involved in performing a basic WMI query:<\/p>\n<ol>\n<li>Connect to WMI by using the <strong>Get-WMIObject<\/strong><strong> <\/strong>cmdlet.<\/li>\n<li>Specify a valid WMI class name to query.<\/li>\n<li>Specify a value for the namespace&mdash;omit the <strong>Namespace<\/strong> parameter to use the default root\\cimv2 namespace.<\/li>\n<li>Specify a value for the <strong>ComputerName<\/strong><em> <\/em>parameter&mdash;omit the <strong>ComputerName<\/strong><em> <\/em>parameter to use the default value of <strong>LocalHost<\/strong><em>.<\/em><\/li>\n<\/ol>\n<p>Windows PowerShell makes it easy to query WMI. In fact, at its most basic level, the only thing required is <strong>gwmi<\/strong><em> <\/em>(alias for the <strong>Get-WmiObject <\/strong>cmdlet) and the WMI class name. An example of this simple syntax is shown here, along with the associated output:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">PS C:\\&gt; gwmi win32_bios<br \/> SMBIOSBIOSVersion : BAP6710H.86A.0064.2011.0504.1711<br \/> Manufacturer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : Intel Corp.<br \/> Name&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : BIOS Date: 05\/04\/11 17:11:33 Ver: 04.06.04<br \/> SerialNumber&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; :<br \/> Version&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; : INTEL&nbsp; &#8211; 1072009<\/p>\n<p>However, there are more properties available in the Win32_Bios WMI class than the five displayed in the previous output. The reason for the limited output that is displayed from the command is a custom view of the Win32_Bios class defined in the types.ps1xml file that resides in the Windows PowerShell home directory on your system. The following command uses the <strong>Select-String <\/strong>cmdlet to search the Types.ps1xml file to see if there is any reference to the WMI class Win32_Bios.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">Select-String -Path $pshome\\*.ps1xml -SimpleMatch &#8220;Win32_Bios&#8221;<\/p>\n<p>In the following image, several <strong>Select-String <\/strong>commands display results when a special format exists for a particular WMI class. The last query (for the Win32_CurrentTime WMI class) does not return any results, indicating that no special formatting exists for this class.<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3580.12-5-13-01.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/3580.12-5-13-01.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>The <strong>Select-String <\/strong>queries shown in the previous image indicate that there is a special formatting for the Win32_Bios, Win32_DesktopMonitor, and Win32_Service WMI classes. The Types.ps1xml file provides information to Windows PowerShell that tells it how to display a particular WMI class. When an instance of the Win32_Bios WMI class appears, Windows PowerShell uses the <strong>DefaultDisplayPropertySet<\/strong><em> <\/em>configuration to display only five properties. The portion of the Types.ps1xml file that details these five properties is shown here:<\/p>\n<p class=\"CodeBlock\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;PropertySet&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;DefaultDisplayPropertySet&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ReferencedProperties&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;SMBIOSBIOSVersion&lt;\/Name&gt;<br \/> &nbs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;&nbsp;&nbsp;&nbsp; &lt;\/PropertySet&gt;<\/p>\n<p>The complete type definition for the Win32_Bios WMI classis shown in following image:<\/p>\n<p class=\"Num-Caption\"><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2465.12-5-13-02.png\"><img decoding=\"async\" title=\"Image of command output\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/2465.12-5-13-02.png\" alt=\"Image of command output\" \/><\/a><\/p>\n<p>Special formatting instructions for the Win32_Bios WMI class indicate that there is an alternate property set available&mdash;a property set that is in addition to the <strong>DefaultDisplayPropertySet<\/strong><em>. <\/em>This additional property set, named <strong>PSStatus<\/strong><em>, <\/em>contains the four properties in the <strong>PropertySet<\/strong><em> <\/em>shown here:<\/p>\n<p class=\"CodeBlock\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;PropertySet&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;PSStatus&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;ReferencedProperties&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;Status&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;Name&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;Caption&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;Name&gt;SMBIOSPresent&lt;\/Name&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/ReferencedProperties&gt;<br \/> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;\/PropertySet&gt;<\/p>\n<p>Finding the <strong>PSStatus<\/strong><em> <\/em>property set is more than a simple academic exercise, because it can be used directly with Windows PowerShell cmdlets such as <strong>Select-Object <\/strong>(<strong>select<\/strong> is an alias),<strong> Format-List <\/strong>(<strong>fl<\/strong> is an alias),<strong> <\/strong>or <strong>Format-Table <\/strong>(<strong>ft<\/strong> is an alias). The following commands illustrate this technique:<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">gwmi win32_bios | select psstatus<br \/> gwmi win32_bios | fl psstatus<br \/> gwmi win32_bios | ft psstatus&nbsp;<\/p>\n<p>Unfortunately, you cannot use the alternate property set, <strong>PSStatus<\/strong>, to select the properties via the <strong>Property<\/strong><em> <\/em>parameter. Therefore, the following command fails.<\/p>\n<p class=\"CodeBlock\" style=\"padding-left: 30px\">gwmi win32_bios -Property psstatus<\/p>\n<p>That is all there is to using Windows PowerShell to query WMI.&nbsp; Join me tomorrow when I will have a guest post from Yuri Diogenes,&nbsp;<strong>Security Series: Using PowerShell to Enable BYOD&mdash;Part 1<\/strong>.<\/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\" target=\"_blank\">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><strong>Ed Wilson, Microsoft Scripting Guy<\/strong>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about querying WMI in this excerpt of his book, Windows PowerShell 3.0 First Steps. Microsoft Scripting Guy, Ed Wilson, is here. Today I have the last excerpt from my new Microsoft Press book, Windows PowerShell 3.0 First Steps. In most situations, when you use WMI, you are performing [&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":[51,3,4,45],"class_list":["post-2479","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-getting-started","tag-scripting-guy","tag-scripting-techniques","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about querying WMI in this excerpt of his book, Windows PowerShell 3.0 First Steps. Microsoft Scripting Guy, Ed Wilson, is here. Today I have the last excerpt from my new Microsoft Press book, Windows PowerShell 3.0 First Steps. In most situations, when you use WMI, you are performing [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2479","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=2479"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/2479\/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=2479"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=2479"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=2479"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}