{"id":7651,"date":"2015-02-24T00:01:00","date_gmt":"2015-02-24T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2015\/02\/24\/when-to-use-wmi-and-powershell\/"},"modified":"2019-02-18T10:30:30","modified_gmt":"2019-02-18T17:30:30","slug":"when-to-use-wmi-and-powershell","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/when-to-use-wmi-and-powershell\/","title":{"rendered":"When to Use WMI and PowerShell"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about when to use WMI.<\/span>\nMicrosoft Scripting Guy, Ed Wilson, is here.&nbsp;<span style=\"font-size:12px\">One of the things that Windows PowerShell has done is make WMI very easy to use. Beginning with Windows PowerShell 1.0 and the <\/span><b style=\"font-size:12px\">Get-WmiObject<\/b><span style=\"font-size:12px\"> cmdlet, WMI information has become much easier to access. Whereas a simple VBScript script or Perl script would require a minimum of seven lines of rather complicated code to access WMI information, Windows PowerShell made the same information available as a one-liner. Truly a marvelous win for IT pros!<\/span>\nBut one problem remains. WMI (even in Windows PowerShell) can be complicated and inconsistent. In fact, when I taught WMI classes, I would say, &ldquo;The only thing consistent about WMI is that it is inconsistent.&rdquo; Although that is a bit of an overstatement, anyone who spends much time working with WMI also spends a lot of time researching anomalies on MSDN.\nHistorically, some of the tasks that required using WMI <span>on local or remote computers&nbsp;<\/span>are:<\/p>\n<ul>\n<li>Working with the registry<\/li>\n<li>Working with processes&nbsp;<\/li>\n<li>Working with services&nbsp;<\/li>\n<li>Performing hardware inventory&nbsp;<\/li>\n<li>Working with files and folders&nbsp;<\/li>\n<li>Working with event logs&nbsp;<\/li>\n<\/ul>\n<p>Beginning with Windows PowerShell 2.0, you can perform these tasks (except for a hardware inventory) by using Windows PowerShell. There is no need to use WMI to work with files, folders, event logs, the registry, processes and services&mdash;either locally or remotely.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp; There are still some things related to these topics that require WMI, but most of the main tasks related to these topics can be performed without using WMI.\nBeginning with Windows PowerShell&nbsp;3.0 in Windows&nbsp;8 and Windows Server&nbsp;2012, many of the hardware-related tasks (such as inventorying hard disks and network adapters) no longer require complex WMI queries.<\/p>\n<p style=\"margin-left:30px\"><b>Note<\/b>&nbsp;&nbsp;The underlying technology is still WMI, but the classes are basically wrapped and exposed as functions that behave like Windows PowerShell cmdlets.\nSo, what is the best way to use WMI?\nAvoid using WMI at all.\nIf I can use a Windows PowerShell command such as:<\/p>\n<p style=\"margin-left:30px\">Get-NetAdapter\ninstead of typing:<\/p>\n<p style=\"margin-left:30px\">Get-WmiObject Win32_NetworkAdapter\n-or-<\/p>\n<p style=\"margin-left:30px\">Get-CimInstance Win32_NetworkAdapter\n&#8230;then of course, I want to use the Windows PowerShell command.\nThe other thing that is great about using the <b>Get-NetAdapter<\/b> cmdlet is that it uses CIM, which uses Windows PowerShell remoting instead of remote procedure call (RPC). This means that I can query for network adapter information from a remote computer through one port in the firewall instead of opening a bunch of ports in the firewall. The other advantage is that it is much faster.\nThis week, I will talk about when to use Windows PowerShell and when I still have to use WMI. It will be fun.\nThat is all there is to using Windows PowerShell to find WMI stuff. WMI Week will continue tomorrow when I will talk about more cool stuff.<span style=\"font-size:12px\">&nbsp;<\/span>\nI 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=\"http:\/\/blogs.technet.commailto: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.\n<b>Ed Wilson, Microsoft Scripting Guy<\/b><span style=\"font-size:12px\">&nbsp;<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about when to use WMI. Microsoft Scripting Guy, Ed Wilson, is here.&nbsp;One of the things that Windows PowerShell has done is make WMI very easy to use. Beginning with Windows PowerShell 1.0 and the Get-WmiObject cmdlet, WMI information has become much easier to access. Whereas a simple VBScript [&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-7651","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, talks about when to use WMI. Microsoft Scripting Guy, Ed Wilson, is here.&nbsp;One of the things that Windows PowerShell has done is make WMI very easy to use. Beginning with Windows PowerShell 1.0 and the Get-WmiObject cmdlet, WMI information has become much easier to access. Whereas a simple VBScript [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7651","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=7651"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/7651\/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=7651"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=7651"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=7651"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}