Get registry key value in Batch File

I am creating a batch file which will decide whether to install .NET framework 4.5.2 based on following registry key value.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\Release

When .NET framework 4.5.2 is installed its  value is #379893.

When .NET framework is not installed this key is not present. I would like the batch file in following format.

If Above key is present on machine - check its value. If value is less than 379893 GOTO : InstallNetFramework

If above key is present on machine but its value is more than 379893  GOTO :DoNotInstallAnything

If above key is not present on machine GOTO : InstallNetFramework
Who is Participating?
The patch needs the full path to the script's folder, and msiexec is in the system32 folder ("%~dp0" will expand to the script's Drive and Path, including a trailing backslash):
START "" /WAIT "%Systemroot%\system32\msiexec.exe" /p "%~dp0TestPatch.msp"

Open in new window

Try this:
@echo off
set regNet=HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full
set InstallIfLess=379893
set Release=
for /f "tokens=3" %%a in ('reg.exe query "%regNet%" /v "Release" 2^>NUL ^| find.exe /i "Release"') do (set /a Release=%%a)
if not defined Release (
	echo .NET v4 not found, will install.
	goto InstallNetFramework
echo .NET v4 found; release: %Release%
if %Release% lss %installIfLess% (
	echo .NET v4 in an older version than %installIfLess%% found, will install.
	goto InstallNetFramework
echo .NET v4 is current, leaving.
goto :eof

echo Please wait while installing ...

Open in new window

*Edit* - I saw oBdA's post after mine but decided to leave my script included.  It's essentially the same thing, just includes a UAC check and the logic for determining the goto structure is a tad different.
@echo off
setlocal EnableDelayedExpansion
set version=

:: Start the batch file
echo .NET Version Checker: Starting (version 1.0)

:: Check for administrative permissions
echo .NET Version Checker: Checking for administrative privileges
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"

:: If error flag is not equal to 0, we do not have admin.
if '%errorlevel%' NEQ '0' (
	echo .NET Version Checker: Requesting administrative privileges
	goto _UACPrompt
) else ( 
	echo .NET Version Checker: User has administrative privileges
	goto _IsAdmin

:: Creating a temporary vbscript to rerun this batch file while requesting Administrative privileges
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\adminprompt.vbs"
if "%1" NEQ "" ( 
	echo UAC.ShellExecute "%~s0", "%1 %2", "", "runas", 1  >> "%temp%\adminprompt.vbs" 
) else ( 
	echo UAC.ShellExecute "%~s0", "", "", "runas", 1  >> "%temp%\adminprompt.vbs" 
exit /B

:: Just continue processing the batch file, the user already has administrative rights.
if exist "%temp%\adminprompt.vbs" ( del "%temp%\adminprompt.vbs" >nul 2>&1 )
pushd "%CD%"
CD /D "%~dp0"

:: Checking .NET Framework Version installed
set qry="HKLM\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full" /v Release
echo .NET Version Checker: Checking .NET Framework Version
for /f "skip=2 tokens=*" %%a in ('reg query %qry% ^>nul 2^>^&1^| find /i "Release"') do ( set /a version=%%a )
if "%version%" equ "" goto _InstallNetFramework
if %version% geq 379893 goto _DoNotInstallAnything

:: Do your installation of the .NET Framework here
echo .NET Version Checker: Installing .NET Framework
goto _eof

:: Continue your processing here
echo .NET Version Checker: .NET Framework Version Found


Open in new window

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

GrahamPemberyAuthor Commented:
Hi Saige,

Thank you for your kind support.  
I have placed .NET framework setup and batch file in a folder on desktop. I run batch file from there and it works perfectly well.

But when I try to run batch file from the network share I get error that this setup file not found. I have inserted my .NET framework installation line as below. Perhaps I need some special handling when the setup is run from network drive.

:: Do your installation of the .NET Framework here
echo .NET Version Checker: Installing .NET Framework

START "" /WAIT "NDP452-KB2901907-x86-x64-AllOS-ENU /passive /norestart"
The quotes are set incorrectly, and it never hurts to pass the full path to the installation source.
START "" /WAIT "%~dp0NDP452-KB2901907-x86-x64-AllOS-ENU" /passive /norestart

Open in new window

GrahamPemberyAuthor Commented:
Hi saige,

I found another issue with batch file. After installing .NET framework 4.5.2 the batch file is not correctly able to read the registry and again starts installing .NET framework 4.5.2.

Please have a look again.


GrahamPemberyAuthor Commented:
Hi OBda,

Thanks. Your suggestion has worked. But its not working for following follow up installation.

START "" /WAIT "%~dp0msiexec.exe" /p "TestPatch.msp"

GrahamPemberyAuthor Commented:
All solutions are excellent.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.