Need your Windows 8 product key and you just cannot find it anyway. Well, look no further! Here is your salvation.

You want to replace your HDD with an SSD and you cannot find the product key for your Windows 8. You look all over… you scour your system for that glorious little label that Microsoft used to require all resellers to put on their systems with the product key. But, this label is no where to be found! What do you do!??!?!

Easy. You just need to whip up a Powershell script and run it to get yourself a neat little text file with it inside.

First, open up a text editor (you can use Notepad or my preferred Notepad++).

Then, paste in the following:

#Main function
Function GetWin8Key
{
	$Hklm = 2147483650
	$Target = $env:COMPUTERNAME
	$regPath = "Software\Microsoft\Windows NT\CurrentVersion"
	$DigitalID = "DigitalProductId"
	$wmi = [WMIClass]"\\$Target\root\default:stdRegProv"
	#Get registry value 
	$Object = $wmi.GetBinaryValue($hklm,$regPath,$DigitalID)
	[Array]$DigitalIDvalue = $Object.uValue 
	#If get successed
	If($DigitalIDvalue)
	{
		#Get producnt name and product ID
		$ProductName = (Get-itemproperty -Path "HKLM:Software\Microsoft\Windows NT\CurrentVersion" -Name "ProductName").ProductName 
		$ProductID =  (Get-itemproperty -Path "HKLM:Software\Microsoft\Windows NT\CurrentVersion" -Name "ProductId").ProductId
		#Convert binary value to serial number 
		$Result = ConvertTokey $DigitalIDvalue
		$OSInfo = (Get-WmiObject "Win32_OperatingSystem"  | select Caption).Caption
		#if result is not null
		If($OSInfo -match "Microsoft Windows 8")
		{
			if($Result)
			{
				
				[string]$value ="ProductName  : $ProductName `r`n" `
				+ "ProductID    : $ProductID `r`n" `
				+ "Installed Key: $Result"
				$value 
				#Save Windows info to a file 
				$Choice = GetChoice
				If( $Choice -eq 0 )
				{	
					$txtpath = "C:\Users"+$env:USERNAME+"\Desktop"
					New-Item -Path $txtpath -Name "WindowsKeyInfo.txt" -Value $value   -ItemType File  -Force | Out-Null 
				}
				Elseif($Choice -eq 1)
				{
					Exit 
				}
			}
			Else
			{
				Write-Warning "Please run this script on Windows 8."
			}
		}
		Else
		{
			Write-Warning "Please run this script on Windows 8."
		}
		
	}
	Else
	{
		Write-Warning "Failed to get Windows 8 product key,Some error occured."
	}

}
#Get user choice 
Function GetChoice
{
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
    $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no)
    $caption = "Confirming"
    $message = "Save product key to a file?"
    $result = $Host.UI.PromptForChoice($caption,$message,$choices,0)
    $result
}
#Convert binary to serial number 
Function ConvertToKey($Key)
{
	$Keyoffset = 52 
	$isWin8 = [int]($Key[66]/6) -band 1
	$HF7 = 0xF7
	$Key[66] = ($Key[66] -band $HF7) -bOr (($isWin8 -band 2) * 4)
	$i = 24
	[String]$Chars = "BCDFGHJKMPQRTVWXY2346789"	
	do
	{
		$Cur = 0 
		$X = 14
		Do
		{
			$Cur = $Cur * 256    
			$Cur = $Key[$X + $Keyoffset] + $Cur
			$Key[$X + $Keyoffset] = [math]::Floor([double]($Cur/24))
			$Cur = $Cur % 24
			$X = $X - 1 
		}while($X -ge 0)
		$i = $i- 1
		$KeyOutput = $Chars.SubString($Cur,1) + $KeyOutput
		$last = $Cur
	}while($i -ge 0)
	
	$Keypart1 = $KeyOutput.SubString(1,$last)
	$Keypart2 = $KeyOutput.Substring(1,$KeyOutput.length-1)
	if($last -eq 0 )
	{
		$KeyOutput = "N" + $Keypart2
	}
	else
	{
		$KeyOutput = $Keypart2.Insert($Keypart2.IndexOf($Keypart1)+$Keypart1.length,"N")
	}
	$a = $KeyOutput.Substring(0,5)
	$b = $KeyOutput.substring(5,5)
	$c = $KeyOutput.substring(10,5)
	$d = $KeyOutput.substring(15,5)
	$e = $KeyOutput.substring(20,5)
	$keyproduct = $a + "-" + $b + "-"+ $c + "-"+ $d + "-"+ $e
	$keyproduct 
	
  
}
GetWin8Key

Next you are going to save the file as “RecoverWin8Key.ps1” or whatever you want as long as the extension is “.ps1” and not “.txt”. I usually just save it to my Desktop. After that you just Right Click on the file and select “Run with PowerShell”.

You should get a PowerShell terminal box that will open. It looks different than the Command box in the fact it is dark blue instead of black. It should display the product name, product id, and the installed key. It will also prompt you to save the product key to a file. Select Y for yes and it will save it to the location the file is run from.

That is it!

On a side note… you MAY get an execution error when running it.  If so… you may have to open PowerShell “as Administrator” and then type in “Set-ExecutionPolicy RemoteSigned” and hit enter. Select Y for yes. This will allow you to execute unsigned PowerShell scripts. Then you can close PowerShell and run the script like previously described.

If you have any issues with this… send me a comment with a description of the issue and I will try to help you.