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 the System window.
    windows-10-right-click-this-pc-properties-menu
  2. Click the Advanced system settings item in the popup window left side, it will popup the System Properties window.
  3. Click the Environment Variables… button at the bottom-right of the System Properties window, it will popup the Environment Variables window.
    windows-10-advanced-system-settings-environment-variables-edit-window
  4. There are two parts in the Environment Variables dialog. The upper part area is the User variables ( only available to this user ) list, the lower part area is the 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 CLASSPATH), and variable value ( such as C:\ ) in the popup dialog, click the 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 a 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 the command line to process it.

2.1 Print Windows Environment Variables In Command-Line.

  1. Run the command set in the 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\;
    ......
  2. Run the command  echo %variable-name% in the Dos window to print the 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 the Windows PowerShell window 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 window 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 the 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%
    'haha'
    
    C:\Users\song zhao>echo %ABC%
    C:\
  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 the 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.

Leave a Comment

Your email address will not be published. Required fields are marked *

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