How To Set Windows Environment Variables

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.

  1. 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.
  2. Click the Advanced system settings item in the popup window left side, it will popup the System Properties window.
  3. Click Environment Variables… button at bottom-right of the System Properties window, it will popup Environment Variables window.
  4. 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.
  5. 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.
  6. 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.
  7. 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.

  1. Run command set in Dos window to show current environment variables.
    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\;
  2. 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;
  3. 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
  4. Run command echo $Env:variable-name in 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.

  1. We can run setx command 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.
  2. 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.
  3. To see the changes, you need to open a new Dos window or PowerShell window and run the command echo %HELLO% or echo %ABC% .
    C:\Users\song zhao>echo %HELLO%
    C:\Users\song zhao>echo %ABC%
  4. 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.
  5. 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.

0 0 votes
Article Rating
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks
View all comments
Would love your thoughts, please comment.x