PowerShell: retrieve the Windows product key of a computer
PowerShell function that decodes DigitalProductId through WMI to show the Windows product key of a local or remote computer.
This memo describes a PowerShell function, Get-WindowsKey, that reads the Windows product key of a local or remote computer and displays it with some system information. The function comes from a script by Nedim Mehic; it is not reproduced here in full.
How it works
- The binary value
DigitalProductIdunderHKLM\Software\Microsoft\Windows NT\CurrentVersionis read through WMI (theStdRegProvclass in theroot\defaultnamespace), which also works against a remote machine. - The 15 bytes at index 52 to 66 of that value hold the key encoded in base 24. A double loop decodes them with the alphabet
BCDFGHJKMPQRTVWXY2346789and inserts a dash every five characters. - The function then queries
Win32_OperatingSystemto add the OS name (Caption), service pack version (CSDVersion), architecture (OSArchitecture), build number (BuildNumber), registered user (RegisteredUser) and product ID (SerialNumber). - It returns one object per machine with the properties
Computer,Caption,CSDVersion,OSArch,BuildNumber,RegisteredTo,ProductIDandProductKey.
Usage
The -targets parameter accepts a list of computer names (default ., the local computer):
Get-WindowsKey
Get-WindowsKey -targets "pc01","pc02"
Notes
- The function relies on
Get-WmiObjectand remote WMI access, which require administrator rights on the target. - On recent Windows versions the key may be stored in firmware (UEFI) or tied to a digital licence; the value decoded from
DigitalProductIdmay then not match the purchased key.