PowerShell – Return Product Key

email me

This is how you return the product key for your computer using PowerShell

$Computer = $Env:Computername

$TranslationMapping="BCDFGHJKMPQRTVWXY2346789"


$MSRegKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)

$value = $MSRegKey.OpenSubKey("SOFTWARE\Microsoft\Windows NT\CurrentVersion").GetValue('DigitalProductId4')[0x34..0x42]

$ProductKeyOutput = "" 

for ($i = 24; $i -ge 0; $i--) { 
    $r = 0 
    for ($j = 14; $j -ge 0; $j--) { 
        $r = ($r * 256) -bxor $value[$j] 
        $value[$j] = [math]::Floor([double]($r/24)) 
        $r = $r % 24 
        } 

    $ProductKeyOutput = $TranslationMapping[$r] + $ProductKeyOutput 

    if (($i % 5) -eq 0 -and $i -ne 0) { 
        $ProductKeyOutput = "-" + $ProductKeyOutput 
        } 
}


clear-host
write-host ""
"Product Key: " + $ProductKeyOutput
write-host ""