Managing Windows License Key (Part 2)

by Feb 26, 2019

Most automated license key management tasks are done via a command called slmgr.

This command is actually an ancient VBScript. To read all of your license settings, for example, try this:

 
PS> slmgr.vbs /dlv
 

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:

 
PS> slmgr.vbs /dlv

PS> cscript.exe slmgr.vbs /dlv
Microsoft (R) Windows Script Host, Version 5.812
Copyright (C) Microsoft Corporation. All Rights Reserved.

Script file "C:\Users\tobwe\slmgr.vbs" not found.

PS> 
 

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:

 
PS> Get-Command slmgr.vbs | Select-Object -ExpandProperty Source
C:\WINDOWS\system32\slmgr.vbs
 

And this code reads the information into the PowerShell console:

$Path = Get-Command slmgr.vbs | Select-Object -ExpandProperty Source
$Data = cscript.exe //Nologo $Path /dlv
$Data 

$Data contains ugly plain text, though, and it is localized, too. 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.

 
PS> slmgr /?  
 

Your learning points:


    • Windows license management is often done via slmgr.vbs, a VBScript. When you call it with “/?”, you get help for its command parameters.
    • PowerShell can call slmgr. To receive results, run it with cscript.exe, and provide the complete (absolute) path to slmgr.vbs.
    • Get-Command is an easy way of finding the complete (absolute) paths of files that reside in one of the system folders (listed in $env:PATH).


psconf.eu – PowerShell Conference EU 2019 – June 4-7, Hannover Germany – visit www.psconf.eu There aren’t too many trainings around for experienced PowerShell scripters where you really still learn something new. But there’s one place you don’t want to miss: PowerShell Conference EU – with 40 renown international speakers including PowerShell team members and MVPs, plus 350 professional and creative PowerShell scripters. Registration is open at www.psconf.eu, and the full 3-track 4-days agenda becomes available soon. Once a year it’s just a smart move to come together, update know-how, learn about security and mitigations, and bring home fresh ideas and authoritative guidance. We’d sure love to see and hear from you!

Twitter This Tip! ReTweet this Tip!