Detect Microsoft .Net Framework versions

I use the following code to determine which specific version of the Microsoft .Net Framework 4.0 is installed on my SQL Server. It detects versions from 4.5 to 4.7, and can be easily extended as new versions are release by adding new rows to the #NetFrameworkVersions table. Be aware, if you run this on a server that does not have at least version 4.5, you’ll see no results.

IF OBJECT_ID(N'tempdb..#NetFrameworkVersions', N'U') IS NOT NULL
DROP TABLE #NetFrameworkVersions;
CREATE TABLE #NetFrameworkVersions
(
    ReleaseNum int NOT NULL
    , ReleaseName varchar(100) NOT NULL
);
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (394802, '4.6.2');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (394806, '4.6.2');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (394254, '4.6.1');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (394271, '4.6.1');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (393295, '4.6');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (393297, '4.6');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (379893, '4.5.2');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (378675, '4.5.1');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (378758, '4.5.1');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (378389, '4.5');
INSERT INTO #NetFrameworkVersions (ReleaseNum, ReleaseName) VALUES (460805, '4.7');

IF OBJECT_ID(N'tempdb..#NetFrameworkVersionsInstalled', N'U') IS NOT NULL
DROP TABLE #NetFrameworkVersionsInstalled;
CREATE TABLE #NetFrameworkVersionsInstalled
(
    ReleaseNum int NOT NULL
    , VersionNum varchar(100) NOT NULL
    , IsFullInstall bit NOT NULL
);

DECLARE @valint int;
DECLARE @valstr nvarchar(100);

SET @valint = NULL;
SET @valstr = NULL;
EXEC sys.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client', 'Release', @valint OUTPUT;
IF @valint IS NOT NULL 
BEGIN
    EXEC sys.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client', 'Version', @valstr OUTPUT;
    INSERT INTO #NetFrameworkVersionsInstalled (ReleaseNum, VersionNum, IsFullInstall)
    SELECT @valint, @valstr, 0;
END

SET @valint = NULL;
SET @valstr = NULL;
EXEC sys.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Release', @valint OUTPUT;
IF @valint IS NOT NULL 
BEGIN
    EXEC sys.xp_regread 'HKEY_LOCAL_MACHINE', 'SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full', 'Version', @valstr OUTPUT;
    INSERT INTO #NetFrameworkVersionsInstalled (ReleaseNum, VersionNum, IsFullInstall)
    SELECT @valint, @valstr, 1;
END

DECLARE @WindowsRelease varchar(100);
SET @WindowsRelease  = 'unknown';
IF EXISTS (SELECT 1 FROM master.sys.all_objects ao WHERE ao.name = 'dm_os_windows_info')
BEGIN
    SET @WindowsRelease = (SELECT TOP(1) dowi.windows_release FROM sys.dm_os_windows_info dowi);
END

SELECT TOP(1) ServerName = @@SERVERNAME
    , SQLServerBuildCLR = SERVERPROPERTY('BuildClrVersion')
    , nfv.ReleaseName
    , nfvi.IsFullInstall
    , WindowsRelease = @WindowsRelease
    , ProductVersion = SERVERPROPERTY('ProductVersion')
FROM #NetFrameworkVersionsInstalled nfvi
    LEFT JOIN #NetFrameworkVersions nfv ON nfvi.ReleaseNum = nfv.ReleaseNum
ORDER BY nfvi.IsFullInstall DESC;

The code in this post is based upon the details provided by Microsoft here.