Last active
February 5, 2025 06:55
-
-
Save milnak/3000cd6d94c6311f8099ae341b3d1fe9 to your computer and use it in GitHub Desktop.
Get Windows Product Key from PowerShell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Derived from WinProdKeyFinder | |
# https://github.com/mrpeardotnet/WinProdKeyFinder | |
$digitalProductId = (Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows NT\CurrentVersion').DigitalProductId | |
# First byte appears to be length | |
if ($digitalProductId[0] -ne $digitalProductId.Length) { | |
throw 'Invalid length.' | |
} | |
$ProductId = [Text.Encoding]::UTF8.GetString($digitalProductId[8..30]) | |
$Sku = [Text.Encoding]::UTF8.GetString($digitalProductId[36..48]) | |
$key = $null | |
$keyOffset = 52 | |
$isWin8 = [math]::truncate($digitalProductId[66] / 6) -band 1 | |
$digitalProductId[66] = ($digitalProductId[66] -band 0xf7) -bor (($isWin8 -band 2) * 4) | |
# decrypt base24 encoded binary data | |
$digits = 'BCDFGHJKMPQRTVWXY2346789' | |
$last = 0 | |
For ($i = 24; $i -ge 0; $i--) { | |
$current = 0 | |
For ($j = 14; $j -ge 0; $j--) { | |
$current = $current * 256 | |
$current += $digitalProductId[$keyOffset + $j] | |
$digitalProductId[$keyOffset + $j] = [math]::truncate($current / 24) | |
$current = $current % 24 | |
$last = $current | |
} | |
$key = $digits[$current] + $key | |
} | |
$key = $key.Substring(1, $last) + 'N' + $key.Substring($last + 1, $key.Length - ($last + 1)) | |
$key = $key.Substring(0, 5) + '-' + $key.Substring(5, 5) + '-' + $key.Substring(10, 5) + '-' + $key.Substring(15, 5) + '-' + $key.Substring(20, 5) | |
$cv = Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion' | |
$win32os = Get-WmiObject Win32_OperatingSystem | |
[PSCustomObject]@{ | |
Caption = $win32os.Caption | |
Version = '{0} (OS Build {1}.{2})' -f $cv.DisplayVersion, $cv.CurrentBuild, $cv.UBR | |
OSArch = $win32os.OSArchitecture | |
RegisteredOwner = $cv.RegisteredOwner | |
ProductID = $ProductId | |
Sku = $Sku | |
ProductKey = $key | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment