Windows environment variables are very important, it plays an important role in Windows OS. This article will tell you how to set windows environment variables both on GUI and command line.
1. Set Windows Environment Variables On GUI.
- Open a Windows Explorer, right-click This-PC on the left side, then click the Properties menu item in the popup menu list, then it will popup System window.
- Click the Advanced system settings item in the popup window left side, it will popup the System Properties window.
- Click Environment Variables… button at bottom-right of the System Properties window, it will popup Environment Variables window.
- There are two parts in the Environment Variables dialog. The upper part area is User variables ( only available to this user) list, the lower part area is System variables ( available to all users that use this Windows OS ) list.
- Click the New… button to add a new environment variable, input variable name ( such as ABC ) and variable value ( such as C:\ ) in the popup dialog, click OK button to save it.
- To edit a windows environment variable, just select it and click the Edit… button. For example, we select the system variable Path and click the Edit… button, then you can add new path value or edit the existing path value in the popup dialog.
- The variable name is just a string, and if one environment variable has multiple values, you should use
;to separate them.
2. Set Windows Environment Variables In Command-Line.
If you want to get and set windows environment variables in a shell script, you had better use command line to process it.
2.1 Print Windows Environment Variables In Command-Line.
- Run command
setin Dos window to show current environment variables.
C:\WorkSpace>set ALLUSERSPROFILE=C:\ProgramData ...... OS=Windows_NT Path=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\; ......
- Run command
echo %variable-name%in Dos window to print special environment variable value.
C:\WorkSpace>echo %PATH% C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;
- Run command
Get-ChildItem Env:in Windows PowerShell to print all environment variables.
PS C:\Users\song zhao> Get-ChildItem Env: Name Value ---- ----- ALLUSERSPROFILE C:\ProgramData APPDATA C:\Users\song zhao\AppData\Roaming CommonProgramFiles C:\Program Files\Common Files
- Run command
echo $Env:variable-namein Windows PowerShell to print special environment variable value.
PS C:\Users\song zhao> echo $Env:PATH C:\Program Files (x86)\Common Files\Oracle\Java\javapath;
2.2 Set Windows Environment Variables In Command-Line.
- We can run
setxcommand in both Dos window and Windows PowerShell to set environment variables in command line. The command format is
setx variable-name variable-value.
C:\WorkSpace>setx HELLO 'haha' SUCCESS: Specified value was saved. ------------------------------------------- PS C:\Users\song zhao> setx ABC "C:\" SUCCESS: Specified value was saved.
- If the user environment variable does not exist, then it will add a new one. If the user environment variable name already exists, then it will overwrite it.
- To see the changes, you need to open a new Dos window or PowerShell window and run the command
C:\Users\song zhao>echo %HELLO% 'haha' C:\Users\song zhao>echo %ABC% C:\
- If you want to set a system environment variable, you should first run Dos window or PowerShell window as administrator and then run the command
setx variable-name variable-value /M, otherwise, you will encounter the error message ERROR: Access to the registry path is denied.
- In the below example, we use windows GUI to add a system environment variable TEST_VAR first, then we want to overwrite it in command line. Because the Dos window is not opened as administrator, then an error message is thrown.
PS C:\Users\song zhao> setx TEST_VAR "C:\" /M ERROR: Access to the registry path is denied.
We can also set environment variables in Windows registry, we will introduce it later in this article.