Get SQL Server Product Keys for Local and Remote Servers

Using a string of servers, a text file, or Central Management Server to provide a list of servers, this script obtains the product key and versions for all installed instances on a server or cluster. Supports SQL Server 2005-2014.

 
 
 
 
 
4.6 Star
(18)
37,676 times
Add to favorites
Databases
6/2/2015
E-mail Twitter del.icio.us Digg Facebook
  • Fixed For SQL Server 2017 Newer Keys With Letter N
    2 Posts | Last post July 22, 2019
    • After finding out this script does not work with the new encryption for keys with the letter N I adjusted one of the Win8 decryption functions to work with this script. NOTE: I have not tested with other versions or keys without the letter N.
      
      Function Unlock-SQLServerKey {
       param(
        [Parameter(Mandatory = $true)]
        [byte[]]$data,
        [int]$version
       )
       try {
        $Key = ($data)[0..66]
        if ($version -ge 11) { $Keyoffset = 0 } else { $Keyoffset = 52 }
        
        $isNKey = [int]($Key[14]/6) -bAND 1
        $HF7 = 0xF7
        $Key[14] = ($Key[14] -bAND $HF7) -bOR (($isNKey -bAND 2) * 4)
        $i = 24
        [String]$Chars = "BCDFGHJKMPQRTVWXY2346789"
        
        do {
         $Cur = 0 
         $X = 14
         do {
          $Cur = $Cur * 256    
          $Cur = $Key[$X + $Keyoffset] + $Cur
          $Key[$X + $Keyoffset] = [math]::Floor([double]($Cur/24))
          $Cur = $Cur % 24
          $X = $X - 1 
          } while($X -ge 0)
         $i = $i- 1
         $KeyOutput = $Chars.SubString($Cur,1) + $KeyOutput
         $last = $Cur
        } while($i -ge 0)
        
        $Keypart1 = $KeyOutput.SubString(1,$last)
        $Keypart2 = $KeyOutput.Substring(1,$KeyOutput.length-1)
        
        if($last -eq 0 ) {
         $KeyOutput = "N" + $Keypart2
        }
        else {
         $KeyOutput = $Keypart2.Insert($Keypart2.IndexOf($Keypart1)+$Keypart1.length,"N")
        }
        
        $a = $KeyOutput.Substring(0,5)
        $b = $KeyOutput.substring(5,5)
        $c = $KeyOutput.substring(10,5)
        $d = $KeyOutput.substring(15,5)
        $e = $KeyOutput.substring(20,5)
        $keyproduct = $a + "-" + $b + "-"+ $c + "-"+ $d + "-"+ $e
       }
       catch { $keyproduct = "Cannot decode product key." }
       return $keyproduct
      }
    • Thanks @HawkmanAZ, your solution working for me with a letter N in Product Key
  • Great powershell script
    1 Posts | Last post July 03, 2019
    • Thank you very much.
  • Updates for latest SQL versions.
    1 Posts | Last post May 09, 2019
    • First of all, I'd like to say many thanks to Chrissy for really universal script and to HawkmanAZ for the updated algorithm. That's awesome job. In my turn, I compiled everything together, including the original Jakob Bindslet's script, and wrote it in C#. You can find the final version tested with SQL 2017 here: https://maxcore.home.blog/2019/05/09/accidental-task-recover-sql-server-product-key-from-existing-installation/
  • 2016/2017 compatible version
    4 Posts | Last post March 06, 2019
    • This works for me with 2016/2017 -- adding/replacing lines for key lookups on 2016/2017 sql versions:
      
      13 { $key = "$basepath\130\Tools\ClientSetup\DigitalProductID"; $sqlversion = "SQL Server 2016 $servicePack" }
      14 { $key = "$basepath\140\Tools\ClientSetup\DigitalProductID"; $sqlversion = "SQL Server 2017 $servicePack" }
      
      Output:
      SQL Instance      SQL Version         SQL Edition               Product Key
      ------------      -----------         -----------               -----------
      xxxxxxxxxxxx      SQL Server 2016 SP1 Standard Edition (64-bit) xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
      
      Cheers ;) 
    • I try for 2017 but get this error after calling the function.
      WARNING: No instances found on SERVER-NAME. Moving on. =(
    • Thank you, it works
    • Thanks, works just fine with both SQL Server 2016 installations I've tested it on!
  • No output
    3 Posts | Last post November 16, 2018
    • I can't get any output whatsoever from this script
    • try this; assuming you download the file to downloads folder
      
      Unblock-File -Path $ENV:USERPROFILE\DOWNLOADS\Get-SqlServerKeys.psm1 
      IMPORT-MODULE $ENV:USERPROFILE\DOWNLOADS\Get-SqlServerKeys.psm1
      Get-SqlServerKeys .|FL
    • Leave it up to the guys at MSFT, already 7 versions of Powershell. If you want a command set that doesnt change every week I would suggest getting off this bandwagon.. #linux
  • 2017 Version Not Working With Letter 'N'
    1 Posts | Last post May 12, 2018
    • My product key has the letter 'N' in it and there is no letter 'N' in the character array to decode to. You can't simply add the 'N' because that changes the base encoding from 24 to 25. Any thoughts here?
  • Notes for PowerShell Beginners and SQL 2016 Users
    2 Posts | Last post April 04, 2018
    • For those getting into PowerShell and experimenting with SQL 2016 here is an addition for this post. Keep in mind I was during this on my home domain which isn't very intricate but hope it still helps. 
      
      -Remember to run PowerShell as administrator
      -If you are working with PowerShell for the first time run 
       Set-ExecutionPolicy Unrestricted.
            o-See (https://technet.microsoft.com/en-us/library/ee176961.aspx).
      -Change directories where you saved the Get-SqlServerKeys.psm1
      -Type in Import-Module .\Get-SqlServerKeys.psm1
      -Type Get-SqlServerKeys
      
      This should be working for you now but if you get errors about access denied or product not supported add the following code (Scroll to line 172 and just paste in below so these line up with lines 173 and 174) in the module (remember to save!) then repeat above steps:
      
      13 { $key = "$basepath\130\Tools\Setup\DigitalProductID"; $sqlversion = "SQL Server 2016 $servicePack" }
      14 { $key = "$basepath\140\Tools\Setup\DigitalProductID"; $sqlversion = "SQL Server 2016a $servicePack" }
      
      Side note for above: I had a bit of a mess experimenting with different SQL and SharePoint setups. So I just added to the different variations of 130 and 140 that I saw in my SQL Server directory. The script did a good job of actually finding the database that was live.
      
      Hope this helps those that were having some issues getting started with it. And thanks for the post!
      
      
      
      
    • Here is the code you need for SQL Server 2017 because the key is stored in different Registry location:
      
      14 { $key = "$basepath\140\Tools\ClientSetup\DigitalProductID"; $sqlversion = "SQL Server 2017" 
  • How does one execute this?
    3 Posts | Last post February 23, 2017
    • I'm not a coder.  How does one actually execute this?  Doing an inventory/audit and I need something that works and returns correct information.  Any help would be appreciated!
    • Keep getting Can't connect to <machine> or access denied.  Moving on  error.  How advise?
      
    • Hi THE_True_Josh - this will only work on SS2014 down, not SS2016 - to run it, you open the file in Notepad, select all, copy the code, open up powershell, paste the code, click enter, then after that, you type Get-SqlServerKeys and press enter.
      
      To add to The Duder's comment, it would be great if this could work for 2016!!!
  • SQL Server 2016 Support
    1 Posts | Last post October 21, 2016
    • I imagine this script has been abandoned at this point, but if you ever come back to it, any chance you can add support for SQL Server 2016?
      
      Thanks
  • How can the credentials be changed
    1 Posts | Last post September 20, 2016
    • Due to our environment I have to use a range of Usernames and passwords for a variety of domains.  I tried using gwmi but got seriously lost trying to convert the properties and didn't know how to continue.  Thanks for any help
      
      $user = "domain\user"
      $pass = ConvertTo-SecureString "UsersPwd" -AsPlainText -Force
      
      $reg=Get-WmiObject -List -Namespace root\default -ComputerName $server -Credential $cred | Where-Object {$_.Name -eq "StdRegProv"}
1 - 10 of 11 Items