This guide explains how to use the Get-WindowsKey PowerShell script to retrieve the product key of a Windows operating system. The script works effectively on Windows Server 2025 but may not function as expected on earlier versions due to changes in how product keys are stored or accessed in the Windows registry.
The Get-WindowsKey script is designed to extract the Windows product key from the system registry. The product key is essential for system reactivation, troubleshooting, or auditing purposes.
- Registry Path: The script accesses the following registry path:
HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion
This location stores the DigitalProductId, which contains the product key in an encoded format.
- Decoding Logic:
- The script extracts the DigitalProductId value and applies an algorithm to decode it into a readable product key.
- The decoding process iterates over the binary data stored in the registry and converts it into a 25-character alphanumeric key using a specific character set.
- Character Formatting:
The product key is formatted in the standard 5x5 structure (e.g., XXXXX-XXXXX-XXXXX-XXXXX-XXXXX) for readability.
- Output:
The decoded product key is returned and displayed in the PowerShell console.
function Get-WindowsKey {
$Path = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$DigitalProductId = (Get-ItemProperty -Path $Path).DigitalProductId
$ProductKey = ""
$KeyOffset = 52
$Chars = "BCDFGHJKMPQRTVWXY2346789"
for ($i = 0; $i -lt 25; $i++) {
$Current = 0
for ($j = 14; $j -ge 0; $j--) {
$Current = $Current * 256 -bxor $DigitalProductId[$KeyOffset + $j]
$DigitalProductId[$KeyOffset + $j] = [math]::Floor($Current / 24)
$Current = $Current % 24
}
$ProductKey = $Chars[$Current] + $ProductKey
if (($i % 5 -eq 4) -and ($i -ne 24)) {
$ProductKey = "-" + $ProductKey
}
}
return $ProductKey
}
Get-WindowsKey
- Open PowerShell as Administrator:
- Press Win + S, search for "PowerShell", right-click it, and select Run as Administrator.
- Copy and Paste the Script:
- Copy the above script into the PowerShell console and press Enter.
- View the Output:
- After running the script, the decoded product key will be displayed in the console.
- Windows Server 2025: The script works reliably as the registry structure for the product key is supported.
- Earlier Versions: On some older Windows versions (e.g., Windows 7, Windows Server 2012), the script may fail because:
- The DigitalProductId may not be stored in the same location.
- The encoding method for the product key might differ.
- OEM Keys: If the operating system was activated using a digital license tied to hardware or a Microsoft account, the key retrieved might not match the one used for installation.
- Volume Licensing: If the system is activated through a KMS (Key Management Service) or MAK (Multiple Activation Key), the script might return a generic product key.
- Digital Licenses: For systems activated with a digital license, the key might not be stored in the registry and cannot be retrieved using this script.
- Backup the Product Key: Once retrieved, store the product key in a safe location for future use.
- Test Compatibility: Before relying on the script, test it on the intended Windows version to confirm it retrieves the correct product key.
- If the script does not produce a result, ensure:
- PowerShell is run as an administrator.
- The registry path contains the DigitalProductId value.
- The system is not activated through a digital license or KMS. This script is a handy tool for administrators managing Windows Server 2025 and can simplify product key retrieval without third-party software.