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 (https://blog.sqlauthority.com)

SQL Scripts, SQL Stored Procedure, SQL Utility
Previous Post
SQL SERVER – What is New in SQL Server Agent for Microsoft SQL Server 2005
Next Post
SQL SERVER – Delete Duplicate Records – Rows

Related Posts

25 Comments. Leave new

  • Thanks, I’ve used this twice now..

    BTW, when I cut and paste the code above, it uses ‘ (acute quotes) instead of ' which messed me up briefly.

  • Hemanth Kelkar
    August 6, 2007 4:35 am

    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.

  • Praveen Barath
    August 7, 2007 7:01 am

    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

  • Praveen Barath
    August 8, 2007 2:46 am

    What we can use for MSSQL 2005

  • Kamlesh K Mishra
    October 16, 2007 11:48 pm

    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

  • Hi

    Is there any way of reading the regstry & getting to knw the SQL Server Instacnes on the particular box along with the version (2000 Or 2005)


  • 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)

  • 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.

  • 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!

      • Check this snapshot:

        Source Link:

      • Thanks again, that link took care of the error. I apologize for my lack of knowledge, trying to figure out some basics here. If I execute the function with Get-SQLserverKey the object returns nothing. How do I get it do display the $productKey?

      • Are you trying it on your local machine or you are connected to Server?
        Even I tried on server but ran into errors…

      • I am running this on the server. This is a 64bit server…I don’t know if that would have an affect on this script.

    • 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?

  • Thanks so much, it teally helped me

  • 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)

  • Sakinaka ka chhora
    February 16, 2012 7:12 pm

    Just checking

  • 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 :)

  • For SQL Server 2012:


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

  • wat is d use of this?

  • It worked for me on SQL 2012 when I fixed line 24 to $binArray = ($data.uValue)[0..66]
    Maybe it would help somebody else.

  • Ismail Tutumluer
    November 30, 2014 5:15 am

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

  • 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