On Windows, how do I set an environment variable for a user other than the currently logged in one? I need to set the TMP
variable to change the temporary directory used by an ASP.NET app.
Asked
Active
Viewed 5.1k times
29
Stijn
- 218
- 3
- 20
George Mauer
- 479
- 3
- 7
- 13
-
As an unrelated comment: Why in the world would an ASP.NET application look to the computers Env. variables for a file path setting? AppSettings are built-in for that reason alone. – Brent Pabst Jun 12 '12 at 16:48
-
Also, this question is off topic for ServerFault, it should be posted on SuperUser – Brent Pabst Jun 12 '12 at 16:51
-
1@BrentPabst - The standard temp directory api in .Net reads from it - http://msdn.microsoft.com/en-us/library/system.io.path.gettemppath.aspx – George Mauer Jun 12 '12 at 18:26
-
Right, so what user is your application running under? Does it change that context each time a user logs in? Typically in IIS this is not the logged in user. – Brent Pabst Jun 12 '12 at 22:10
-
@BrentPabst - I don't get what you're getting at. It runs under the same context every time (Network Service). But rather than setting the TMP variable for the entire server, I want to set it just for that user. I think what uSlackr recommends is going to work. Are you proposing another approach? – George Mauer Jun 13 '12 at 12:46
-
@BrentPabst 5 years later, [ASP.NET Core reads the `ASPNETCORE_ENVIRONMENT` environment variable](https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments) to determine the environment it is running in. – Stijn Dec 22 '17 at 10:22
1 Answers
30
You can access through the registry. Modify the \Environment\Tmp
key in HKEY_Users\<their SID>
Here are two solutions for getting the account SID
$User = New-Object System.Security.Principal.NTAccount("domainname", "username")
$SID = $User.Translate([System.Security.Principal.SecurityIdentifier])
$SID.Value
or
Get-WmiObject win32_useraccount -Filter "name = 'username' AND domain = 'domainname'"
uSlackr
- 6,337
- 21
- 36
-
1Inspired by this answer, [I was able to figure out how to do this via Chef](http://stackoverflow.com/a/28224034/173497). – Kenny Evitt Feb 09 '15 at 00:12
-
1Is there an elegant way to get the SID of a user?. Particularly a virtual account?. – Beau Trepp Sep 15 '15 at 03:01
-
1You could ask that as another question, but if you do a google search for `get sid of application pool identity`, the first result, http://winterdom.com/2014/05/iis-apppool-identity-sids, has a pretty elegant solution. – austinian Sep 25 '15 at 10:08
-
6@austinian That link is now unavailable. That's why it's important to include such information in an answer itself, instead of linking to it. – Stijn Dec 21 '17 at 17:25
-
5This powershell code will grab the SID `$User = New-Object System.Security.Principal.NTAccount("domain", "username") $SID = $User.Translate([System.Security.Principal.SecurityIdentifier]) $SID.Value` or `Get-WmiObject win32_useraccount -Filter "name = 'user' AND domain = 'domain'"` – uSlackr Dec 22 '17 at 18:10
-
1The above is 3 separate lines starting with $. The last line can either be $SID.Value or Get-WmiObject. Thanks uSlackr, my SID's key (folder) wasn't created yet in Windows' regedit -- I really didn't want to log into it, but by editing my other account's temporary variables via My Computer -> Environmental Variables -> User Variables I was able to get Windows to automatically create the other account's key. – abelito Apr 26 '19 at 11:26
-
@Stijn no its still available: http://web.archive.org/web/20160405211944/https://winterdom.com/2014/05/iis-apppool-identity-sids – avgvstvs Feb 18 '20 at 21:51
-