Windows – How to refresh the PATH variable from the registry, without a reboot, logoff, or restarting explorer

batch fileenvironment-variableswindowswindows-registry

I've made some changes to the %PATH% variable in the registry. Now, I'd like to see those changes applied without having to go so far as a logoff, reboot, or reload of Explorer. Is there a way this can be done?

I'd rather do this via some sort of command that can be put at the end of a .BAT file, and don't want to use any tools other than those that come with the OS in a fresh install. This needs to be minimally compatible with Windows XP SP3, and should work all the way up to Windows 7 x64 and Server 2008 R2.

Best Answer

  • Change either User or System PATH in System Properties.
  • Running this batch file pulls the new PATH variables with a REG query.
  • The FOR commands parse the PATH variables from the REG results.
  • The current PATH is updated to the registry values.
  • I use ConEmu for my consoles and it runs this batch file on each new console to refresh the PATH so a reboot isn't necessary.

@echo off
echo.
echo Refreshing PATH from registry

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

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

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

echo Refreshed PATH
echo %PATH%

```

The task Commands parameter in ConEmu launches C:\Windows\System32\cmd.exe with the /k switch to run the refreshpath.cmd above and then remain. That updates the path and leaves the console open.

C:\Windows\System32\cmd.exe /k refreshpath.cmd

ConEmu Task settings

Related Question