系统和用户环境变量(System and User Environment Variables)一直是日常Windows 操作系统(Windows OS)用户讨论的复杂话题。他们在做什么?网上有个教程说要加个PATH变量,那是什么?我在哪里可以找到这些变量?所有这些问题都将在这篇文章中得到解答。我们试图为您提供系统(System)和用户环境变量(User Environment Variables)是什么以及如何使用它们的高级概述。
在这篇文章中,我们将讨论环境变量(Environment Variables)、系统(System)和用户环境变量(User Environment Variables)以及如何添加和修改它们。然后我们将继续讨论动态系统变量(Dynamic System Variables),看看如何在命令提示符下使用它们。
什么是Windows 操作系统(Windows OS)中的环境变量(Variable)
环境变量(Environment Variable)由两个单独的词“环境(Environment)”和“变量(Variable)”组成。让我们先讨论“变量”。这意味着实体可以存储一个值,并且因计算机而异。Windows为应用程序执行和执行操作提供了一个“环境(Environment)”,这就是第一个词的组成部分。结合两者,环境(Environment)变量是那些存储环境提供的值的动态对象。现在环境提供的值可以帮助其他程序获取有关系统的一些关键信息。就像有一个名为“windir”的环境变量,它对应于Windows所在的目录(Windows)已安装。要查看实际情况,请打开资源管理器窗口并在地址栏中输入“ %windir% ”。Windows安装文件夹将打开。
非常相似,您可以在其他程序和脚本中使用“windir”变量来引用Windows目录。(Windows)还有许多其他变量可以访问,“ TEMP ”或“TMP”是指向存储所有临时文件的目录的变量。最流行的“路径(Path)”变量是指向包含可执行文件的目录的变量。这样您就可以从任何其他目录中的命令提示符运行程序。(Command Prompt)我们稍后在这篇文章中解释了Path 。当您开发某些东西或大量使用 shell 时,所有这些变量都会派上用场。
什么是系统(System)和用户环境变量(User Environment Variables)
非常类似于注册表(Registry)在Windows上的工作方式,我们有系统(System)和用户环境变量(User Environment Variables)。系统变量在系统范围内被接受,并且不会因用户而异。然而(Whereas),用户环境(User Environments)的配置因用户而异。您可以在用户下添加变量,以便其他用户不受它们的影响。
仅供您参考,因为我们正在深入讨论该主题。系统变量在(System Variables)用户变量(User Variables)之前被评估。因此,如果有一些用户变量与系统变量同名,则将考虑用户变量。Path变量以不同的方式生成。有效路径(Path)将是附加到系统路径变量的(System Path)用户路径(User Path)变量。因此条目的顺序将是系统条目,然后是用户条目。
如何添加和修改环境变量
在我们深入之前的一个小警告。创建系统还原点,并尽量不要篡改为您的系统配置的现有设置。除非你对自己的行为非常确定。要打开“环境变量”窗口,请执行以下步骤:
- 右键单击“这台电脑”图标并选择“属性”。
- 现在在此窗口中,从左侧选择“高级系统设置”。
- 点击(Hit)最后一个按钮“环境变量”以打开我们的目标窗口。
打开它后,您将能够分别查看用户(User)和系统(System)变量。变量名在第一列,它的值在第二列。表格下方的相应按钮可让您“添加”、“编辑”和“删除”这些变量。
使用路径变量
路径(Path)是最常用的环境变量。正如我所提到的,它指向包含可执行文件的目录。正确设置Path变量后,您可以在系统的任何位置使用这些可执行文件。让我们尝试通过一个例子来理解这一点。打开环境变量窗口并在系统变量中查找“路径”。(Path)
单击(Click)“编辑”,然后单击“新建(New)”,而无需立即使用其他控件。现在单击浏览并选择包含所需可执行文件的文件夹。单击(Click)“确定”并保存所有内容。我在路径中添加了“Visual Studio Code”可执行文件。
现在要运行您的应用程序,打开命令提示符(Command Prompt)并输入文件夹中可执行文件的名称。如果程序支持,您可以提供其他参数。该程序将从命令提示符运行,而实际上并不在您执行命令的目录中。这就是Path 变量(Path variable)的美妙之处。
所有环境变量的列表
打开命令提示符并键入“ SET ”并按Enter。将显示带有当前值的整个变量列表,您可以参考它来对您的计算机进行更改。
动态环境变量
与常规变量不同,动态环境变量由CMD提供,而不是由系统提供。您无法更改这些变量的值,并且在查询时它们会扩展为各种离散值。我们通常将这些变量用于批处理,并且这些变量不存储在环境中。即使是“SET”命令也不会显示这些变量。下面列出了一些动态环境变量。
阅读(Read):如何将环境变量添加到上下文菜单(How to add Environment Variables to Context Menu)。
Windows 11/10中的环境变量列表
%APPDATA% – C:Users<用户名>AppDataRoaming
%ALLUSERSPROFILE% – C:ProgramData
%CD% – 输入此命令将为您提供您正在工作的当前目录。
%COMMONPROGRAMFILES% – C:Program FilesCommon Files
%COMMONPROGRAMFILES(x86)% – C:Program Files (x86)Common Files
%COMMONPRGRAMW6432% – C:Program FilesCommon Files
%CMDEXTVERSION% – 此变量扩展为命令行扩展的版本。
%COMSPEC% – C:WindowsSystem32cmd.exe
%DATE% : – 此变量将根据日期格式首选项为您提供当前日期。
%ERRORLEVEL% – 确定上次执行命令设置的错误级别。
%HOMEDRIVE% - C:
%HOMEPATH% -C:Users<用户名>
%LOCALAPPDATA% – C:Users<用户名>AppDataLocal
%LOGONSERVER% – \<domain_logon_server>
%PATH% – C:Windowssystem32;C:Windows;C:WindowsSystem32Wbem
%PATHEXT% – .com ; .exe;.bat ; .cmd ; .vbs ; .vbe ;.js; .jse ; .wsf ; .wsh ;.msc
%PROGRAMDATA% – C:程序数据
%PROGRAMFILES% – C:程序文件
%PROGRAMW6432% – C:程序文件
%PROGRAMFILES(X86)% – C:程序文件 (x86)
%PROMPT% – $P$G
%SYSTEMDRIVE% - C:
%SYSTEMROOT% - C:Windows
%TIME% – 同样,它根据时间格式首选项为您提供当前时间。
%TMP% – C:Users<用户名>AppDataLocalTemp
%TEMP% – C:Users<用户名>AppDataLocalTemp
%USERNAME% – <用户名>
%USERPROFILE% – C:Users<用户名>
%USERDOMAIN%–(Userdomain)与当前用户关联的用户域。
%USERDOMAIN_ROAMINGPROFILE%–(Userdomain)与漫游配置文件关联的用户域。
%WINDIR% – C:Windows
%PUBLIC% – C:UsersPublic
%PSMODULEPATH% – % SystemRoot %system32WindowsPowerShell 1.0Modules
%ONEDRIVE% – C:用户<用户名>OneDrive
%DRVERDATA% – C:WindowsSystem32DriversDriverData
%CMDCMDLINE% –输出(Outputs)用于启动当前命令提示符(Command Prompt)会话的命令行。(命令提示符(Command Prompt)。)
%COMPUTERNAME% - 输出系统名称。
%PROCESSOR_REVISION% – 输出处理器版本。
%PROCESSOR_IDENTIFIER% – 输出处理器标识符。
%PROCESSOR_LEVEL% – 输出处理器级别。
%RANDOM% – 这个变量打印一个从 0 到 32767 的随机数
%NUMBER_OF_PROCESSORS% – 输出物理和虚拟内核的数量。
%OS% – Windows_NT
这几乎与Windows上的(Windows)系统(System)和用户环境变量(User Environment Variables)有关。Windows确实带有更多变量——不要忘记使用“SET”命令检查它们。
提示(TIP):Rapid Environment Editor是一个强大的 Windows 环境变量编辑(Windows)器(Variables Editor)。
System & User Environment Variables in Windows 11/10 explained
System and User Environment Variables has always been a complex topic of discussion for day-to-day Windows OS users. What do they do? A tutorial on the internet said to add a PATH variable, but what is it? Where do I find these variables? All of these questions will be shortly answered in this post. We’ve tried to give you a high-level overview of what System and User Environment Variables are and how you can utilize them.
In this post, we will discuss Environment Variables, System and User Environment Variables and how to add and modify them. Then we will move on to Dynamic System Variables and see how to use them in the command prompt.
What is an Environment Variable In Windows OS
Environment Variable is formed up by two separate words, ‘Environment’ and ‘Variable’. Let’s discuss ‘variable’ first. It means that the entity can store a value and vary from computer to computer. Windows provide an ‘Environment’ for applications to execute and perform operations and that is what makes the first word. Combining both, Environment Variables are those dynamic objects that store the values provided by the environment. Now environment provides values that help other programs in obtaining some crucial information about the system. Like there is an environment variable called ‘windir’ that corresponds to the directory where Windows is installed. To see this in action, open up an explorer window and type in ‘%windir%’ in the address bar. The Windows installation folder will open up.
Very similarly, you can make reference to the Windows directory using ‘windir’ variable in other programs and scripts. There are numerous other variables that can be accessed, ‘TEMP’ or ‘TMP’ is the variable that points to the directory where all temporary files are stored. The most popular, ‘Path’ variable is the one that points to the directories containing executable files. So that you can run a program from the Command Prompt in any other directory. We’ve explained Path later on in this post. All these variables come in handy when you are developing something or using the shell a lot.
What are System & User Environment Variables
Very similar to how the Registry works on Windows, we have System and User Environment Variables. The system variables are system-wide accepted and do not vary from user to user. Whereas, User Environments are configured differently from user to user. You can add your variables under the user so that other users are not affected by them.
Just for your information since we are discussing the topic in depth. System Variables are evaluated before User Variables. So if there are some user variables with the same name as system variables then user variables will be considered. The Path variable is generated in a different way. The effective Path will be the User Path variable appended to the System Path variable. So the order of entries will be system entries followed by user entries.
How to add & modify Environment Variables
A small warning before we go deeper. Create a system restore point, and try not to tamper with the existing settings configured for your system. Until unless you are very sure about your actions. To open the ‘Environment Variables’ Window, follow these steps:
- Right-click ‘This PC’ icon and select ‘Properties’.
- Now in this window select ‘Advanced System Settings’ from the left part.
- Hit the last button saying ‘Environment Variables’ to open our destined window.
Once you’ve opened this up, you will be able to view User and System variables separately. The variable name is in the first column and its value in the second. The corresponding buttons below the table let you ‘Add’, ‘Edit’ and ‘Delete’ these variables.
Using the Path variable
Path is the most used environment variable. As I mentioned, it points to directories that contain executable files. Once you’ve correctly setup your Path variable, you can use these executables from anywhere in the system. Let’s try to understand this with an example. Open up the environment variables window and look for ‘Path’ in system variables.
Click on ‘Edit’ and then click on ‘New’ without playing around with other controls right now. Now click browse and select a folder that has your desired executable file. Click ‘Ok’ and save everything. I added ‘Visual Studio Code’ executable to the path.
Now to run your application, open up Command Prompt and type in the name of the executable file that was in the folder. You can provide additional arguments if the program supports it. The program will run from the command prompt without actually being in the directory from where you executed the command. That is the beauty of the Path variable.
List of all Environment Variables
Open command prompt and type ‘SET’ and hit Enter. The entire list of variables with their current values will be displayed and you can refer to it for making changes to your computer.
Dynamic Environment Variables
Unlike, conventional variables, dynamic environment variables are provided by the CMD and not by the system. You cannot change the values of these variables and they expand to various discrete values whenever queried. We usually use these variables for batch processing and these are not stored in the environment. Even the ‘SET’ command will not reveal these variables. Some of the dynamic environment variables are listed below.
Read: How to add Environment Variables to Context Menu.
List of environment variables in Windows 11/10
%APPDATA% – C:\Users\<username>\AppData\Roaming
%ALLUSERSPROFILE% – C:\ProgramData
%CD% – Typing in this command will give you the current directory you are working in.
%COMMONPROGRAMFILES% – C:\Program Files\Common Files
%COMMONPROGRAMFILES(x86)% – C:\Program Files (x86)\Common Files
%COMMONPRGRAMW6432% – C:\Program Files\Common Files
%CMDEXTVERSION% – This variable expands to the version of the command-line extensions.
%COMSPEC% – C:\Windows\System32\cmd.exe
%DATE%: – This variable will give you the current date according to date format preferences.
%ERRORLEVEL% – Determines the error level set by last executing command.
%HOMEDRIVE% – C:\
%HOMEPATH% -C:\Users\<username>
%LOCALAPPDATA% – C:\Users\<username>\AppData\Local
%LOGONSERVER% – \\<domain_logon_server>
%PATH% – C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem
%PATHEXT% – .com;.exe;.bat;.cmd;.vbs;.vbe;.js;.jse;.wsf;.wsh;.msc
%PROGRAMDATA% – C:\ProgramData
%PROGRAMFILES% – C:\Program Files
%PROGRAMW6432% – C:\Program Files
%PROGRAMFILES(X86)% – C:\Program Files (x86)
%PROMPT% – $P$G
%SYSTEMDRIVE% – C:
%SYSTEMROOT% – C:\Windows
%TIME% – Similarly, it gives you current time according to the time format preferences.
%TMP% – C:\Users\<username>\AppData\Local\Temp
%TEMP% – C:\Users\<username>\AppData\Local\Temp
%USERNAME% – <username>
%USERPROFILE% – C:\Users\<username>
%USERDOMAIN% – Userdomain associated with current user.
%USERDOMAIN_ROAMINGPROFILE% – Userdomain associated with roaming profile.
%WINDIR% – C:\Windows
%PUBLIC% – C:\Users\Public
%PSMODULEPATH% – %SystemRoot%\system32\WindowsPowerShell\v1.0\Modules\
%ONEDRIVE% – C:\Users\<username>\OneDrive
%DRVERDATA% – C:\Windows\System32\Drivers\DriverData
%CMDCMDLINE% – Outputs command line used to launch the current Command Prompt session. (Command Prompt.)
%COMPUTERNAME% -Outputs the system name.
%PROCESSOR_REVISION% – Outputs processor revision.
%PROCESSOR_IDENTIFIER% – Outputs processor identifier.
%PROCESSOR_LEVEL% – Outputs processor level.
%RANDOM% – This variable prints a random number from 0 through 32767
%NUMBER_OF_PROCESSORS% – Outputs the number of physical and virtual cores.
%OS% – Windows_NT
This was pretty much about System and User Environment Variables on Windows. Windows does come with a lot more variables – don’t forget to check them using the ‘SET’ command.
TIP: Rapid Environment Editor is a powerful Environment Variables Editor for Windows.