PoshCode Archive  Artifact [50b04e44e0]

Artifact 50b04e44e0a38fa32d3562083fc969dc2e02eb0b68b0b6f3a38a1e1d8f91c2ee:

  • File get-windows-product-key.ps1 — part of check-in [2c6151981d] at 2018-06-10 13:55:48 on branch trunk — retrieve the windows product key of a specified machine using WMI (user: greg zakharov size: 1085)

# encoding: ascii
# api: powershell
# title: get windows product key
# description: retrieve the windows product key of a specified machine using WMI
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Get-ProductKey
# x-poshcode-id: 5478
# x-archived: 2014-10-08T03:38:34
# x-published: 2014-10-02T14:56:00
#
#
function Get-ProductKey([String]$Computer = '.') {
  <#
    .NOTES
        Author: greg zakharov
  #>
  begin {
    $val = [Byte[]]([wmiclass]('\\' + $Computer + '\root\default:StdRegProv')).GetBinaryValue(
      2147483650, 'SOFTWARE\Microsoft\Windows NT\CurrentVersion', 'DigitalProductId'
    ).uValue[52..66]
    $map = "BCDFGHJKMPQRTVWXY2346789"
    $key = ""
  }
  process {
    for ($i = 24; $i -ge 0; $i--) {
      $k = 0
      for ($j = 14; $j -ge 0; $j--) {
        $k = ($k * 256) -bxor $val[$j]
        $val[$j] = [Math]::Floor([Double]($k / 24))
        $k = $k % 24
      }
      $key = $map[$k] + $key
      if (($i % 5) -eq 0 -and $i -ne 0) {$key = "-" + $key}
    }
  }
  end {$key}
}