相当简单的问题。我需要在 Windows Server 2008 Core 中更新我的 PATH 环境变量。考虑到没有 GUI,如何从命令行完成此操作?
答案1
要使更改持久化,请使用setx
。
例如,要将文件夹添加到当前路径的末尾,请使用:
SETX Path %Path%;C:\MyFolder
您的更改不会在当前cmd.exe
会话中显示,但会在所有未来的会话中显示。
SETX
还允许在远程系统上设置系统环境变量。
答案2
处理 Path 变量很棘手,因为它是系统 Path 和用户 Path 变量的组合。前面的答案没有考虑到这一点。例如
SETX PATH %PATH%;C:\MyFolder
会将用户路径设置为整个当前系统路径加上用户路径,然后将“;C:\MyFolder”附加到该路径。如果我使用了,SETX /M PATH %PATH%;C:\MyFolder
那么系统路径将添加当前用户路径。
对于除 Path 之外的任何环境变量,使用SETX
或SETX /M
都很好。遗憾的是,处理 Path 变量很麻烦,因为它涉及更新注册表,并且为了能够附加新值,我们必须先将注册表项复制到环境变量中,将我们要添加的目录附加到路径中,然后将结果写回到注册表中。
还有一个问题是 Path 变量存储为 REG_EXPAND_SZ 字符串,系统路径通常包含对 的引用%SystemRoot%
。这意味着无论使用何种机制来读取、操作和写入 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
答案3
就像自 DOS 时代以来一直做的那样:
SET PATH = C:\SomeDir
SET PATH = %PATH%;C:\AnotherDir
答案4
如果您想设置带有空格的路径或其他环境变量,我发现它更容易使用regedit
- 您只需从 Server Core 上的命令提示符启动即可。
系统范围的环境变量在 中HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
,用户环境变量在 中HKCU\Environment
。
@user3347790 的回答提出了一些有效的观点:上面的许多方法都将用扩展路径替换路径(例如%ProgramFiles%
或%SystemRoot%
或%UserProfile%
将永久扩展),并且它们还将机器和用户路径合并为一个。基于此,改编该答案中的代码(如果您需要代码)或仅使用regedit
(如果您只想手动进行一些更改)可能更安全...