66
28
What is the difference between
SETX
andSET
?
As per my understanding:
Both are used to set environment variables
SETX
is for user variables.
SET
is for shell variables.
66
28
What is the difference between
SETX
andSET
?
As per my understanding:
Both are used to set environment variables
SETX
is for user variables.
SET
is for shell variables.
89
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
13that is very well written, I would add though, that setx changes the registry – barlop – 2015-05-19T15:24:42.503
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 – 2017-06-21T18:27:13.910
11
Actually we can set variable at three scopes:
1. Shell
2. User
3. System (Machine) or Global
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.
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.
4
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.
4Try
SET /?
andSETX /?
- that should give you some ideas. – Iszi – 2015-05-19T12:33:13.8503
Also, see SS64 references for SET and SETX. SS64 is a great command reference for several languages and command environments - I find it particularly handy for CMD & PowerShell.
– Iszi – 2015-05-19T12:35:28.207Ans: http://superuser.com/a/832962/294054
– Premraj – 2015-06-04T05:23:02.810http://superuser.com/questions/647505/set-enviroment-variable-setx – Premraj – 2015-09-23T06:11:09.917
http://coderjony.com/blogs/difference-between-set-and-setx-in-powershell/ – Ankush Jain – 2018-05-29T18:53:01.247