Most people may have used the VBS to read the Windows Product key in the VBS script, the VBS script is usually more obscure, difficult to understand, today, a sneak, write a PowerShell script to read the Windows product key.
The code is as follows:
Copy Code code as follows:
===== FileName: get-windowsproductkey.ps1=====
function Get-windowsproductkey ([string] $computer)
{
$comments =@ '
AUTHOR:FUHJ (powershell#live.cn, http://fuhaijun.com)
Example:get-windowsproductkey.
'@
$reg = [WMIClass] ("\" + $computer + "\root\default:StdRegProv")
$values = [byte[]] ($reg. Getbinaryvalue (2147483650, "SOFTWARE\Microsoft\Windows NT\CurrentVersion", " DigitalProductID "). Uvalue)
$lookup = [char[]] ("B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", " 9 ")
$keyStartIndex = [int]52;
$keyEndIndex = [int] ($keyStartIndex + 15);
$decodeLength = [int]29
$decodeStringLength = [int]15
$decodedChars = New-object char[] $decodeLength
$hexPid = New-object System.Collections.ArrayList
for ($i = $keyStartIndex; $i-le $keyEndIndex; $i + +) {[void] $hexPid. ADD ($values [$i])}
for ($i = $decodeLength-1; $i-ge 0; $i-)
{
if (($i + 1)% 6-eq 0) {$decodedChars [$i] = '-'}
Else
{
$digitMapIndex = [int]0
for ($j = $decodeStringLength-1; $j-ge 0; $j-)
{
$byteValue = [int] ($DIGITMAPINDEX * [int]256)-bor [byte] $hexPid [$j]);
$hexPid [$j] = [byte] ([Math]::floor ($byteValue/24));
$digitMapIndex = $byteValue% 24;
$decodedChars [$i] = $lookup [$digitMapIndex];
}
}
}
$STR = '
$decodedChars | % {$str +=$_}
$STR
}
Get-windowsproductkey.
The execution effect is as follows: