title: How to retrieve SQL Product Key from an existing installation
body: <p>We want to retrieve product key of SQL server. 
  We have multiple versions of SQL and understood that product key's path changes for each version. We don't want to hard code the path hence searching for a generic logic. 
  As every version has different folder structure to store product key, we are using Switch statement as</p>
  
  <pre><code>switch (majorVersion)
          {
              case "8":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\80\Registration";              
                  break;
              case "9":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\90\ProductId";        
                  break;
              case "10":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\100\Tools\ClientSetup";           
                  break;
              case "11":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\110\Tools\ClientSetup";                
                  break;
              case "12":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\120\Tools\ClientSetup";
                  break;
              case "13":
                   registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\130\Tools\Setup";
                  break;
              case "13.1":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\130\Tools\Setup\Client_Components_Full\1033";
                  break;
              case "14":
                  registryPath = @"SOFTWARE\Microsoft\Microsoft SQL Server\140\Tools\Setup";
                  break;
              default:
                  registryPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion";
                  break;
          }
  </code></pre>
  
  <p>Want to avoid this switch and retrieve key without specifying hard coded path. </p>
  
  <p>How can we get correct product key of any installed sql version?</p>
  
  <p>( Looking for sql script solution only and not power shell.)</p>