Find if .NET Framework 4.0 is installed – using command line

.NET Framework 4.0

From cmd.exe (command line) one can find the version(s) of .NET Framework installed on Windows 7.

c:\> reg query "hklm\software\microsoft\net framework setup\ndp"

To find if a particular version was installed, such as .NET Framework 4.0 Full, one could do

c:\> reg query "hklm\software\microsoft\net framework setup\ndp\v4\Full" | findstr /i install
    Install    REG_DWORD    0x1
    InstallPath    REG_SZ    C:\windows\Microsoft.NET\Framework64\v4.0.30319\

What about using a quick and dirty batch file?

@echo off
setlocal

:: remember data from for loop if needed
set myvar=1

:: 1 indicates an explicit error
set response=1

:: check if .NET framework is already installed
for /f "tokens=*" %%a in (
    'reg query "hklm\software\microsoft\net framework setup\ndp\v4\full" 2^> nul ^| findstr /i install'
) do (
    set response=%errorlevel%
)

:: determine if .NET framework should be installed
if %response% equ 0 (
    echo Great! .NET Framework 4.0 Full is already installed
) else (
    echo Installing .NET Framework 4.0 Full
    .\dotNetFx40_Full_x86_x64.exe /passive /promptrestart

    :: check if installation succeeded	
    if %errorlevel% equ 0 (
        echo .NET Framework 4.0 Full installed successfully
    ) else (
        echo Could not install .NET Framework 4.0 Full
        pause
        exit /b
    )
)

pause
exit /b