Reputation: 21
I am trying to create an environment variable for a specific user profile whilst using the command line (cmd.exe). I know of the setx
command but cannot create a variable for a user. So my goal is to create an environment variable that is user specific aimed to the mapped network drive letter "M:". Anyway?
Upvotes: 1
Views: 909
Reputation: 4132
setx
cannot create environment variables for other users (though it can create user-scoped variables), and it sends the WM_SETTINGSCHANGE
message to notify running processes that the environment has changed.
User-scoped environment variables are just registry values, stored under HKEY_CURRENT_USER\Environment
. You can create one for a given profile by modifying the value/data pairs under that key. Modifying the registry doesn't send the WM_SETTINGSCHANGE
, like setx
does, though.
You can create a variable for a specific user (whose profile already exists) by loading the registry hive
reg load HKU\ForeignUser C:\Users\<userid>\NTUSER.DAT
(The user cannot be logged on or have been logged on since Windows was booted. NTUSER.DAT
will be locked in those circumstances.)
Then you can execute registry-modifying commands relative to that key (HKEY_CURRENT_USER\ForeignUser
) to create/modify environment variables.
reg add HKU\ForeignUser\Environment /v ENVVAR_NAME /t REG_SZ /d envvar_value
If the user has never logged onto the machine, modifying the default user profile will allow you to precreate the environment for all new user profiles.
You could modify the default user profile (HKEY_USERS\.Default\Environment
) to configure environment variables.
reg add HKEY_USERS\.Default\Environment /v ENVVAR_NAME /t REG_EXPAND_SZ /d envvar_value
If you include %USERNAME%
or some other environment variable in the _envvar_value_, then you could make it work for all users.
This would, however, create the envvar for all subsequent users who log into the machine for the first time. If you only want the variable created for a single user, this is probably not the best solution.
Alternately, you could create a login script (invoked by creating a value under HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run
; description) that would create the envvar, using setx
or registry modification commands. If you only wanted it to work for a specific user, you'd have to check the username inside the script, like:
IF "%USERNAME%"=="TARGET_USER" SETX ENVVAR_NAME "envvar_value"
Upvotes: 1