从命令行更新环境variables(Windows 2008 Server Core)

相当直接的问题。 我需要在Windows Server 2008 Core中更新我的PATH环境variables。 考虑到没有GUI,这是如何从命令行完成的?

从Mueller书籍pipe理Windows Server 2008核心 – 使用WMIC

WMIC环境其中Name =“Path”SET VariableValue =“C:\ Temp;%PATH%

要使用setx进行持久更改。

例如,要将文件夹添加到当前path的末尾,请使用:

 SETX Path %Path%;C:\MyFolder 

您的更改在当前cmd.exe会话中将不可见,但会在将来的所有会话中进行更改。

SETX还允许在远程系统上设置系统环境variables。

从DOS时代开始一直这样做:

 SET PATH = C:\SomeDir SET PATH = %PATH%;C:\AnotherDir 

处理Pathvariables是粘性的,因为它是系统Path和用户Pathvariables的组合。 以前的答案没有考虑到这一点。 例如

 SETX PATH %PATH%;C:\MyFolder 

会将用户path设置为整个当前系统path,再加上用户path,然后将“; C:\ MyFolder”附加到该path。 如果我已经使用了SETX /M PATH %PATH%;C:\MyFolder那么系统path将获得当前添加的用户path。

使用SETXSETX /M适用于除path以外的任何环境variables。 不幸的是,处理Pathvariables是一个痛苦,因为它涉及更新registry,并且能够追加一个新值,我们必须首先将registry项复制到一个环境variables中,追加我们添加到path中的目录,然后编写结果返回到registry中。

还有另一个问题,即Pathvariables存储为REG_EXPAND_SZstring,系统path包含对%SystemRoot%引用是很常见的。 这意味着无论用什么机制来阅读,操作,然后编写Pathvariables,理想情况下都不应该展开任何东西。

最后,通常没有用户Pathvariables,这意味着更新Path的代码需要考虑如果您尝试读取不存在的variables时将会遇到的错误。

以下是一些示例代码,可以通过更新registry中的值来更新Path。

 @echo off setlocal ENABLEDELAYEDEXPANSION rem rem Add c:\bin to the path rem rem rem rem There are two values we can use for the PATHKEY. The one that starts rem with HKEY_LOCAL_MACHINE sets the path that's used by all processes and rem users on the system. The PATHKEY that starts with HKEY_CURRENT_USER rem updates the part of the Path that only visible to processes running rem under the current user account. rem set PATHKEY=HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment set PATHKEY=HKEY_CURRENT_USER\Environment set PATHVAR=Path set TMPFILE=%TEMP%\addpath.txt set ADDPATH=c:\bin rem rem Read the Path value from the registry into a file. I could have coded rem this with no temporary file by using: rem for /f "delims=" %%i in ('REG QUERY "%PATHKEY%" /v "%PATHVAR%" 2>nul:') do set XLINE=%%i rem However, having the temporary file was useful for debugging and as rem updating the path is something we don't often do we don't care if rem doing so is a bit slower. rem REG QUERY "%PATHKEY%" /v "%PATHVAR%" >"%TMPFILE%" 2>nul: if errorlevel 1 goto :newpath rem rem REG QUERY outputs several lines. We only care about the last non-blank line rem Fortunately, a 'for' loop ignores blank lines. rem for /f "delims=" %%i in (%TMPFILE%) do set XLINE=%%i rem rem Extract the value from the Path variable. Here's what we expect to see rem in XLINE though with with spaces shown as ~ symbols: rem rem ~~~~Path~~~~REG_EXPAND_SZ~~~~Path-is-here..." rem rem See below for other ways we can extract the path value from XLINE. rem for /f "tokens=1,2,* delims= " %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k rem rem Append an element to the Path. rem set NEWPATH=!XVALUE!;!ADDPATH! rem rem Update the path rem REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!NEWPATH!" /f goto :done rem rem The Path variable does not exist and so create it rem :newpath REG ADD "%PATHKEY%" /v "%PATHVAR%" /t REG_EXPAND_SZ /d "!ADDPATH!" goto :done rem rem Delete the temporary file. rem :done del "%TMPFILE%" endlocal goto :eof rem rem Here are some variations for parsing XLINE to extract the value. rem rem rem Quick and dirty method. It takes advantage of that REG QUERY returns a rem line with four spaces, the variable name which we know is four rem characters, four spaces, the variable type which we know is rem REG_EXPAND_SZ and is 13 characters, four spaces, and then the value. As rem some systems may be using REG_SZ Path strings the quick and dirty method rem seems like a bad idea. rem set XVALUE=!XLINE:~29! rem rem One flaw with the method I used in the code above is that you are rem allowed to have spaces in variable names. Here's a slight modification rem to the code to support spaces in variable names. It takes advantage of rem the fact that REG VIEW always puts four spaces each of the fields. We rem first translate instances of four spaces into vertical bars and use that rem character as the delimiter when parsing the line. rem rem I could have used any character as the delimiter as long as it's one rem that will not appear in the variable name, the variable type, or as the rem first character(s) of the variable's value. Some people use a tab here. rem I chose not to do that in this example as the tabs are not visible. rem rem The code still has a flaw in that it will fail if the variable name rem contains four consecutive spaces. rem set XLINE=!XLINE: =^|! for /f "tokens=1,2,* delims=|" %%i in ("!XLINE!") do set VARNAME=%%i & set VARTYPE=%%j & set XVALUE=%%k