PowerShell – Back up Product Key

email me

Function BackupKey
{
	$FileName='ProductKey.txt'
    $RegKeyNumber = 2147483650
	$ComputerName = $env:COMPUTERNAME
	$RegPath = "Software\Microsoft\Windows NT\CurrentVersion"
	$ID = "DigitalProductId"
	$objWMI = [WMIClass]"\\$ComputerName\root\default:stdRegProv"
	
    # Return Reg Value 
	$Object = $objWMI.GetBinaryValue($RegKeyNumber,$RegPath,$ID)
	[Array]$IDvalue = $Object.uValue 
	
	If($IDvalue)
	{
		# Return Product name and 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
		
        # Perform binary translation 
        $Result = ConvertTokey $IDvalue
		
        # Return Operating System
        $OperatingSystem = (Get-WmiObject "Win32_OperatingSystem"  | select Caption).Caption
		If($OperatingSystem -match "Microsoft Windows 8" -or $OperatingSystem -match "Microsoft Windows 10")
		{
			if($Result)
			{
				
				[string]$value ="ProductName  : $ProductName `r`n" `
				+ "ProductID    : $ProductID `r`n" `
				+ "Installed Key: $Result"
				$value 

				# Output to file 
				$Choice = GetChoice
				If( $Choice -eq 0 )
				{	
                    # back up key to user desktop
					$txtpath = "C:\Users\"+$env:USERNAME+"\Desktop"
                    
					New-Item -Path $txtpath -Name $FileName -Value $value   -ItemType File  -Force | Out-Null 
                    
                    [System.Windows.MessageBox]::Show('Great Success!')
				}
				Elseif($Choice -eq 1)
				{
					Exit 
				}
			}
			Else
			{
				Write-Warning "Please run this script on Windows 8.x or Windows 10!"
			}
		}
		Else
		{
			Write-Warning "Please run this script on Windows 8!"
		}
		
	}
	Else
	{
		Write-Warning "Failed to get Windows 8 product key!"
	}

}
 
Function GetChoice
{
    $OptionYES = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes",""
    $OptionNO = New-Object System.Management.Automation.Host.ChoiceDescription "&No",""
    $CHOICES = [System.Management.Automation.Host.ChoiceDescription[]]($OptionYES,$OptionNO)
    $CAPTION = "Product Key"
    $MESSAGE = "  Do you want to save the product key?"
    $RESULT = $Host.UI.PromptForChoice($CAPTION,$MESSAGE,$CHOICES,0)
    $RESULT
}
# Perform Binary to serial translation
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)
	$PRODUCTKEY = $a + "-" + $b + "-"+ $c + "-"+ $d + "-"+ $e
	$PRODUCTKEY 
	
  
}

BackupKey