Last active
December 6, 2015 13:53
-
-
Save bburns/d1572eee7be76b1dd5e3 to your computer and use it in GitHub Desktop.
Set an environment variable value in the current environment *and* the Windows registry.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: setr | |
:: Set an environment variable in the current session and the registry. | |
:: https://gist.github.com/bburns/d1572eee7be76b1dd5e3#file-setr-bat | |
:: -------------------------------------------------------------------------------- | |
:: Set, delete, or show value of an environment variable in the local | |
:: environment *and* the user registry. | |
:: Combines set, setx, and reg commands. | |
:: Todo: | |
:: add -m option to specify machine registry keys instead of user | |
:: set uvar="HKCU\Environment" | |
:: set mvar="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" | |
:: For more information see | |
:: http://stackoverflow.com/a/18899441/243392 | |
@echo off | |
rem no arguments: show help | |
rem note: can't use :: comments near the if statements - must use rem | |
if "%1" == "" ( | |
echo. | |
echo SETR | |
echo Set, delete, or show value of an environment variable in the local | |
echo environment *and* the registry. Combines set, setx, and reg commands. | |
echo. | |
echo Usage: SETR [-d] variable [value] | |
echo. | |
echo SETR variable show value | |
echo SETR variable value set value | |
echo SETR -d variable delete variable | |
echo. | |
echo Note: Currently only the user registry is used, not the system registry. | |
echo. | |
echo To pass a value that includes semicolons or equal signs, e.g. a Java | |
echo classpath, enclose the value in double quotes. | |
echo. | |
rem -d option: delete variable | |
) else if "%1" == "-d" ( | |
:: delete variable (just clear it - can't actually delete it) | |
set %2= | |
:: /f forces the delete without asking for confirmation | |
reg delete HKCU\Environment /v %2 /f | |
echo. | |
rem one value: show value | |
) else if "%~2" == "" ( | |
:: show value | |
set %1 | |
reg query HKCU\Environment /v %1 | |
echo. | |
rem two values: set value | |
) else ( | |
:: set value | |
set %1=%~2 | |
echo %1 -^> %~2 | |
setx %1 %~2 | |
reg query HKCU\Environment /v %1 | |
echo. | |
) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment