Friday 8 February 2019

command line - Setting or modifying a (system wide) environment variable in cmd.exe


I am looking for a convenient way to add and/or modify and/or delete an environment variable from the command line. Particularly, I find myself at times in situations when I have to add a few variables in cmd.exe.


I'd be grateful if someone showed me a non-GUI way to modify (that is: to add a new directory to) the %PATH% variable.


The change should be be permanent, not just for duration of the cmd session.



Answer



The Old School method of directly manipulating registry variables with the reg command was on the money. Here's how you do it:


reg add HKCU\Environment /v PATH /d "%addonpath%;%path%" /f

Throw that into a one line script called apath.bat that looks like this:


@echo off
reg add HKCU\Environment /v PATH /d "%~dp0;%path%" /f

Then, all you need to provide is the path of the new directory you're adding when calling the script and you're dialed in:


e.g: apath.bat %addonpath%


Although Hinch is right. The best way to do it if you're using Vista or above is to use the SETX command which is designed to allow us to propagate environment variables without the risk of directly manipulating the registry with with the reg command that could save you your machine if you manipulate ENV variables enough to use it on the fly.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...