29

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.

6
  • 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. Jun 12, 2012 at 16:48
  • Also, this question is off topic for ServerFault, it should be posted on SuperUser Jun 12, 2012 at 16:51
  • 1
    @BrentPabst - The standard temp directory api in .Net reads from it - msdn.microsoft.com/en-us/library/… Jun 12, 2012 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. Jun 12, 2012 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? Jun 13, 2012 at 12:46

1 Answer 1

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'"
8
  • 1
    Inspired by this answer, I was able to figure out how to do this via Chef. Feb 9, 2015 at 0:12
  • 1
    Is there an elegant way to get the SID of a user?. Particularly a virtual account?.
    – Beau Trepp
    Sep 15, 2015 at 3:01
  • 1
    You could ask that as another question, but if you do a google search for get sid of application pool identity, the first result, winterdom.com/2014/05/iis-apppool-identity-sids, has a pretty elegant solution.
    – austinian
    Sep 25, 2015 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, 2017 at 17:25
  • 5
    This 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, 2017 at 18:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.