Set New User and System Environment Variables in Windows  

    Set New User and System Environment Variables in Windows

    Set New User and System Environment Variables in Windows

    How to Set New User and System Environment Variables in Windows
    Published by Category: General Tips
    07 Jul 2020
    Designer Media Ltd

    How to Set New User and System Environment Variables in Windows


    Environment variables are a set of dynamic named values that can affect the way running processes will behave on a computer. The variables can be used both in scripts and on the command line. Environment variables makes it easy when certain standard directories and parameters need to be referenced but where the actual locations or names can vary from computer to computer.

    The variable (ex: "%UserProfile%") is used as a type of shortcut of the value (ex: "C:\Users\<username>").

    There are two types of environment variables: user environment variables (set only for current user) and system environment variables (set for all users).

    This tutorial will show you how to set (create) new user and system environment variables in Windows 7, Windows 8, and Windows 10.

    User environment variables are stored in the HKEY_CURRENT_USER\Environment registry key.

    System environment variables are stored in the HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment registry key.



    Contents

    • Option One: Set New User Environment Variables in Environment Variables
    • Option Two: Set New User Environment Variables in Command Prompt
    • Option Three: Set New User Environment Variables in PowerShell
    • Option Four: Set New System Environment Variables in Environment Variables
    • Option Five: Set New System Environment Variables in Command Prompt
    • Option Six: Set New System Environment Variables in PowerShell






    OPTION ONE

    Set New User Environment Variables in Environment Variables


    1 Open the Control Panel (icons view), and click/tap on the User Accounts icon.

    2 Click/tap on the Change my environment variables link on the left side, and close the User Accounts control panel window if you like. (see screenshot below)

    Set New User and System Environment Variables in Windows-environment_variables-1a.jpg

    3 Click/tap on the New button under the top User variables for <current user name> section. (see screenshot below)

    Set New User and System Environment Variables in Windows-user_environment_variables-1.png

    4 Enter a variable name you want to use, enter a variable value you want to use, and click/tap on OK. (see screenshot below)

    If you like, you could click/tap on the Browse Directory button to navigate to and select a directory to have its path entered for the variable value.

    If you like, you could click/tap on the Browse File button to navigate to and select a file to have its path entered for the variable value.

    Set New User and System Environment Variables in Windows-user_environment_variables-2.png

    5 When finished creating new user variables, click/tap on OK to apply (set) the new variables. (see screenshot below)

    Set New User and System Environment Variables in Windows-user_environment_variables-3.png






    OPTION TWO

    Set New User Environment Variables in Command Prompt


    1 Open a command prompt.

    2 Type the command below into the command prompt, and press Enter. (see screenshot below)

    setx [variable name] "[variable value]"

    Substitute [variable name] in the command above with the actual variable name (ex: "Downloads") you want to use.

    Substitute [variable value] in the command above with the actual variable value (ex: "C:\Users\Brink\Downloads") you want to use.

    3 You can now close the command prompt if you like.

    Set New User and System Environment Variables in Windows-user_environment_variables-command.png






    OPTION THREE

    Set New User Environment Variables in PowerShell


    1 Open Windows PowerShell.

    2 Type the command below into PowerShell, and press Enter. (see screenshot below)

    [Environment]::SetEnvironmentVariable("[variable name]","[variable value]","User")

    Substitute [variable name] in the command above with the actual variable name (ex: "Downloads") you want to use.

    Substitute [variable value] in the command above with the actual variable value (ex: "C:\Users\Brink\Downloads") you want to use.

    3 You can now close Windows PowerShell if you like.

    Set New User and System Environment Variables in Windows-user_environment_variables-powershell.png






    OPTION FOUR

    Set New System Environment Variables in Environment Variables


    You must be signed in as an administrator to do this option.


    1 Open the Control Panel (icons view), and click/tap on the System icon.

    Starting with Windows 10 build 20161, you will need to open Settings instead, click/tap on the System icon, and click/tap on About on the left side.


    2 Click/tap on the Advanced system settings link on the left side, and close the System window if you like. (see screenshots below)

    Set New User and System Environment Variables in Windows-environment_variables-1.jpg Set New User and System Environment Variables in Windows-settings_system.png

    3 Click/tap on the Environment Variables button. (see screenshot below)

    Set New User and System Environment Variables in Windows-environment_variables-2.png

    4 Click/tap on the New button under the bottom System variables section. (see screenshot below)

    Set New User and System Environment Variables in Windows-system_environment_variables-1.png

    5 Enter a variable name you want to use, enter a variable value you want to use, and click/tap on OK. (see screenshot below)

    If you like, you could click/tap on the Browse Directory button to navigate to and select a directory to have its path entered for the variable value.

    If you like, you could click/tap on the Browse File button to navigate to and select a file to have its path entered for the variable value.

    Set New User and System Environment Variables in Windows-system_environment_variables-2.png

    6 When finished creating new system variables, click/tap on OK to apply (set) the new variables. (see screenshot below)

    Set New User and System Environment Variables in Windows-system_environment_variables-3.png

    7 Click/tap on OK. (see screenshot below)

    Set New User and System Environment Variables in Windows-environment_variables-3.png






    OPTION FIVE

    Set New System Environment Variables in Command Prompt


    You must be signed in as an administrator to do this option.


    1 Open an elevated command prompt.

    2 Type the command below into the elevated command prompt, and press Enter. (see screenshot below)

    setx [variable name] "[variable value]" -M

    Substitute [variable name] in the command above with the actual variable name (ex: "Downloads") you want to use.

    Substitute [variable value] in the command above with the actual variable value (ex: "%UserProfile%\Downloads") you want to use.

    3 You can now close the elevated command prompt if you like.

    Set New User and System Environment Variables in Windows-system_environment_variables-command.png






    OPTION SIX

    Set New System Environment Variables in PowerShell


    You must be signed in as an administrator to do this option.


    1 Open an elevated Windows PowerShell.

    2 Type the command below into the elevated PowerShell, and press Enter. (see screenshot below)

    [Environment]::SetEnvironmentVariable("[variable name]","[variable value]","Machine")

    Substitute [variable name] in the command above with the actual variable name (ex: "Downloads") you want to use.

    Substitute [variable value] in the command above with the actual variable value (ex: "%UserProfile%\Downloads") you want to use.

    3 You can now close the elevated Windows PowerShell if you like.

    Set New User and System Environment Variables in Windows-system_environment_variables-powershell.png


    That's it,
    Shawn






  1. apb
    Posts : 94
    win 7 64bit
       #1

    Thanks for the details about what's in the registry, plus the arcane routes through the UI maze to get to something. I've pretty much given up on the Windows UI though, and prefer to just modify the registry.

    I am interested in what order the path is searched for the executable.

    1. Is it guaranteed that it will be searched in the order that it appears in the path variable?
    2. Does the user path get searched before the system path, or vice versa?


    I want to be sure that if I have something in my path that is meant to replace what is already found in the default path, that my item will be run, and not what was in the locations that come afterwards in the path.

    Also, if I start up a new shell, when will it know the new path? E.g.:
    1. Just start new cmd.exe.
    2. logout and log back in.
    3. reboot.


    Thanks.

    --peter
      My Computers


  2. Posts : 68,840
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #2

    apb said:
    Thanks for the details about what's in the registry, plus the arcane routes through the UI maze to get to something. I've pretty much given up on the Windows UI though, and prefer to just modify the registry.

    I am interested in what order the path is searched for the executable.

    1. Is it guaranteed that it will be searched in the order that it appears in the path variable?
    2. Does the user path get searched before the system path, or vice versa?

    I want to be sure that if I have something in my path that is meant to replace what is already found in the default path, that my item will be run, and not what was in the locations that come afterwards in the path.

    Thanks.

    --peter
    Hello Peter,

    Q1) The list of variables are referenced all at once. There's no order it searches through.

    Q2) Both the User and System variables are referenced at the same time. It's just the User variables are only good while signed in to the user account they are for.
      My Computers


  3. apb
    Posts : 94
    win 7 64bit
       #3

    apb said:

    Also, if I start up a new shell, when will it know the new path? E.g.:
    1. Just start new cmd.exe.
    2. logout and log back in.
    3. reboot.
    By experimentation I think I found the answer to that part, namely that the changes to the path only are used after a logout/login sequence. That's unfortunate, because it makes it a nuisance to debug. I believe in win 7 or maybe all the way back to xp, a new shell would reread the path.

    - - - Updated - - -

    Brink said:
    Hello Peter,

    Q1) The list of variables are referenced all at once. There's no order it searches through.

    Q2) Both the User and System variables are referenced at the same time. It's just the User variables are only good while signed in to the user account they are for.
    Thanks, but that doesn't make a lot of sense to me, because it leaves it ambiguous as to what happens when two different places in the path both have an item with the same name. So, I would think that either the order would be called "undefined," meaning there is no telling what order it searches, or there is in fact a search order. I'm pretty sure that in prior versions, it searched in the order that the locations appeared in the path variable, from left to right. One reason for it to be undefined would be if the search occurs in multiple asynchronous threads, and the first guy to find it wins.

    Also, by experimentation I see that it prepends the system path to the user path, not vice versa, which means that if you want your prog of the same name to supersede the one previously in the path, you have to change the system path, if, as I suspect, it really is searched in the order it appears in the path variable.

    I'll try a couple of experiments to check it.
      My Computers


  4. Posts : 68,840
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #4

    apb said:
    Thanks, but that doesn't make a lot of sense to me, because it leaves it ambiguous as to what happens when two different places in the path both have an item with the same name. So, I would think that either the order would be called "undefined," meaning there is no telling what order it searches, or there is in fact a search order. I'm pretty sure that in prior versions, it searched in the order that the locations appeared in the path variable, from left to right. One reason for it to be undefined would be if the search occurs in multiple asynchronous threads, and the first guy to find it wins.

    Also, by experimentation I see that it prepends the system path to the user path, not vice versa, which means that if you want your prog of the same name to supersede the one previously in the path, you have to change the system path, if, as I suspect, it really is searched in the order it appears in the path variable.

    I'll try a couple of experiments to check it.
    The system variable would trump the user variable in a situation like that.
      My Computers


  5. apb
    Posts : 94
    win 7 64bit
       #5

    Ok, from my experiments it appears that:

    1. System path is PREpended to user path to make the current path env variable.
    2. The path is searched from left to right, so system path is searched first, in the order it appears in the path variable, then user path.
    3. The path is only updated when the user logs in. I guess that makes sense because it is in "session manager," but it is annoying.


    The search order is in fact the most logical, and consistent with e.g. Linux, hence probably Posix.

    This means that if you add something to your user path, and it is shadowed by something in the system path, your item will not be found. As MS would say, "this is by design," because they don't want the user to effectively unknowingly clobber something that would be expected to run. However, that may be confusing when you add something to your path, and put something there, but it doesn't run.

    On the other hand, it means that if you want to actually have your thing run rather than the default one, or maybe one installed by some app you don't like, you need to change the system path. But, you better know what you are doing.
      My Computers


  6. Posts : 68,840
    64-bit Windows 11 Pro for Workstations
    Thread Starter
       #6

      My Computers


 

Tutorial Categories

Set New User and System Environment Variables in Windows Tutorial Index Network & Sharing Instalation and Upgrade Browsers and Email General Tips Gaming Customization Apps and Features Virtualization BSOD System Security User Accounts Hardware and Drivers Updates and Activation Backup and Restore Performance and Maintenance Mixed Reality Phone


  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 12:58.
Find Us




Windows 10 Forums