Print

VBScript to retrieve Windows Product Key

What?
A quick article with the code to retrieve your product key in Windows 7 with a small VB script file. There are other articles on the web about this but the ones I found returned errors such as WshShell not valid. This article has a working example applicable to Windows 7 Professional.

Why?
With the Windows 10 operating system offered as a free upgrade (for Windows 7 or later at time of print), I needed the serial number / product key from my Windows 7 Professional 32-bit operating system. I didn't want to dig through my CD/DVDs to find my original windows 7 disc and no longer have access to the email account when I purchased Windows 7 (my university one).

How?
I'm going to use my trusty notepad program, copy the following code to it, and run it:
  1. Create a notepad file called read_product_id.vbs
  2. Copy the following code to the file:
    copyraw
    Set wshShell = CreateObject("Wscript.Shell")
    MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId"))
    
    Function ConvertToKey(Key)
    Const KeyOffset = 52
    i = 28
    Chars = "BCDFGHJKMPQRTVWXY2346789"
    Do
    Cur = 0
    x = 14
    Do
    Cur = Cur * 256
    Cur = Key(x + KeyOffset) + Cur
    Key(x + KeyOffset) = (Cur \ 24) And 255
    Cur = Cur Mod 24
    x = x -1
    Loop While x >= 0
    i = i -1
    KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput
    If (((29 - i) Mod 6) = 0) And (i <> -1) Then
    i = i -1
    KeyOutput = "-" & KeyOutput
    End If
    Loop While i >= 0
    ConvertToKey = KeyOutput
    End Function
    1.  Set wshShell = CreateObject("Wscript.Shell") 
    2.  MsgBox ConvertToKey(WshShell.RegRead("HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\DigitalProductId")) 
    3.   
    4.  Function ConvertToKey(Key) 
    5.  Const KeyOffset = 52 
    6.  i = 28 
    7.  Chars = "BCDFGHJKMPQRTVWXY2346789" 
    8.  Do 
    9.  Cur = 0 
    10.  x = 14 
    11.  Do 
    12.  Cur = Cur * 256 
    13.  Cur = Key(+ KeyOffset) + Cur 
    14.  Key(+ KeyOffset) = (Cur \ 24) And 255 
    15.  Cur = Cur Mod 24 
    16.  x = x -1 
    17.  Loop While x >= 0 
    18.  i = i -1 
    19.  KeyOutput = Mid(Chars, Cur + 1, 1) & KeyOutput 
    20.  If (((29 - i) Mod 6) = 0) And (<> -1) Then 
    21.  i = i -1 
    22.  KeyOutput = "-" & KeyOutput 
    23.  End If 
    24.  Loop While i >= 0 
    25.  ConvertToKey = KeyOutput 
    26.  End Function 
  3. Save the file and double-click on it to run it
  4. Done
Category: Windows OS :: Article: 630