{"id":54283,"date":"2009-03-02T21:18:00","date_gmt":"2009-03-02T21:18:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2009\/03\/02\/hey-scripting-guy-how-can-i-use-wmi-with-windows-powershell\/"},"modified":"2009-03-02T21:18:00","modified_gmt":"2009-03-02T21:18:00","slug":"hey-scripting-guy-how-can-i-use-wmi-with-windows-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-use-wmi-with-windows-powershell\/","title":{"rendered":"Hey, Scripting Guy! How Can I Use WMI with Windows PowerShell?"},"content":{"rendered":"<p><H2><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> <\/H2>\n<P>I have been looking at Windows PowerShell, and think it would be cool to use. We use WMI a lot at work, and I am wondering if I can gather the same kinds of information using Windows PowerShell that we use VBScript for? Is Windows PowerShell designed to do this?<BR><BR>&#8211; DB<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hi DB,<\/P>\n<P>It is rather early in the morning down here in the southern part of the United States, and it is three hours earlier out in Redmond, Washington, in the USA. My tea pot is empty, so I am going to go make me a pot of tea and come back to you. Okay, back. Made a nice pot of Zhu Ye Qing tea. It has a lovely aroma and is a slightly sweet green tea. When paired with a small piece of 80 percent extra dark organic chocolate and maybe an ANZAC biscuit, it makes an awesome midafternoon tea. Because this is still early morning, I skipped the biscuit. Oh, yes, you want to know about using WMI with Windows PowerShell. No problem. <\/P>\n<TABLE id=\"EXC\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">This week we will be looking at working with WMI from within Windows PowerShell. In honor of this week, we have created <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/wmi.mspx\">a new WMI Scripting Hub<\/A> that pulls together a number of useful articles and tools from all over the Script Center into a single location. Because the articles will be using Windows PowerShell, you may also be interested in the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/msh.mspx\">Windows PowerShell Scripting Hub<\/A>. You will find information there that will tell you how to download and install Windows PowerShell. <\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>I know this is the Hey Scripting Guys! article, but when working with Windows PowerShell and WMI it is often not a requirement to write a script. We can actually do some pretty good things without needing to write a script. I use the following command from the Windows PowerShell console to see how much free disk space I have on my computer. <\/P><PRE class=\"codeSample\">Get-WmiObject -Class Win32_LogicalDisk<\/PRE>\n<P>That is it. It will display the most commonly requested information about logical disks. This is seen here:<\/P><IMG border=\"0\" alt=\"Image of the display of the most commonly requested information about logical disks\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0302\/hsg-3-2-9-1.jpg\" width=\"500\" height=\"426\"> \n<P>&nbsp;<\/P>\n<P>While the command seen above is short and easily typed, it can be made easier by using aliases. The <B>Get-WmiObject<\/B> has an alias (or shortcut name) of <B>GWMI<\/B>\u2014or <B>gwmi<\/B>, which is the way I usually type it. The class name is the default parameter for the <B>Get-WmiObject<\/B> cmdlet, so we do not need to use the <B>\u2013class<\/B> either. Using this super short syntax, the command becomes this: <\/P><PRE class=\"codeSample\">gwmi win32_LogicalDisk<\/PRE>\n<P>That command is shorter than a short-haired miniature Dachshund puppy running on a sandy beach in June. <\/P>\n<P>To perform the same command in VBScript, and to retrieve exactly the same information in VBScript, you would need to use a script that looks similar to <B>ListLogicalDiskDetails.vbs<\/B>, which is seen here: <\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const wbemFlagReturnImmediately = &amp;h10\nConst wbemFlagForwardOnly = &amp;h20<\/p>\n<p>StrComputer = &#8220;.&#8221;\n   WScript.Echo\n   Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\CIMV2&#8221;)\n   Set colItems = objWMIService.ExecQuery(&#8220;SELECT * FROM Win32_LogicalDisk&#8221;, &#8220;WQL&#8221;, _\n                                          wbemFlagReturnImmediately + wbemFlagForwardOnly)<\/p>\n<p>For Each objItem In colItems\n      WScript.Echo &#8220;DeviceID: &#8221; &amp; objItem.DeviceID\n      WScript.Echo &#8220;DriveType: &#8221; &amp; objItem.DriveType\n      WScript.Echo &#8220;FreeSpace: &#8221; &amp; objItem.FreeSpace\n      WScript.Echo &#8220;ProviderName: &#8221; &amp; objItem.ProviderName\n      WScript.Echo &#8220;Size: &#8221; &amp; objItem.Size\n      WScript.Echo &#8220;VolumeName: &#8221; &amp; objItem.VolumeName\n      WScript.Echo \nNext<\/PRE>\n<P>Suppose I want information about the parallel port on my computer. I can use this command (again reverting to the long syntax for clarity): <\/P><PRE class=\"codeSample\">Get-WmiObject -Class Win32_ParallelPort<\/PRE>\n<P>That is it. You can also see <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/hardware\/ports\/hwpovb01.mspx\">the same command written in VBScript<\/A>. The output from running our Windows PowerShell command is seen here: <\/P><IMG border=\"0\" alt=\"Image of the output from running the Windows PowerShell command\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0302\/hsg-3-2-9-2.jpg\" width=\"500\" height=\"426\"> \n<P>&nbsp;<\/P>\n<P>To obtain information about my sound card by using VBScript, it takes <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/hardware\/devices\/hwdevbo6.mspx\">nearly 20 lines of code<\/A>, but it can be written as this single line in Windows PowerShell, if I want to use the alias for <B>Get-WmiObject<\/B> and rely upon the <B>\u2013class<\/B> parameter being the default parameter: <\/P><PRE class=\"codeSample\">gwmi win32_sounddevice<\/PRE>\n<P>That one-line command retrieves all the information that WMI supplies about sound cards, the same as the previously mentioned VBScript. This is seen here: <\/P><IMG border=\"0\" alt=\"Image of the information that WMI supplies about sound cards\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0302\/hsg-3-2-9-3.jpg\" width=\"500\" height=\"426\"> \n<P>&nbsp;<\/P>\n<P>If you are interested in obtaining information about your desktop monitor, you can use the <B>Win32_DesktopMonitor<\/B> WMI class on Microsoft Windows operating systems before Vista. Beginning with Windows Vista, the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394122(VS.85).aspx\">Win32_DesktopMonitor<\/A> WMI class does not return reliable information because it is not compatible with the Windows Display Driver Model (WDDM). On Windows Vista and later, you will need to use the <A href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/aa394512(VS.85).aspx\">Win32_VideoController<\/A> class. If you are afraid you will get confused, you can use <B>Win32_VideoController<\/B> on Windows XP and Windows Server 2003 as well as Windows Vista and later. To retrieve this information using Windows PowerShell, you would use the command seen here, or you can use the <B>GWMI<\/B> alias (to write the same script in VBScript would take <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/hardware\/display\/hwdivb05.mspx\">more than 50 lines of code<\/A>): <\/P><PRE class=\"codeSample\">Get-WmiObject -Class Win32_VideoController<\/PRE>\n<P>The results of this command are seen here: <\/P><IMG border=\"0\" alt=\"Image of the display of the most commonly requested information about logical disks\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/hsg\/2009\/march\/hey0302\/hsg-3-2-9-4.jpg\" width=\"500\" height=\"426\"> \n<P>&nbsp;<\/P>\n<P>I realize I am being unfair to VBScript, and this is not intended to be a head-to-head comparison. In fact, I love VBScript. I have written three books on VBScript, and spent more than 5 years traveling around the world, visiting nearly 20 countries in the process, teaching VBScript via the different workshops I wrote. Because Windows PowerShell was written after VBScript, things that were confusing and cumbersome to do in VBScript are ridiculously easy to do in Windows PowerShell. That simply is not fair! The good thing is we can turn this devious trick on its head, and leverage it for our own purposes. WMI is super easy to use from the Windows PowerShell prompt. Sneak attack or not, I will accept it. <\/P>\n<P><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/vista\/wmi2.mspx\">This article<\/A> talks about using the <B>win32_processor<\/B> class on Windows Vista. Note the examples, which are using VBScript, and compare them to this syntax: <\/P><PRE class=\"codeSample\">gwmi win32_processor<\/PRE>\n<P>Well, DB, I will let you go. Get Windows PowerShell, install it on your workstation (or on the computer belonging to your kids or significant other if you are afraid of messing up your own computer), and practice the techniques in this article. Go to the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/scripts\/default.mspx\">Script Center Script Repository<\/A>, and look through the scripts there, particularly in the hardware section. See you tomorrow when we will continue with WMI Week. Until then, peace! <\/P>\n<P>&nbsp;<\/P>\n<P><B>Ed Wilson and Craig Liebendorfer, Scripting Guys<\/B><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have been looking at Windows PowerShell, and think it would be cool to use. We use WMI a lot at work, and I am wondering if I can gather the same kinds of information using Windows PowerShell that we use VBScript for? Is Windows PowerShell designed to do this?&#8211; DB Hi DB, It is [&hellip;]<\/p>\n","protected":false},"author":595,"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-54283","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>I have been looking at Windows PowerShell, and think it would be cool to use. We use WMI a lot at work, and I am wondering if I can gather the same kinds of information using Windows PowerShell that we use VBScript for? Is Windows PowerShell designed to do this?&#8211; DB Hi DB, It is [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54283","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\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=54283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/54283\/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=54283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=54283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=54283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}