{"id":715,"date":"2014-09-08T00:01:00","date_gmt":"2014-09-08T00:01:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2014\/09\/08\/use-powershell-to-detect-activation-status\/"},"modified":"2014-09-08T00:01:00","modified_gmt":"2014-09-08T00:01:00","slug":"use-powershell-to-detect-activation-status","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/use-powershell-to-detect-activation-status\/","title":{"rendered":"Use PowerShell to Detect Activation Status"},"content":{"rendered":"<p><b style=\"font-size:12px\">Summary<\/b><span style=\"font-size:12px\">: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to display Windows activation status.<\/span><\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Question\" \/>&nbsp;Hey, Scripting Guy! The previous network administrator at my company was not attentive to his work. I am not sure what he actually did for a living, but it certainly was not network administration. I think he was a professional Internet surfer. I suspect that many of our desktop operating systems are not activated properly. I want to produce a list of desktop systems, get the names and versions of the operating systems, and find out if the system is activated properly. Can I do this with Windows PowerShell?<\/p>\n<p>&mdash;DB<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" alt=\"Hey, Scripting Guy! Answer\" \/>&nbsp;Hello DB,<\/p>\n<p>Microsoft Scripting Guy, Ed Wilson, is here. This morning I am still sore from my session with my new personal trainer. In fact, I am pretty sure that I am sore in muscles that I did not know that I had. I guess that means it is working&hellip;we will see. I am sipping a cup of Oolong tea this morning. Normally, I would have a black tea in the morning, but this morning, for some reason, I thought that Oolong would do the trick.<\/p>\n<p>I brought back some very nice Oolong tea from Leipzi, Germany earlier this year, and I have been saving it for special occasions. I feel like this morning is a special occasion. With Oolong, especially a very nice Oolong, I do not add anything to the tea. No cinnamon stick, no lemon, no milk, and especially no sugar. Only tea. The Oolong has a very complex flavor, and I can close my eyes and taste the natural sweetness of the tea. There is no need to mess around with something that is already perfect.<\/p>\n<h2>Windows Management Instrumentation<\/h2>\n<p>There are numerous ways to access Windows Management Instrumentation (WMI) information by using Windows PowerShell. The best way is to use the CIM cmdlets. The CIM cmdlets shipped in Windows PowerShell&nbsp;3.0 (with Windows&nbsp;8 and Windows Server&nbsp;2012), so they have been around for a while. The WMI team wrote a great article about CIM cmdlets on the Windows PowerShell Blog that you may want to read: <a href=\"http:\/\/blogs.msdn.com\/b\/powershell\/archive\/2012\/08\/24\/introduction-to-cim-cmdlets.aspx\" target=\"_blank\">Introduction to CIM Cmdlets<\/a>.<\/p>\n<p>To find information about current versions of Windows, I can use the SoftwareLicensingProduct WMI class. (For versions earlier than Windows&nbsp;7, other WMI classes are required). The <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/cc534596(v=vs.85).aspx\" target=\"_blank\">SoftwareLicensingProduct class<\/a> is documented on MSDN, and it contains numerous methods that permit network administrators to completely manage Windows licensing. Two of the more useful methods (for me anyway) are the <b>Active<\/b><i> <\/i>and the <b>UninstallProductKey<\/b><i> <\/i>methods, both of which are pretty much self-explanatory as to what they do.<\/p>\n<p>One reason for using the <b>Get-CimInstance<\/b> cmdlet is that I can create a CIM session pretty much automatically. This is because it relies on WINRM for remoting. It uses the same remoting technology as other Windows PowerShell remoting. This means that it is automatically activated and it simply works in Windows Server&nbsp;2012&nbsp;R2 and Windows Server&nbsp;2012.<\/p>\n<p>For desktop operating systems, WinRM needs to be enabled, but that is easy enough to do via Group Policy (in larger environments) or via a logon script (for smaller environments). If you have been using Windows PowerShell for a while to manage your network, chances are good that you already have WinRM enabled.<\/p>\n<p>I can easily query Active Directory Domain Services (AD&nbsp;DS) by using the <b>Get-Computer<\/b> cmdlet to retrieve a list of the computers I want to check. I can then feed that list to the <b>New-CimSession<\/b> cmdlet to create CIM sessions, and then I can use <b>Get-CimInstance<\/b> to retrieve my information.<\/p>\n<p>That is how I might modify things to run against all of the computers on my network. For output, I would probably pipe the results to <b>Export-CSV<\/b> to create a nice file that I can open in Microsoft Excel for my documentation.<\/p>\n<h2>Getting licensing status<\/h2>\n<p>I am going to use the <b>Get-CimInstance<\/b> cmdlet to query the SoftwareLicensingProduct WMI class, and to find the ApplicationID for my version of Windows. This is sort of a magic number, and it takes a bit of searching on the Internet to find the information (it is not in the MSDN article for the WMI class).<\/p>\n<p>I use the <b>Get-CimInstance<\/b> Windows PowerShell cmdlet, specify the WMI class, and pick up the ComputerName value from the environmental variable. I specify the <b>ApplicationID<\/b> property value for my filter, and I look for a <b>LicenseStatus<\/b> of 1 (this means it is activated, and this enumeration value does come from the MSDN article).<\/p>\n<p>Then I select the name and description of the product, and I create a custom property that includes the name of the computer. For this example, I throw it to <b>Format-List<\/b> so I can see my results. Here is the script:<\/p>\n<p style=\"margin-left:30px\">Get-CimInstance SoftwareLicensingProduct -ComputerName $env:computername&nbsp; `<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -Filter &quot;ApplicationID = &#039;55c92734-d682-4d71-983e-d6ec3f16059f&#039;&quot; |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; where licensestatus -eq 1&nbsp; |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Select name, description, @{Label=&#039;computer&#039;;<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Expression = {$_.PscomputerName}} |<\/p>\n<p style=\"margin-left:30px\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Format-List&nbsp; name, description, computer&nbsp;<\/p>\n<p>The use of the script and the associated output are shown in the following image:<\/p>\n<p><a href=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-8-14-01.png\"><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/hsg-9-8-14-01.png\" alt=\"Image of command output\" title=\"Image of command output\" \/><\/a><\/p>\n<p>DB, that is all there is to using Windows PowerShell to detect product activation status. WMI Week will continue tomorrow when I will talk about more cool stuff.<\/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><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 using Windows PowerShell and WMI to display Windows activation status. &nbsp;Hey, Scripting Guy! The previous network administrator at my company was not attentive to his work. I am not sure what he actually did for a living, but it certainly was not network administration. I think he [&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":[385,31,3,4,288,45],"class_list":["post-715","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-cim","tag-operating-system","tag-scripting-guy","tag-scripting-techniques","tag-version-information","tag-windows-powershell"],"acf":[],"blog_post_summary":"<p>Summary: Microsoft Scripting Guy, Ed Wilson, talks about using Windows PowerShell and WMI to display Windows activation status. &nbsp;Hey, Scripting Guy! The previous network administrator at my company was not attentive to his work. I am not sure what he actually did for a living, but it certainly was not network administration. I think he [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/715","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=715"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/715\/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=715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=715"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}