132

What is the difference between SETX and SET?

As per my understanding:
Both are used to set environment variables
SETX is for user variables.
SET is for shell variables.

6

4 Answers 4

161

I'm afraid it's not quite that simple. Environment variables are not limited by scope, as you suggest, but you are right that the lifetime of the value in the variable is different when comparing the verbs.

set modifies the current shell's (the window's) environment values, and the change is available immediately, but it is temporary. The change will not affect other shells that are running, and as soon as you close the shell, the new value is lost until such time as you run set again.

setx modifies the value permanently, which affects all future shells, but does not modify the environment of the shells already running. You have to exit the shell and reopen it before the change will be available, but the value will remain modified until you change it again.

See here for an example: http://batcheero.blogspot.com/2008/02/set-and-setx.html

2
  • 21
    that is very well written, I would add though, that setx changes the registry
    – barlop
    Commented May 19, 2015 at 15:24
  • 1
    It's actually even less simple than described here. There are "volatile" environment variables to be mindful of. And, under some circumstances, the list of variables shown by the SET command will differ depending on whether the user has invoked an "elevated" command prompt or not.
    – kreemoweet
    Commented Jun 21, 2017 at 18:27
23

Actually we can set variable at three scopes:
1. Shell
2. User
3. System (Machine) or Global

SET : Create or Update the current shell scope for temporary.

C:\Users\977246>set /?
Displays, sets, or removes cmd.exe environment variables.

SET [variable=[string]]

  variable  Specifies the environment-variable name.
  string    Specifies a series of characters to assign to the variable.

Type SET without parameters to display the current environment variables.

SETX : Create or Update the current user environment variables for permanent.

C:\Users\977246>setx /?

SetX has three ways of working:

Syntax 1:
    SETX [/S system [/U [domain\]user [/P [password]]]] var value [/M]

Syntax 2:
    SETX [/S system [/U [domain\]user [/P [password]]]] var /K regpath [/M]

Syntax 3:
    SETX [/S system [/U [domain\]user [/P [password]]]]
         /F file {var {/A x,y | /R x,y string}[/M] | /X} [/D delimiters]

Description:
    Creates or modifies environment variables in the user or system
    environment. Can set variables based on arguments, regkeys or
    file input.

To remove the variable set value to empty string as follows

Example: setx path ""

In GUI User and System environment variables.

enter image description here

22

Adding a point that was missed by other answerers.

To set a System Environment Variable rather than a User Environment Variable, we just need to use the /m option in the setx command and run it from an elevated(Administrator) Command Prompt.

setx variable value /m

Example: Open Command prompt as administrator and run

setx Path "%Path%;C:\Users\User\Libs" /m

Explanation: The above command will append "C:\Users\User\Libs" to the already existing Path Variable(System Environment Variable).

Without the /m argument, it will make changes to or create a User-level Path variable only.

From the setx user manual,

/M Specifies that the variable should be set in the system wide (HKEY_LOCAL_MACHINE) environment. The default is to set the variable under the HKEY_CURRENT_USER environment.

3
  • 2
    ATTENTION: DO NOT USE THE POSTED COMMAND LINE TO REDEFINE THE SYSTEM ENVIRONMENT VARIABLE PATH! It is an absolute NO GO - NEVER EVER to redefine system or user environment variable Path with command SETX using the string value of the local environment variable referenced with %Path%. The result is a system Path with all environment variable references expanded, with folder paths of user Path appended resulting in duplicates in concatenated local Path and perhaps removed folder paths caused by truncation of command SETX.
    – Mofi
    Commented Jun 2, 2022 at 18:37
  • The redefinition of system or user Path from within a Windows command prompt window or using a batch file is really difficult, see for example Why are other folder paths also added to system PATH with SetX and not only the specified folder path? The batch code in this post is much better, but is nevertheless not 100% fail-safe.
    – Mofi
    Commented Jun 2, 2022 at 18:43
  • Users should click on Windows Start menu button, type on keyboard environment and Windows offers in language of Windows Edit the system environment variables and Edit environment variables for your account. Then the user should click on one of the two items to open the dialog window in which the user can modify safely the system and user environment variables. For more details about Path management see also What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?
    – Mofi
    Commented Jun 2, 2022 at 18:49
6

setx.exe is a program that sets user/system environment variables in the registry, which is used for future process creation. set is a command that shows the process environment variables in the environment in the parameter block in the PEB of the current process (which show the user and system variables as well as variables defined by the current or parent processes using set), as well as allowing more variables to be defined in it by the current process, and the changes to the environment can be viewed from process explorer.

cmd /c set variable=value will set variables for the child process cmd.exe that it creates (which attaches itself to the parent cmd.exe console window and does not allocate its own, and inherits the parent's environment) so it won't take effect when you perform set after that from within the same window, because it be referring to the parent process cmd.exe. Top level processes inherit their environment directly from the registry and explorer.exe dynamically updates its environment variables (unlike most other apps), so when you setx it won't even update the current process environment, only the registry, which is either read by explorer.exe, or the explorer.exe process is updated by the windows API function that sets the environment variable (less likely), such that future opened cmd.exes inherit explorer.exe's environment, as they are children.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .