Fairly straightforward question. I need to update my PATH environment variable in Windows Server 2008 Core. Considering there's no GUI, how is this done from the command line?
Fairly straightforward question. I need to update my PATH environment variable in Windows Server 2008 Core. Considering there's no GUI, how is this done from the command line?
To make persistent changes use
setx
.For example, to add a folder to the end of the current path, use:
Your change won't be visible in the current
cmd.exe
session, but it will be in all future ones.SETX
also allows setting system environment variables on remote systems.Dealing with the Path variable is sticky as it is a combination of the system Path and user Path variables. The previous answers don't account for this. For example
will set the user Path to the entire current system path, plus user path, and then append ';C:\MyFolder' to that. If I had used
SETX /M PATH %PATH%;C:\MyFolder
then the system Path will get the current user Path added to it.Using
SETX
orSETX /M
is fine for any environment variable except the Path. Unfortunately, dealing with the Path variables is a pain as it involves updating registry and to be able to append a new value we must first copy the registry entry into an environment variable, append directory that we are adding to the path, and then write the results back into the registry.There's another issue which is that the Path variables are stored as REG_EXPAND_SZ strings and it's common for a system path to contain references to
%SystemRoot%
. This means whatever mechanism you use for reading, manipulating, and then writing the Path variable should ideally not expand anything.Finally, it's common for there to be no user Path variable meaning the code that updates the Path needs to account for the error you will get if you try to read a variable that does not exist.
Here's some example code that can update the Path by updating the values in the registry.
As it has always been done since DOS times:
from the Mueller book Administering Windows Server 2008 core - use WMIC
WMIC Environment Where Name="Path" SET VariableValue="C:\Temp;%PATH%
If you want to set a path or other environment variable with spaces in it, I found it easier to use
regedit
- which you can just start from the command prompt on Server Core.The system wide environment variables are in
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
and the user ones are inHKCU\Environment
.@user3347790's answer makes some valid points: a lot of the methods above are going to replace path with the expanded path (e.g.
%ProgramFiles%
or%SystemRoot%
or%UserProfile%
are going to be permanently expanded) and they are also going to combine machine and user paths into one. Based on that, adapting the code from that answer (if you need code) or just usingregedit
(if you just want to make a few changes by hand) might be safer...