How can I refresh the PATH environment variable in a Batch script
Asked Answered
S

4

6

I have a batch file "file.bat" that will call an installer with the following command:

msiexec.exe /i "%~dp0\installer.msi"

The installer will install a program and update the Path variable. While this works fine, the problem is when I try to start the program it's not found because apparently the PATH variable was not updated. I tried restarting the batch file from within itself with:

start cmd /c file.bat 

but it didn't work. Is there a way to refresh the PATH variable or maybe restart the batch file in a new process so that it detects the new environment?

PS: restarting the batch file manually works of course but it's not what I want.

Thanks.

Supplemental answered 4/10, 2016 at 15:26 Comment(4)
set path=%path%;C:\Some other folderTurmoil
I think it may be useful if you posted the entire file.bat.Insincerity
You have to read the system PATH value just modified by the installer directly from registry either with command reg or with command setx and update/replace local PATH with new system PATH. See for example answer on Why are other folder paths also added to system PATH with SetX and not only the specified folder path? how to get system PATH directly from registry.Amiss
Thank you for the suggestions. I ended up using the solution from here: #172088 It needs an extra file to work. It's weird that this can't be solved more easily!Supplemental
G
11

Easiest way, use Chocolatey (freeare). Then you will be able to reload PATH (with variable expansion) with a simple command:

refreshenv

Installation from cmd (requires admin rights):

@"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

Example usage:

> SET JAVA_HOME=c:/java/jdk6
> SET PATH=%JAVA_HOME%/bin
> ECHO %PATH%
c:/java/jdk6/bin

> SET JAVA_HOME=c:/java/jdk8
> refreshenv
Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
> echo %PATH%
c:/java/jdk8/bin
Guillermo answered 10/8, 2018 at 15:16 Comment(0)
L
3

IN Windows PowerShell exit and run again

Lambeth answered 15/4, 2018 at 16:45 Comment(0)
D
1

simple batch file that refreshes the %path% environment variable:

@echo off
echo.
echo Refreshing PATH from registry

:: Get System PATH
for /f "tokens=3*" %%A in ('reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path') do set syspath=%%A%%B

:: Get User Path
for /f "tokens=3*" %%A in ('reg query "HKCU\Environment" /v Path') do set userpath=%%A%%B

:: Set Refreshed Path
set PATH=%userpath%;%syspath%

echo Refreshed PATH
echo %PATH%
Denim answered 10/10, 2016 at 16:17 Comment(3)
It is a good answer J03L, but the problem is when I set path like this: JAVA_7 = C:\Program Files\Java\jdk1.7xxxx JAVA_8 = C:\Program Files\Java\jdk1.8xxxx JAVA_HOME = %JAVA_8% Path = %JAVA_HOME%\bin;%MAVEN_HOME%\bin Because this will generate a PATH with %JAVA_8%\bin;C:\windows\System32\WindowsPowerShell\v1.0\;C:\windows\idmu\common;etc. Do you think it's possible to change the batch file to generate a second level variable substituion on user variables? Just curious, still a nice answer tho!Vasodilator
For some reason, not all spaces in a path are preserved (i.e. the original path shows 'Program Files (x86)', but the set variable shows 'ProgramFiles (x86)' ).Soup
I believe this part set userpath=%%A%%B should really be set userpath=%%A %%B (notice additional space).Salyer
A
0

To reset the path within a Windows batch script, you can output the system environment variables from powershell and assign the output to the path as follows:

for /f "tokens=* usebackq" %%p in (powershell -Command "& {[System.Environment]::GetEnvironmentVariable('Path','Machine') + ';' + [System.Environment]::GetEnvironmentVariable('Path','User')}") do (set path=%%p)

Acetylate answered 31/7, 2023 at 21:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.