Sometimes you'll need to retrieve your SQL Server Product Key from an existing installation on your Client or Server machine: the most common scenario takes place when you have an old Server to move or relocate and no one around you seem to remember where the license are... or if you've simply lost the Service Key post-it.
Luckily enough, you can easily get this information back thanks to this neat PowerShell script made by Jacob Bindslet:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
function GetSqlServerProductKey { ## function to retrieve the license key of a SQL 2008 Server. param ($targets = ".") $hklm = 2147483650 $regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup" $regValue1 = "DigitalProductId" $regValue2 = "PatchLevel" $regValue3 = "Edition" Foreach ($target in $targets) { $productKey = $null $win32os = $null $wmi = [WMIClass]"\\$target\root\default:stdRegProv" $data = $wmi.GetBinaryValue($hklm,$regPath,$regValue1) [string]$SQLver = $wmi.GetstringValue($hklm,$regPath,$regValue2).svalue [string]$SQLedition = $wmi.GetstringValue($hklm,$regPath,$regValue3).svalue $binArray = ($data.uValue)[52..66] $charsArray = "B","C","D","F","G","H","J","K","M","P","Q","R","T","V","W","X","Y","2","3","4","6","7","8","9" ## decrypt base24 encoded binary data For ($i = 24; $i -ge 0; $i--) { $k = 0 For ($j = 14; $j -ge 0; $j--) { $k = $k * 256 -bxor $binArray[$j] $binArray[$j] = [math]::truncate($k / 24) $k = $k % 24 } $productKey = $charsArray[$k] + $productKey If (($i % 5 -eq 0) -and ($i -ne 0)) { $productKey = "-" + $productKey } } $win32os = Get-WmiObject Win32_OperatingSystem -computer $target $obj = New-Object Object $obj | Add-Member Noteproperty Computer -value $target $obj | Add-Member Noteproperty OSCaption -value $win32os.Caption $obj | Add-Member Noteproperty OSArch -value $win32os.OSArchitecture $obj | Add-Member Noteproperty SQLver -value $SQLver $obj | Add-Member Noteproperty SQLedition -value $SQLedition $obj | Add-Member Noteproperty ProductKey -value $productkey $obj } } |
The script works with any SQL Server edition & version starting from 2005: SQL Server 2005, SQL Server 2008 and SQL Server 2008 R2. Pay close attention, though, if you're using Sql Server 2012 or Sql Server 2014 you'll have to make some small modifications to that code.
For Sql Server 2012 you need to replace two lines of code. In details, replace line 5 with the following line:
1 |
$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup" |
And also replace line 16 with the following line (thanks to gprkns for pointing it out):
1 |
$binArray = ($data.uValue)[0..16] |
You can also take a look of the complete script code for Sql Server 2012 at the following link.
For Sql Server 2014, Microsoft moved the DigitalProductID node to the actual instance name in the registry, so you will need to replace line 5 with something similar to the following (depending on your installation):
1 |
$regPath = "SOFTWARE\Microsoft\Microsoft SQL Server\MSSQL12.[YOUR SQL INSTANCE NAME]\Setup" |
All you have to do in order to execute this script is to perform these actions:
- Launch a PowerShell prompt (Start > Run, then type powershell and press ENTER.
- Copy the above function text and past it directly inside the prompt area.
- Press ENTER a couple times, just to be sure you're back to the prompt.
- Type GetSqlServerProductKey, then press ENTER.
If everything has been done like it should you'll be able to see the following informations:
As you can see there's a lot of stuff regarding your SQL Server installation, most of them you should know already, the latter being the Product Key.
That's all for now: happy recover!