Skip to content

Instantly share code, notes, and snippets.

@jordanrinke
Last active June 9, 2024 18:22
Show Gist options
  • Save jordanrinke/74810ecbec5d1e6bb880ca45313a408f to your computer and use it in GitHub Desktop.
Save jordanrinke/74810ecbec5d1e6bb880ca45313a408f to your computer and use it in GitHub Desktop.
function Get-ProductKey {
<#
.SYNOPSIS
Retrieves the product key and OS information from a local or remote system/s.
.DESCRIPTION
Retrieves the product key and OS information from a local or remote system/s. Queries of 64bit OS from a 32bit OS will result in
inaccurate data being returned for the Product Key. You must query a 64bit OS from a system running a 64bit OS.
.PARAMETER Computername
Name of the local or remote system/s.
.NOTES
Original Author: Boe Prox
Components taken direct from Jakob Bindslet (jakob@bindslet.dk)
Small changes to make keys correct - Jordan Rinke
Version: 1.1
-Update of function from http://powershell.com/cs/blogs/tips/archive/2012/04/30/getting-windows-product-key.aspx
-Added capability to query more than one system
-Supports remote system query
-Supports querying 64bit OSes
-Shows OS description and Version in output object
-Error Handling
.EXAMPLE
Get-ProductKey -Computername Server1
OSDescription Computername OSVersion ProductKey
------------- ------------ --------- ----------
Microsoft(R) Windows(R) Server 2003, Enterprise Edition Server1 5.2.3790 bcdfg-hjklm-pqrtt-vwxyy-12345
Description
-----------
Retrieves the product key information from 'Server1'
#>
[cmdletbinding()]
Param (
[parameter(ValueFromPipeLine=$True,ValueFromPipeLineByPropertyName=$True)]
[Alias("CN","__Server","IPAddress","Server")]
[string[]]$Computername = $Env:Computername
)
Begin {
$map="BCDFGHJKMPQRTVWXY2346789"
$hklm = 2147483650
$regPath = "Software\Microsoft\Windows NT\CurrentVersion"
$regValue = "DigitalProductId"
}
Process {
ForEach ($Computer in $Computername) {
Write-Verbose ("{0}: Checking network availability" -f $Computer)
If (Test-Connection -ComputerName $Computer -Count 1 -Quiet) {
Try {
Write-Verbose ("{0}: Retrieving WMI OS information" -f $Computer)
$OS = Get-WmiObject -ComputerName $Computer Win32_OperatingSystem -ErrorAction Stop
} Catch {
$OS = New-Object PSObject -Property @{
Caption = $_.Exception.Message
Version = $_.Exception.Message
}
}
Try {
Write-Verbose ("{0}: Attempting remote registry access" -f $Computer)
$remoteReg = [WMIClass]"\\$Computer\root\default:stdRegProv"
$data = $remoteReg.GetBinaryValue($hklm,$regPath,$regValue)
$value = ($data.uValue)[51..66]
$ProductKey = ""
Write-Verbose ("{0}: Translating data into product key" -f $Computer)
for ($i = 24; $i -ge 0; $i--) {
$r = 0
for ($j = 14; $j -ge 1; $j--) {
$r = ($r * 256) + $value[$j]
$value[$j] = [math]::Floor([double]($r/24))
$r = $r % 24
}
$ProductKey = $map[$r] + $ProductKey
if (($i % 5) -eq 0 -and $i -ne 0) {
$ProductKey = "-" + $ProductKey
}
}
} Catch {
$ProductKey = $_.Exception.Message
}
$object = New-Object PSObject -Property @{
Computername = $Computer
ProductKey = $ProductKey
OSDescription = $os.Caption
OSVersion = $os.Version
}
$object.pstypenames.insert(0,'ProductKey.Info')
$object
} Else {
$object = New-Object PSObject -Property @{
Computername = $Computer
ProductKey = 'Unreachable'
OSDescription = 'Unreachable'
OSVersion = 'Unreachable'
}
$object.pstypenames.insert(0,'ProductKey.Info')
$object
}
}
}
}
@jordanrinke
Copy link
Author

jordanrinke commented Dec 17, 2016

Further testing shows this to be returning, unique, but incorrect data.

edit: Fixed, the loop and start point were off. Tested this on multiple machines and it is returning the proper key now (tested on server 2012 r2)

@ainsean
Copy link

ainsean commented Aug 18, 2017

can we have version for server 2016?

@ainsean
Copy link

ainsean commented Aug 18, 2017

coz its not working on server 2016

@acastillov
Copy link

Hi Jordan

it is returning almost the correct data, the first 5 digits are wrong

your script is returning:
FFXTT-

and must be:

FXTNT-

the rest are okay.

is there any updated version?

thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment