mjm
mjm

Reputation: 31

environment variable other user

I'm trying to set a environment variable for a user xy. How can I do this in a visual basic script?

This is what I have:

Dim objShell As Object
Set objShell = CreateObject("WScript.Shell")
objShell.Environment("user")("TEST") = "12345"

In this way I can set a environment variable for the logged in user. But how can I set a environment variable for an other user xy?

Markus

Upvotes: 0

Views: 302

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

If you want to change environment variables for other users you either have to run your code in their context (with runas) or change the variables in the registry. For the latter you have to load the user hive from the ntuser.dat in the user's profile:

Set sh = CreateObject("WScript.Shell")
sh.Run "%COMSPEC% /c reg load HKU\Temp C:\Users\foo\ntuser.dat", 0, True

After the hive was loaded you can manipulate the environment subkey:

sh.RegWrite "HKEY_USERS\Temp\Environment\TEST", "12345", "REG_SZ"

Unload the hive after you've finished and you're done:

sh.Run "%COMSPEC% /c reg unload HKU\Temp", 0, True

Upvotes: 1

peter
peter

Reputation: 42182

Only the system environment is available tot other users.

Set wshShell = CreateObject( "WScript.Shell" )
Set wshSystemEnv = wshShell.Environment( "SYSTEM" )
WScript.Echo "SYSTEM:  TEMP=" & wshSystemEnv( "TEMP" )

Upvotes: 0

Related Questions