0

SET "temporarily" sets/changes an environment variable.

How do I get the "original" / "global" / "unchanged" one from a shell/process in which it has been altered?


To explain the title: SET works in what I'll call a local scope and SETX in a global one.
%ABC% gets me the variable from the local scope, but I'm looking for something to get it from the global one.

Thus SET : SETX :: %ABC% : ???

1 Answers1

0

To avoid confusion with terminology used in domains on networks, instead of 'local' say 'per user', and instead of 'global' say 'per machine' (for all local users).

'Local' really means 'on this machine only', and could mean for all local users.

Per machine variables are stored in

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Per user variables are stored in HKEY_CURRENT_USER\Environment

SETX can be per user or per machine.

SET is temporary (for the duration of the CMD session); SETX is permanent (stored in the registry).

Variables set with SETX are not available in any CMD session that was open at the time it was set; it will be available in new CMD sessions.

To set a variable 'per Machine', use the /M option with SETX.

Example demo:

Enter this as Administrator:

C:\WINDOWS\system32>setx apps %SystemDrive%\apps /m
SUCCESS: Specified value was saved.

Then open another cmd window as a non-admin user:

C:\Users\jh>echo %apps%
C:\apps

Altough "apps" is machine-wide, %apps% gets the value for an ordinary user.

So the format "%ABC%" gets both user and machine variables.

To clear that variable, as admin, do setx apps "" /m

j77h
  • 1