- Published on
How to configure environment variables in Windows
- Authors
- Name
- Rahul Neelakantan
Environment variables store information about the operating system environment. This information includes details such as the operating system path, the number of processors used by the operating system, and temporary folders’ location. These variables are globally accessible, and programs can query the value of the variable.
The environment variables store data that is used by the operating system and other programs. For example, the environment variable WINDIR contains the location of the Windows installation directory. We can use this to determine where Windows operating system files are located.
Table of Contents
- Scope
- Temporary/Session
- User
- Machine
- Why are environment variables needed?
- Important Environment Variables In Windows
- How to create User Defined Environment Variables in Windows 10?
- Working with Environment Variables in Command Prompt?
- Create Environment Variables
- Edit Environment Variables
- Remove Environment Variables
- Working with Environment Variables in PowerShell?
- Conclusion
Scope
There are 3 different scopes for Environment variables:
Temporary/Session
Say you open a command prompt or program, you can define a environment variable which will exist only till the program is running, after which the variable will be lost.
This scope is mostly used when you’re frequently using the path or information e.g. batch scripts, Jenkins, etc...
User
These variables are scoped at the user level, and these are only visible to the current user. This scope is mostly used when you don’t have permissions to change environment variables for all the users, or you don’t want to spam them with unnecessary variables.
Once you change the variable in user scope, these variables will be visible for all the programs for that user.
Machine
These variables are scoped machine-wide, i.e., all the users accessing the particular machine will have access to read these variables; the machine administrator can only create these variables. Most of the time, we update these variables when we install new software, but we can do so for other scenarios.
Make sure to not store sensitive information 🙂
Why are environment variables needed?
Environment variables are needed to pass on static information to our programs, which we can access whenever needed. They provide us useful information like machine names, processor count, etc.
The most used environment variable is PATH. If you add any Exe to the PATH variable, it becomes more easily accessible to Command Prompt & PowerShell. Whenever you install any application, you mostly need to add it to your path if you need it to be found or accessed frequently.
But one more point to take note of is Max limit for an Environment Variable is 32,760 characters, so if we keep adding all the Exe’s we find to our path, there is a chance that we could exceed the limit. But you can still shorten it by taking the help of new environment variables that you can create. More on this later.
Important Environment Variables In Windows
Variable | Description |
---|---|
PATH | The PATH is the system variable that your operating system uses to locate needed executables from the command line or Terminal window. |
CD | The current folder directory |
DATE | The current date using the same region-specific format as Date. |
TIME | The current time using the same region-specific format as Time. |
ERRORLEVEL | The exit code of the last run program |
COMPUTERNAME | Gets the Computer Name |
PROCESSOR_ARCHITECTURE | Lets us know if its AMD64/IA64/x86 architecture |
NUMBER_OF_PROCESSORS | The number of processors for the current machine |
USERPROFILE | This is equivalent to the $HOME environment variable in Unix/Linux |
USERDOMAIN | Gets the user domain name |
It’s good to learn the above variables as they give you a sound understanding of your PC even if you are not tech-savvy. If someone asks you how many processors your CPU has, you need to use echo %NUMBER_OF_PROCESSORS% in your command prompt. Likewise, if someone asks you about your user domain if you’re working in an office environment, you can type in echo %USERDOMAIN% to know about it.
How to create User Defined Environment Variables in Windows 10?
Creating user-defined variables is very easy to do in Windows 10. We can follow the same process for the older versions of windows also.
To create go to Search bar and type the following “edit environment variables for your account”
Once you click on that you’ll be able to see the below window where you can see both User variables and Machine Variables, but as you’ve opened “edit environment variables for your account”, you’ll only be able to edit User variables, the system variables are disabled.
To edit the System/Machine Environment variables, go to search bar and type in “Edit the system environment variables”, this will open the System properties window, where you’ll find the Environment Variables… button, but to edit System variables you’ll need to be the administrator of the machine, otherwise you’ll not be able to add/edit any of the System variables.
To edit the PATH variable search for it in User variables, select it and then click on Edit…
Now we will get a new window where we can add/edit or delete path from Path variable. Now click on New.
Then the cursor will be enabled for you to add your required folder to the path. This process is the same for Edit also.
Likewise, we can create/edit/delete other environment variables, the most used variable is Path, so I have explained it in this post as I mentioned earlier that the max limit for an Environment Variable is 32,760 characters, so in case you think that you might be exceeding the limit of your path variable.
You can create a new environment variable, say TEST, which has a path “C:\WayTooLongPath” in your Path variable. You can use it as an expansion. “%TEST%\Folder” this will get expanded to “C:\WayTooLongPath\Folder”.
Working with Environment Variables in Command Prompt?
Create Environment Variables
For the command prompt, the environment variables are first-class citizens, i.e., each variable we define gets added to the Environment variables, only the scope changes. Whichever variable we define exists only till the command prompt is active.
To define a variable in command prompt we can use set command.
SET VAR_NAME=TEST
The above example we create an environment variable VAR_NAME, which has a value TEST. We can access this environment variable in our commands with % wrapped around the variable.
ECHO %VAR_NAME% > %VAR_NAME%.txt
This will write a text TEST to TEST.txt file, which will get created in the current directory. Any machine or user environment variable can be accessed from the command prompt. To create a user-scoped environment variable, you’ll have to use the below command.
SETX VAR_NAME TEST
If you want to create an environment variable scoped at the machine level, use the below command.
SETX /M VAR_NAME TEST
Whenever you’re updating machine level variables, you’ll have to run the command prompt or PowerShell in administrator mode.
Edit Environment Variables
To edit, the environment variable is the same as setting it, and please see the below example to understand how you can do that.
SET VAR_NAME=%VAR_NAME%;NEW_TEXT
Now if we check the content in VAR_NAME it would be TEST;NEW_TEXT
Remove Environment Variables
To remove the variable from the current command session, use the regular built-in set command and then variable name – just put nothing after the equals sign.
SET VAR_NAME=
To verify if the above command has removed variable, just type in set command, this will display all the environment variables present.
The below command is used to remove the environment variable from the current user profile.
REG DELETE "HKCU\Environment" /V VAR_NAME /F
To remove an environment variable present at the system/machine level, we’ll need to open the command prompt in administrator mode and type in the below command.
REG delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /F /V VAR_NAME
Working with Environment Variables in PowerShell?
Working with PowerShell variables is very similar to the command prompt; just syntax would be slightly different. All the variables start with the $ dollar symbol, so you want to create a local variable.
$VAR_NAME="TEST"
One point to note is that we have to use double quotes, whereas in the command prompt, we didn’t have to do that; PowerShell has more strict language rules.
If you need to access environment variables, we’ll have to use $env:VAR_NAME, i.e., prepend with env: so that PowerShell knows that the variable is not locally scoped.
The below commands let you know how to update the environment variable to User & Machine profile, like always making sure to run in administrator mode to update machine variables.
[Environment]::SetEnvironmentVariable('VAR_NAME', 'TEST', 'User') # Add VAR_NAME to User Env
[Environment]::SetEnvironmentVariable('VAR_NAME', 'TEST', 'Machine') # Add VAR_NAME to Machine Env
To delete the variable, you’ll need to use $null in place of ‘TEST’, and this will remove the environment variables.
[Environment]::SetEnvironmentVariable('VAR_NAME', $null, 'Machine') # Remove VAR_NAME to Machine Env
[Environment]::SetEnvironmentVariable('VAR_NAME', $null, 'User') # Remove VAR_NAME to User Env
Make sure to close the current session for the variable to reflect in your command prompt or PowerShell; otherwise, changes to the variables will not reflect immediately.
Conclusion
I hope this post has given you a better understanding of working with environment variables and why it is essential for every Windows user.
Even if you don’t have administrative rights for your machine, you can still use User & Session variables. PATH and other variables like CD, DATE, TIME is critical if you plan on writing batch or PowerShell scripts.