{"id":5407,"date":"2025-11-24T18:06:18","date_gmt":"2025-11-24T18:06:18","guid":{"rendered":"https:\/\/leeryanrs.com\/?p=5407"},"modified":"2025-11-24T18:06:18","modified_gmt":"2025-11-24T18:06:18","slug":"sql-server-license-retrieval","status":"publish","type":"post","link":"https:\/\/leeryanrs.com\/?p=5407","title":{"rendered":"SQL Server License Retrieval"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code># Confirmed working for Windows versions of SQL Server 2016-2019\nfunction GetSqlServerProductKey($InstanceName=\"MSSQL13.MSSQLSERVER\") {\n    $localmachine = &#91;Microsoft.Win32.RegistryHive]::LocalMachine\n    $defaultview = &#91;Microsoft.Win32.RegistryView]::Default\n    $reg = &#91;Microsoft.Win32.RegistryKey]::OpenBaseKey($localmachine, $defaultview)\n    $key = \"SOFTWARE\\Microsoft\\Microsoft SQL Server\\$InstanceName\\Setup\"\n    $encodedData = $reg.OpenSubKey($key).GetValue(\"DigitalProductID\")\n    $reg.Close()\n \n    try {\n        $binArray = ($encodedData)&#91;0..66]\n        $productKey = $null\n \n        $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\"\n \n        $isNKey = (&#91;math]::truncate($binArray&#91;14] \/ 0x6) -band 0x1) -ne 0;\n        if ($isNKey) {\n        $binArray&#91;14] = $binArray&#91;14] -band 0xF7\n        }\n \n        $last = 0\n \n        for ($i = 24; $i -ge 0; $i--) {\n            $k = 0\n            for ($j = 14; $j -ge 0; $j--) {\n                $k = $k * 256 -bxor $binArray&#91;$j]\n                $binArray&#91;$j] = &#91;math]::truncate($k \/ 24)\n                $k = $k % 24\n            }\n            $productKey = $charsArray&#91;$k] + $productKey\n            $last = $k\n        }\n \n        if ($isNKey) {\n            $part1 = $productKey.Substring(1, $last)\n            $part2 = $productKey.Substring(1, $productKey.Length-1)\n            if ($last -eq 0) {\n                $productKey = \"N\" + $part2\n            }\n            else {\n                $productKey = $part2.Insert($part2.IndexOf($part1) + $part1.Length, \"N\")\n            }\n        }\n \n        $productKey = $productKey.Insert(20, \"-\").Insert(15, \"-\").Insert(10, \"-\").Insert(5, \"-\")\n    } \n    catch {\n        $productkey = \"Cannot decode product key.\"\n    }\n \n    $productKey\n}<\/code><\/pre>\n\n\n\n<p>Change the instance name to what is displayed in the registry<\/p>\n\n\n\n<p>Then run the below command in powershell, after execting the function creation script above ofcourse.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GetSqlServerProductKey($InstanceName=\"MSSQL13.MSSQLSERVER\")<\/code><\/pre>\n\n\n\n<p>Adapt the instance name to what is required.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Change the instance name to what is displayed in the registry Then run the below command in powershell, after execting the function creation script above ofcourse. Adapt the instance name to what is required.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-5407","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/5407","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5407"}],"version-history":[{"count":1,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/5407\/revisions"}],"predecessor-version":[{"id":5408,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=\/wp\/v2\/posts\/5407\/revisions\/5408"}],"wp:attachment":[{"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5407"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5407"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/leeryanrs.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5407"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}