There are times when you may want to recover your Windows product key from a working Windows installation, whether to back up the product key before beginning to install a newer version of Windows, or to reinstall existing version of Windows operating system.
The standard or the popular way of retrieving Windows product key is to use a third-party software such as License Crawler or use a free Windows product key recovery CD to recover the product key from unbootable PCs.
While there are plenty of free tools available for this job, the fact is that one can recover Windows product key from registry without having to use third-party tools.
Out friend at WinAero has posted a helpful guide on recovering Windows product key without using third-party tools. The only thing is that your PC must be bootable to recover the product key using this method. And if your PC is unbootable, follow our how to recover Windows 7/8/8/8.1 product key from unbootable PC guide.
NOTE: This method works in all recent versions of Windows, including Windows 7, Windows 8, and Windows 8.1.
Step 1: Copy the following code and paste it into Notepad. Save the file as RecoverKey.ps1 on your desktop. Note that it’s important to save the file with .ps1 extension.
function Get-WindowsKey {
## function to retrieve the Windows Product Key from any PC
## by Jakob Bindslet ([email protected])
param ($targets = “.”)
$hklm = 2147483650
$regPath = “Software\Microsoft\Windows NT\CurrentVersion”
$regValue = “DigitalProductId”
Foreach ($target in $targets) {
$productKey = $null
$win32os = $null
$wmi = [WMIClass]”\\$target\root\default:stdRegProv”
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$binArray = ($data.uValue)[52..66]
$charsArray = “B”,”C”,”D”,”F”,”G”,”H”,”J”,”K”,”M”,”P”,”Q”,”R”,”T”,”V”,”W”,”X”,”Y”,”2″,”3″,”4″,”6″,”7″,”8″,”9″
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i–) {
$k = 0
For ($j = 14; $j -ge 0; $j–) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = “-” + $productKey
}
}
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target
$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value $win32os.Caption
$obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
$obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
$obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
}
}
Step 2: Open Windows PowerShell as administrator. To do this, you can type Windows PowerShell in the Start menu (Windows 7) or Start screen (Windows 8/8.1), and then simultaneously press Ctrl + Shift + Enter keys.
Alternatively, you can right-click on the Windows 8.1 Start button and then click Windows PowerShell (Admin) to launch PowerShell as administrator.
Click on the Yes button when you see the User Account Control (UAC) dialog.
Step 3: In the PowerShell window, type the following code (or you can simply copy and paste) and press Enter key to change the execution policy for non-signed scripts.
Set-ExecutionPolicy RemoteSigned
Click Enter key or ‘Y’ key when you see “Do you want to change the execution policy” message.
Step 4: Finally, type the following command and then press Enter key to view your Windows product key. That’s it!
Import-Module C:\Users\PC\Desktop\RecoverKey.ps1; Get-WindowsKey
In the above command, replace “C:\Users\PC\Desktop\RecoverKey.ps1” with the path to the previously saved RecoverKey.ps1 file. To easily get the path, press and hold down the Shift key, right-click on RecoverKey.ps1 file, and then click Copy as path option.
Good luck!
WakeUp says
Copy and paste a script from the web? Not very useful on a virus-ridden machine that you want to keep offline, though, is it?
Zac says
Hey there! I’m trying to do this on Windows 7 and I’m getting errors. I think I’ve fixed all the odd commas and hyphens but I’m still getting the error “You must provide a value expression on the right-hand side of the ‘-‘ operator”, referring to line 16, character 28.
It also says “the term ‘Get-WindowsKey’ is not recognized as the name of a cmdlet…”
Any ideas about this? Thanks.
Justn says
Hey Pete! Glad I read the comments first and saved myself a headache!
The code gave me the Product key right away!
Andre Noun says
Default is No. Can I change it to Yes? I have tried, but no way.
Juan says
Thanks Pete, the copy-paste issue is so common with code that it be a worthwhile project to make a script to clean it up.
Basic concept:
All types of double quotes — > standard double quote
Hyphen —> minus – minus
The screen capture of the code also helps.
Moto says
Just wanted to drop a note here…after adjusting the script as noted here in the comments, I did get it to run. However…the install code it provided did not work. The Windows 8.1 retail disk that had been used to do the install in the first place would not accept it. (I’m trying to help a friend reinstall…they had BBY install 8.1 for them at one point with a full retail copy he bought there, and I have no idea what they did with the COA)
So, I went back into Windows and downloaded ProduKey. Which ran fine, and gave a *completely* different install code. I mean, it wasn’t even close. And the Windows 8.1 install CD accepted that code just fine.
…so I have no idea what to tell you, but somehow the code that this script generated was completely bogus.
Miguel says
please just place a link to download the text document
BKize says
Please ignore my previous post. My apologies.
There is obviously some text processing on this site that occurs during a post and reformats various characters such as quotes and hyphens. The result is incorrect formatting of the script that has to be corrected manually as Peter describes.
BKize says
Here is a corrected version of the script above that works:
function Get-WindowsKey {
## function to retrieve the Windows Product Key from any PC
## by Jakob Bindslet ([email protected])
param ($targets = “.”)
$hklm = 2147483650
$regPath = “Software\Microsoft\Windows NT\CurrentVersion”
$regValue = “DigitalProductId”
Foreach ($target in $targets) {
$productKey = $null
$win32os = $null
$wmi = [WMIClass]”\\$target\root\default:stdRegProv”
$data = $wmi.GetBinaryValue($hklm,$regPath,$regValue)
$binArray = ($data.uValue)[52..66]
$charsArray = “B”,”C”,”D”,”F”,”G”,”H”,”J”,”K”,”M”,”P”,”Q”,”R”,”T”,”V”,”W”,”X”,”Y”,”2″,”3″,”4″,”6″,”7″,”8″,”9″
## decrypt base24 encoded binary data
For ($i = 24; $i -ge 0; $i–) {
$k = 0
For ($j = 14; $j -ge 0; $j–) {
$k = $k * 256 -bxor $binArray[$j]
$binArray[$j] = [math]::truncate($k / 24)
$k = $k % 24
}
$productKey = $charsArray[$k] + $productKey
If (($i % 5 -eq 0) -and ($i -ne 0)) {
$productKey = “-” + $productKey
}
}
$win32os = Get-WmiObject Win32_OperatingSystem -computer $target
$obj = New-Object Object
$obj | Add-Member Noteproperty Computer -value $target
$obj | Add-Member Noteproperty Caption -value $win32os.Caption
$obj | Add-Member Noteproperty CSDVersion -value $win32os.CSDVersion
$obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture
$obj | Add-Member Noteproperty BuildNumber -value $win32os.BuildNumber
$obj | Add-Member Noteproperty RegisteredTo -value $win32os.RegisteredUser
$obj | Add-Member Noteproperty ProductID -value $win32os.SerialNumber
$obj | Add-Member Noteproperty ProductKey -value $productkey
$obj
}
}
admin says
Peter and Robert,
Thanks for your inputs. The script worked fine on my Windows 8 and 8.1 machines. Will try to fix the quotes issue. Between, we have already reviewed Windows 8 Product Key Viewer here:
intowindows.com/windows-8-product-key-viewer/
Robert says
The script fails on my system. Actually asis it won’t run in your copy for some reason (probably due to the “). Anyways I found it elsewhere and on my system it actually failed to retrieve the key. I even set the script to unrestricted. I also tried another script and that version gave me a different and also invalid key. The jellybean key viewer also failed by giving back the wrong key. The only program that worked was the one here… (although it is 3rd party which is not what this article is about)
forums.mydigitallife.info/threads/30363-Windows-8-Product-Key-Viewer
Peter says
Couple of gotchas when copying and pasting the code into notepad…
1. All the quotes will come in as smart quotes instead of plain text quotes. These need to be changed back to standard quote marks for the script to work.
2. The two “for” loops (lines 16 and 18) have had the “–” (two hyphen characters) changed to “—” (em-dash character) just before the closing bracket on each line. These need to be reverted to two hyphens (as per the notepad screenshot above)
Aside from that, I can confirm that the script returned my product key correctly.
Pete.