# encoding: ascii
# api: powershell
# title: Product key
# description: This script has been wrote especialy for WinXP and MS Office 2k7 (that’s why there is no any garanties what it’ll be work at later MS products) to restore lost activation keys.
# version: 12.0
# type: function
# author: greg zakharov
# license: CC0
# function: Get-SerialNumber
# x-poshcode-id: 3323
# x-archived: 2016-05-29T12:48:10
# x-published: 2012-04-06T06:46:00
#
#
function Get-SerialNumber {
  $regVal = Get-ItemProperty $regDir.PSPath
  $arrVal = $regVal.DigitalProductId
  $arrBin = $arrVal[52..66]
  $arrChr = "B", "C", "D", "F", "G", "H", "J", "K", "M", "P", "Q", "R", `
            "T", "V", "W", "X", "Y", "2", "3", "4", "6", "7", "8", "9"

  for ($i = 24; $i -ge 0; $i--) {
    $k = 0;
    for ($j = 14; $j -ge 0; $j--) {
      $k = $k * 256 -bxor $arrBin[$j]
      $arrBin[$j] = [math]::truncate($k / 24)
      $k = $k % 24
    }
    $strKey = $arrChr[$k] + $strKey

    if (($i % 5 -eq 0) -and ($i -ne 0)) {
      $strKey = "-" + $strKey
    }
  }
  $strKey
}

#Windows serial key
$regDir = Get-Item "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$key_1 = Get-SerialNumber

#MS Office 2007 serial key
$regDir = Get-ChildItem HKLM:\SOFTWARE\Microsoft\Office\12.0\Registration
$key_2 = Get-SerialNumber

Write-Output "OS       : $key_1"
Write-Output "MS Office: $key_2"
