PoshCode Logo PowerShell Code Repository

get windows product key by Dominik 6 weeks ago (modification of post by karl prosser view diff)
diff | embed code: <script type="text/javascript" src="http://PoshCode.org/embed/6676"></script>download | new post

retrieve the windows product key of a specified machine using WMI

  1. function get-windowsproductkey([string]$computer)
  2. {
  3. $Reg = [WMIClass] ("\\" + $computer + "\root\default:StdRegProv")
  4. $values = [byte[]]($reg.getbinaryvalue(2147483650,"SOFTWARE\Microsoft\Windows NT\CurrentVersion","DigitalProductId").uvalue)
  5. $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")
  6. $keyStartIndex = [int]52;
  7. $keyEndIndex = [int]($keyStartIndex + 15);
  8. $decodeLength = [int]29
  9. $decodeStringLength = [int]15
  10. $decodedChars = new-object char[] $decodeLength
  11. $hexPid = new-object System.Collections.ArrayList
  12. for ($i = $keyStartIndex; $i -le $keyEndIndex; $i++){ [void]$hexPid.Add($values[$i]) }
  13. for ( $i = $decodeLength - 1; $i -ge 0; $i--)
  14.     {                
  15.      if (($i + 1) % 6 -eq 0){$decodedChars[$i] = '-'}
  16.      else
  17.        {
  18.         $digitMapIndex = [int]0
  19.         for ($j = $decodeStringLength - 1; $j -ge 0; $j--)
  20.         {
  21.             $byteValue = [int](($digitMapIndex * [int]256) -bor [byte]$hexPid[$j]);
  22.             $hexPid[$j] = [byte] ([math]::Floor($byteValue / 24));
  23.             $digitMapIndex = $byteValue % 24;
  24.             $decodedChars[$i] = $lookup[$digitMapIndex];
  25.          }
  26.         }
  27.      }
  28. $STR = ''    
  29. $decodedChars | % { $str+=$_}
  30. $STR
  31. }
  32. get-windowsproductkey .

Submit a correction or amendment below (
click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:


Remember me