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

Credit where Credit is Due:


Feel free to copy, redistribute and share this information. All that we ask is that you attribute credit and possibly even a link back to this website as it really helps in our search engine rankings.

Disclaimer: Please note that the information provided on this website is intended for informational purposes only and does not represent a warranty. The opinions expressed are those of the author only. We recommend testing any solutions in a development environment before implementing them in production. The articles are based on our good faith efforts and were current at the time of writing, reflecting our practical experience in a commercial setting.

Thank you for visiting and, as always, we hope this website was of some use to you!

Kind Regards,

Joel Lipman
www.joellipman.com

Related Articles

Joes Revolver Map

Accreditation

Badge - Certified Zoho Creator Associate
Badge - Certified Zoho Creator Associate

Donate & Support

If you like my content, and would like to support this sharing site, feel free to donate using a method below:

Paypal:
Donate to Joel Lipman via PayPal

Bitcoin:
Donate to Joel Lipman with Bitcoin bc1qf6elrdxc968h0k673l2djc9wrpazhqtxw8qqp4

Ethereum:
Donate to Joel Lipman with Ethereum 0xb038962F3809b425D661EF5D22294Cf45E02FebF
© 2024 Joel Lipman .com. All Rights Reserved.