- Set PATH and other environment variables in Windows 10
- Set environment variables from command prompt
- Set environment variable by Registry edit
- Set the Path and Environment Variables for Command-Line Builds
- PowerShell: Setting an environment variable for a single command only
- 7 Answers 7
- Setting and getting Windows environment variables from the command prompt?
- 6 Answers 6
- Set a persistent environment variable from cmd.exe
- 5 Answers 5
Set PATH and other environment variables in Windows 10
In older windows systems you had to navigate to Advanced System Settings in Control Panel to view, edit or add environmental variables.
- Windows XP — Right-click My Computer, and then click Properties → Advanced → Environment variables → Choose New, Edit or Delete.
- Windows 7 — Click on Start → Computer → Properties → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
In Windows 8 and 10, you can navigate to Advanced System Settings in a similar way.
- Windows 8 — Right click on bottom left corner to get Power User Task Menu → Select System → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
- Windows 10 — Right click on Start Menu to get Power User Task Menu → Select System → Advanced System Settings → Environment variables → Choose New, Edit or Delete.
However, in Windows 10 you can directly get to the Environment Variables window using Search the web and Windows box next to the Start menu. Type environment variables in Windows Search box which gives you two options in search results:
- Edit the system environment variables
- Edit environment variables for your account.
Choose either option and you can add, edit or delete environment variables like PATH.
Set environment variables from command prompt
You can set environment variables from Windows Command Prompt using the set or setx command. The set command only sets the environment variable for the current session. The setx command sets it permanently, but not for the current session. If you want to set it for current as well as future sessions, use both setx and set.
For example, you can set the PATH environment variable permanently (current and future sessions) as below:
To view the current path, run:
- By default setx sets the variable in the local environment (Under HKEY_Current_User Registry key). If you want to set the system variable (Under HKEY_LOCAL_MACHINE registry key), use the flag /m. Start the command prompt as administrator on Windows 10, right click on Start menu and select Command Prompt(Admin).
- The maximum value allowed for a enviroment variable is 1024 characters. So if your variable is long and you try to append to it with setx, you may get a truncated result.
Set environment variable by Registry edit
If your PATH variable is too long, then the best method would be to edit the registry.
For user environment variables, change the key HKEY_CURRENT_USER\Environment . For System Environment variables change
Add, Edit or Delete the environment variable you want to change, then reboot to activate the changes.
Set the Path and Environment Variables for Command-Line Builds
The Microsoft C++ (MSVC) command-line build tools require several environment variables that are customized for your installation and build configuration. When a C++ workload is installed by the Visual Studio installer, it creates customized command files, or batch files, that set the required environment variables. The installer then uses these command files to create shortcuts for the Windows Start menu to open a developer command prompt window. These shortcuts set up the environment variables for a specific build configuration. When you want to use the command-line tools, you can run one of these shortcuts, or you can open a plain command prompt window and then run one of the custom command files to set the build configuration environment yourself. For more information, see Use the MSVC toolset from the command line. To use the command files with a plain command prompt, see the section entitled Developer command file locations.
The MSVC command-line tools use the PATH, TMP, INCLUDE, LIB, and LIBPATH environment variables, and also use other environment variables specific to your installed tools, platforms, and SDKs. Even a simple Visual Studio installation may set twenty or more environment variables. Because the values of these environment variables are specific to your installation and your choice of build configuration, and can be changed by product updates or upgrades, we strongly recommend that you use a developer command prompt shortcut or one of the customized command files to set them, instead of setting them in the Windows environment yourself.
To see which environment variables are set by a developer command prompt shortcut, you can use the SET command. Open a plain command prompt window and capture the output of the SET command for a baseline. Open a developer command prompt window and capture the output of the SET command for comparison. A diff tool such as the one built into the Visual Studio IDE can be useful to compare the environment variables and see what is set by the developer command prompt. For information about the specific environment variables used by the compiler and linker, see CL Environment Variables.
Several command-line tools or tool options may require Administrator permission. If you have permission issues when you use them, we recommend that you open the developer command prompt window by using the Run as Administrator option. On Windows 10, right-click to open the shortcut menu for the command prompt window, then choose More, Run as administrator.
PowerShell: Setting an environment variable for a single command only
On Linux, I can do:
to call «myscript» with the environment variable FOO being set.
Is something similar possible in PowerShell, i.e. without having to first set the variable, call the command, and then unset the variable again?
To be more clear about my use case — I don’t want to use this as part of a script. Rather, I have a third-party script whose behavior I can control using environment variables, but, in this case, not command line arguments. So being able to alternate between typing
would just be very handy.
/.gitconfig, .git/config in each repository, or envars. Of those options, envars are clearly easier to set on-the-fly (and conveniently override the values in the files). So if I want to change my author name for one «git commit» in powershell, how to do it? – Mark Reed Jun 28 ’15 at 20:44
7 Answers 7
Generally, it would be better to pass info to the script via a parameter rather than a global (environment) variable. But if that is what you need to do you can do it this way:
The environment variable $env:FOO can be deleted later like so:
I got motivated enough about this problem that I went ahead and wrote a script for it: with-env.ps1
On the other hand, if you install Gow you can use env.exe which might be a little more robust than the quick script I wrote above.
2 easy ways to do it in a single line:
Just summarized information from other answers (thank you folks) which don’t contain pure one-liners for some reason.
To accomplish the equivalent of the Unix syntax, you not only have to set the environment variable, but you have to reset it to its former value after executing the command. I’ve accomplished this for common commands I use by adding functions similar to the following to my PowerShell profile.
So mycmd is some executable that operates differently depending on the value of the environment variable app_master . By defining cmd_special , I can now execute cmd_special from the command line (including other parameters) with the app_master environment variable set. and it gets reset (or even unset) after execution of the command.
Presumably, you could also do this ad-hoc for a single invocation.
It really should be easier than this, but apparently this isn’t a use-case that’s readily supported by PowerShell. Maybe a future version (or third-party function) will facilitate this use-case. It would be nice if PowerShell had a cmdlet that would do this, e.g.:
Perhaps a PowerShell guru can suggest how one might write such a cmdlet.
Setting and getting Windows environment variables from the command prompt?
I want to set an environment variable from the command prompt and then be able to access it globally (for instance, I should see it by going to System -> Environment Variables).
When I use the set command, it isn’t accessible in a new cmd session.
6 Answers 6
To make the environment variable accessible globally you need to set it in the registry. As you’ve realised by just using:
you are just setting it in the current process space.
According to this page you can use the setx command:
setx is built into Windows 7, but for older versions may only be available if you install the Windows Resource Kit
We can also use «setx var variable /M» to set the var to system environment variable level instead of user level.
Note: This command should be run as administrator.
/M for set system environment variable level instead of user level like @Minh Chau answer
RESTART command line (if you don’t restart command line, environment variable will not work)
You can use setx env var [/M] as mentioned above. If it doesn’t take effect you can use refreshenv to refresh environment variables. You don’t have to restart your computer, explorer.exe or your command prompt to do that.
Edit: apparantly refreshenv doesn’t come naturally with Windows, so here’s the source: https://pastebin.com/1fJqA0pT
Save as RefreshEnv.cmd and place it in a folder that’s included in your PATH environment variables
System variables can be set through CMD and registry For ex. reg query «HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment» /v PATH
All the commonly used CMD codes and system variables are given here: Set Windows system environment variables using CMD.
Open CMD and type Set
You will get all the values of system variable.
Type set java to know the path details of java installed on your window OS.
Set a persistent environment variable from cmd.exe
I have to set environment variables on different windows machines, but I don’t want to be bothered changing them manually by getting on the properties screen of «My Computer»
I want to do it from the command line, with a batch file. As far as I understand, using set will only change the variable for the processes I will call in the command window.
I want to set it definitely, so later, when running a new process, it will use those new settings I have set. Is there a way to do that from the command line ?
5 Answers 5
Use the SETX command (note the ‘x’ suffix) to set variables that persist after the cmd window has been closed.
For example, to set an env var «foo» with value of «bar»:
Though it’s worth reading the ‘notes’ that are displayed if you print the usage ( setx /? ), in particular:
2) On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.
3) On a remote system, variables created or modified by this tool will be available at the next logon session.
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string «Environment». This allows applications, such as the shell, to pick up your updates.
You will of course need admin rights to do this. I know of no way to broadcast a windows message from Windows batch so you’ll need to write a small program to do this.