Managing Windows License Key with PowerShell

With WMI you can get extensive information about Windows licenses and license status. One of the WMI classes used by Windows licensing is called “SoftwareLicensingService“. It can provide you your Windows license number. The same class also contains a lot of additional information.

Example 1


Example 2


Software Licensing Manager (SLMgr)

Most automated license key management tasks are now done via the command slmgr. This command is actually an ancient VBScript. To read all of your license settings, just try this:

This opens a separate window, and displays many license activation details. Displaying the information in a dialog window is useless for PowerShell and automation, however when you try and run the VBScript via cscript.exe, this may fail:


You could change the default VBS host to cscript.exe, but a nicer way without touching global settings is this: find out the complete path for the VBScript, then run it with this absolute path. This is how you know where the VBScript is located:

And this code reads the information into the PowerShell console:


The $Data contains plain text ans this is not a very sophisticated way of getting license and activation info.

Since the slmgr.vbs script can do a lot of magic license tasks, as you can see when you open its help, let’s examine the source code in our upcoming text, and get to the information directly, bypassing the old VBScript.

Most Windows license and activation tasks can be automated using an ancient VBScript called slmgr.vbs. It does not make much sense to use this tool to get information, though, because slmgr.vbs uses localized strings and turns all data into one big string format.

A much better way is to bypass slmgr.vbs and query the original source of information directly. To find out how slmgr.vbs does this, you can look at the script source code. Run the PowerShell code below within the PowerShell ISE:


Let’s start to convert one piece of functionality to PowerShell. The parameter /dlv for example displays license information:


When you search the VBScript source code for “dlv”, you quickly find that at the heart of all of this, there is a function called GetServiceObject() which turns out to be a simple WMI query:

In fact, the script reveals the WMI classes involved in licensing. From PowerShell you can easily query all service related information like this:


Unlike with slmgr.vbs, you get the information in object-oriented format, so it is trivial to access individual items:

All the Windows licensing information returned by slmgr.vbs comes from WMI. Instead of calling slmgr.vbs and receiving text, you can query WMI directly and bypass slmgr.vbs altogether. One big source of information is the SoftwareLicensingService WMI class.

Aside from the SoftwareLicensingService WMI class which we already covered, there is another one called SoftwareLicensingProduct which has many instances and takes some time to retrieve:


The Description column reveals that this class represents the many different license types available for Windows, only some of which are actually licensed. To get useful information, you’d have to filter the information for only the licenses that are in use.

Use a server-side filter with the -Filter parameter in order to considerably speed the query up. Make sure WMI only returns instances that actually have a non-null ProductKeyId:


As you can see, dates are returned in WMI format. Use Get-CimInstance instead of Get-WmiObject to return “real” DateTobjects instead:ime


Individual license information for Microsoft products (including Office) are represented by the SoftwareLicensingProduct WMI class. Use Get-CimInstance instead of Get-WmiObject if you want PowerShell to return readable DateTime objects instead of the cryptic WMI .datetime format.

I really like to hear what you have to say about this article:

  • Was this article helpful for you or do you have any questions?
  • Do you have additions, suggestions or any other ideas?
  • Just let me know by leaving a comment below.

Thank you for spending some time at my site and in my blog. I hope you come to visit again soon 😉

About Lex van der Horst 201 Articles

Be the first to comment

Leave a Reply