PowerShell: Getting Windows Product Key

Ever wanted to read out the Windows license key? In the Windows Registry, this key is present, but it is stored as a digital ID. To convert it back to the license key used to originally license the product, try this function. It uses some serious math:

function Get-ProductKey {   
    $map="BCDFGHJKMPQRTVWXY2346789"
    $value = (get-itemproperty "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").digitalproductid[0x34..0x42] 
    $ProductKey = "" 
    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
      }
      $ProductKey = $map[$r] + $ProductKey
      if (($i % 5) -eq 0 -and $i -ne 0) {
        $ProductKey = "-" + $ProductKey
      }
    }
    $ProductKey
}

2 Responses to “PowerShell: Getting Windows Product Key”

  1. Denis Portela July 2, 2012 at 4:16 pm #

    I simply want to say I am beginner to blogs and seriously loved you’re web-site. Likely I’m likely to bookmark your site . You amazingly have great well written articles. Appreciate it for sharing with us your webpage.

  2. Jorge Rodrigues November 21, 2013 at 3:59 pm #

    Thanks a lot for this 🙂

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

revia in alcoholism