SQL SERVER – T-SQL Script to find the CD key from Registry

Here is the way to find SQL Server CD key, which was used to install it on machine. If user do not have permission on the SP, please login using SA username. Expended stored procedure xp_regread can read any registry values. I have used this XP to read CD_KEY. This is undocumented Stroed Procedure and may not be supported in Future Version of SQL Server.

USE master
EXEC xp_regread 'HKEY_LOCAL_MACHINE','SOFTWARE\Microsoft\Microsoft
SQL Server\80\Registration'

Reference : Pinal Dave (http://blog.SQLAuthority.com)

29 thoughts on “SQL SERVER – T-SQL Script to find the CD key from Registry

  1. This post is really useful. Thanks,

    Also i need to know how to retrieve the Sql server Instance Name from the Registry.
    I am using SQL Server2005 Express version.


  2. Good one …..Mr Dave
    Can you explore some more Undocumented XP_ if possible plz send the link ..I will be great ful to u …thanx or u can send on my mail ID …i guess u have

    Thanx in advance!! chief


  3. CREATE PROC Reg_Get
    EXEC master.dbo.xp_regenumkeys ‘HKEY_LOCAL_MACHINE’,’SOFTWARE\ISS\CPE’
    IF @@ERROR 0
    RAISERROR (‘Key doesnt exists ‘,10,1 )
    RETURN 1
    RETURN 0
    — Calling Script
    EXEC Reg_Get
    — Result
    Msg 22001, Level 16, State 1, Line 0
    RegOpenKeyEx() returned error 2, ‘The system cannot find the file specified.’

    I want to trap above error without using TRY..CATCH. So that It can support to SQL Server 2000 also


  4. Pingback: SQLAuthority News - Best Articles on SQLAuthority.com Journey to SQL Authority with Pinal Dave

  5. unfortunately This code piece z not wotking my MS SQL 2005 /2008 versions.Any help ?

    Error msg .
    RegOpenKeyEx() returned error 2, ‘The system cannot find the file specified.’
    Msg 22001, Level 1, State 1

    (0 row(s) affected)


  6. exec xp_regread ‘HKEY_LOCAL_MACHINE’,’SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\Setup’,’DigitalProductID’

    2008 server has a different registry, I get a binary result. I am not sure if this is even the CD Key, I wasn’t able to find the CD key for 2008.


  7. Here is the power shell script to get CD key from SQL Server 2008 and R2 written by Jakob Bindslet, which I found while searching…

    function Get-SQLserverKey {
    ## function to retrieve the license key of a SQL 2008 Server.
    ## by Jakob Bindslet (jakob@bindslet.dk)
    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

    Use the function to retrieve the Product Key from the local PC:



    • Thanks for the script. I’m very new to PowerShell and am having trouble running this. I changed all the funky double quotes to regular double quotes but cannot get past an error
      “You must provide a value expression on the right-hand side of the ‘-‘ operator.
      At line:20 char:28
      + For ($i = 24; $i -ge 0; $i-) <<<< {

      I've retyped some of the lines thinking a hidden char might be the issue, but to no avail.

      Thanks again!


    • it is an awesome script! thank you … it is working for 2008 , but what about 2012? i didchange the path to SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup… didn’t help , why?


  8. how do i get the key from sql 2008 R2 install on windows plateform?
    2005 query its not working for me.

    giving following error

    RegOpenKeyEx() returned error 2, ‘The system cannot find the file specified.’
    Msg 22001, Level 1, State 0

    (0 row(s) affected)


  9. For Sql Server 2008 R2


    EXEC XP_REGREAD ‘HKEY_LOCAL_MACHINE’,’SOFTWARE\Microsoft\Microsoft SQL Server\100\BIDS\Setup’,’ProductCode’


  10. Shami Qureshi – thank you, just it is not showing CD Key – all of my servers are showing same productCode and i know i used different CD Key for 2 out or 3. I need to find out if i used a correct key for one of the servers and not the key that was intended for a diffent server. anyway to do that? Thanks :)


  11. For SQL Server 2012:


    EXEC XP_REGREAD ‘HKEY_LOCAL_MACHINE’,’SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\Setup’,’ProductCode’


  12. Pingback: SQL SERVER – Weekly Series – Memory Lane – #018 | SQL Server Journey with SQL Authority

  13. USE master
    EXEC xp_regread ‘HKEY_LOCAL_MACHINE’,’SOFTWAREMicrosoftMicrosoft SQL Server110ToolsSetup’,’ProductCode’


  14. The Output of this query like
    Value Data
    DigitalProductID 0xA4000000030000003032……

    Please let me know ,How can i find meaningful data from if.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s