August 21st, 2012

Use PowerShell to Check the License Status of Windows 8

Doctor Scripto
Scripter

Summary: Use Windows PowerShell and WMI to determine the number of days remaining on an evaluation copy of Windows 8.

Microsoft Scripting Guy, Ed Wilson, is here. One of the cool features in Windows 8 is that it has Hyper-V built-in to it. This means that I can use the cool new Hyper-V cmdlets to manage things. For example, I can type the following command to start all of my virtual machines:

Get-VM | Start-VM

When I am finished making my presentation, I can use the following command to shut down all of the virtual machines.

Get-VM | Stop-VM

All this is pretty cool, until I get ready to make a presentation, and all of my virtual machines are whining that they need to activate or they will shut down. Not the best situation if you are preparing to make a presentation at something like TechEd or Tech Mentor. One way to ensure that this situation does not occur is to use the following script.

Get-WindowsLicensingStatus.ps1

$cim = New-CimSession -ComputerName (get-vm).name -Credential nwtraders\administrator

get-ciminstance -class SoftwareLicensingProduct -CimSession $cim |

  where {$_.name -match ‘windows’ -AND $_.licensefamily} |

    format-list -property Name, Description, `

             @{Label=”Grace period (days)”; Expression={ $_.graceperiodremaining / 1440}}, `

             @{Label= “License Status”; Expression={switch (foreach {$_.LicenseStatus}) `

              { 0 {“Unlicensed”} `

                1 {“Licensed”} `

                2 {“Out-Of-Box Grace Period”} `

                3 {“Out-Of-Tolerance Grace Period”} `

                4 {“Non-Genuine Grace Period”} `

              } } }

The first thing I do is create a CIM session to connect to all of my virtual machines. I use the cool automatic foreach technique that I talked about yesterday in My Five Favorite PowerShell 3.0 Tips and Tricks. Now, I use the Get-CimInstance cmdlet to perform a WMI query on each of the servers and clients that are running on my virtual machine.

Note   I could have used the Get-VM | Start-VM commands that I mentioned at the beginning of this blog post to start the virtual machines and to check their status.

I filter out the software products by names that match Windows and only those elements that have a LicenseFamily. Then I pipe the results to the Format-List cmdlet to produce a nice output. Keep in mind, if I am running this against a larger network, I might want to export the information to a CSV file so I can easily sort the results in Microsoft Excel.

The command itself uses a couple of custom labels: the GracePeriodRemaining property reports in minutes; therefore, to have a more meaningful result, I divide by 1440. The LicenseStatus property reports as an enumeration, so I use a switch statement to create a more easily readable report. Certain builds report multiple items. As shown in the image that follows, the server reports multiple items, all of which say there are no remaining grace days and no licensing status.

Image of command output

When I run the script (modified slightly) against my local computer, however, it reports exactly how much grace time I have, and the license status.

Image of command output

This WMI class also reports on things like Office status. Therefore, if you are running the beta version of Office 15 (like I am doing), that product also reports—all I need to do is change my Where-Object. This results in the command shown here.

get-ciminstance -class SoftwareLicensingProduct |

  where {$_.name -match ‘office’ -AND $_.licensefamily}

The output of the command is shown in the following image.

Image of command output

That is about all there is to examining licensing activation status for Windows 8 or for Windows Server 2012. Join me tomorrow for more cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

Author

The "Scripting Guys" is a historical title passed from scripter to scripter. The current revision has morphed into our good friend Doctor Scripto who has been with us since the very beginning.

0 comments

Discussion are closed.