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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

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"
Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

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"

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


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GrahamPemberyAuthor Commented:
All solutions are excellent.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.