0

User Environment variables are separate for each user account on machine.

Consider the following, I'm logged in with userA which is non admin user account, now I want to get/set other user's (say userB) environment variable by using C# application running in userA's context.

Is this possible to do? And if so, how could I do this?

codingbadger
  • 42,678
  • 13
  • 95
  • 110
sailer
  • 449
  • 2
  • 7
  • 12
  • 4
    why you want to do that ? it seems a bad application design. – pylover Jun 12 '12 at 09:23
  • @pylover:i m executing different scripts frm my c# application. script will be executed in different different users context. i want to share environment variable between c# appln and script. script will modify env var value and i will get changed value in c# appln.also script type is not fixed (can be anything .cmd,.vbs,.ps1) – sailer Jun 12 '12 at 09:28

1 Answers1

1

The environment variables are stored in the registry, so to change them for another user you would have to import their registry hive.

The key for user vars is

HKEY_CURRENT_USER\Environment

and the key for system vars is

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

So if you import the current user hive you can change it for other users, or for machine vars change the HKLM key from any admin account.

This is messy though and I wouldn't advise you do it.

Or you can use some P/Invoke to use Impersonation.

Community
  • 1
  • 1
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • every user has an isolated HKEY_CURRENT_USER registry tree.any users may not have to change the other user's entries – pylover Jun 12 '12 at 09:33
  • I'm not sure what you mean. Are you talking about the `HKEY_USERS\S-1-5...` keys? – Bali C Jun 12 '12 at 09:35
  • @Bail C:HKEY_CURRENT_USER have only current users evn var values. i want to modify env var value of other user – sailer Jun 12 '12 at 09:38
  • 3
    @sailer Yes, but if you import the other users NTUSER.dat file you can edit theirs. See here for a detailed explanation. http://www.ghacks.net/2008/03/12/windows-tip-edit-user-registry-of-other-users/ – Bali C Jun 12 '12 at 09:44