Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 483
luffy Avatar asked Oct 04 '16 15:10

luffy


People also ask

How do you update Environment Variables?

In the Settings window, under Related Settings, click Advanced system settings. On the Advanced tab, click Environment Variables. Click New to create a new environment variable. Click Edit to modify an existing environment variable.

Is there a command to refresh Environment Variables from the Command Prompt in Windows?

Environment Vars (like PATH) have changed. Close/reopen your shell to see the changes (or in powershell/cmd.exe just type `refreshenv`).

How do you refresh an environment variable in PowerShell?

To refresh the environment variables in PowerShell, retrieves the environment variables and assign them $Env:Path to reload the environment variable path in the PowerShell. After reloading the path in PowerShell, you don't need to restart the PowerShell ISE or terminal.


1 Answers

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
like image 153
Arkadiusz Przechodzki Avatar answered Sep 25 '22 01:09

Arkadiusz Przechodzki