Permanently Change Environment Variables in Windows

55,685

Solution 1

Nowhere does it mention a dependency between the HOMEDRIVE value and the HOMEDIRECTORY value, what was happening (I think) is that it was failing to map the home directory to the HOMEDRIVE and therefore defaulting back to a safe value (C:)

I wrote a script to update the local AD, replace the values in [] with your values. Copy and paste into a .vbs file and double click on it to run it.

Set objUser = GetObject("WinNT://[COMPUTERNAME]/[USERNAME],user")
objUser.homeDirDrive = "H:"
objUser.HomeDirectory = "[URNPATH]"
objUser.SetInfo

e.g.

Set objUser = GetObject("WinNT://UQBDART-2328/BEN,user")
objUser.homeDirDrive = "H:"
objUser.HomeDirectory = "\\SERVER\SHARE"
objUser.SetInfo

run this, reboot and test. It worked for me.

Solution 2

Sounds like the AD profile on the domain is overwriting the user defined variables. I see your screenshot says you are connected to the eait.org.edu.au domain. That will be the root of your issue. Just to include some details here that I spotted elsewhere, that may be of help to you:

HOMEDRIVE, HOMEPATH & HOMESHARE are set and updated via Active Directory. HOMEDRIVE & HOMEPATH are set even without a home drive set on the account; however they will be overridden by any user account properties set in AD.

Also see these KB articles:

http://support.microsoft.com/kb/841343
http://support.microsoft.com/kb/237566
http://support.microsoft.com/kb/101507 

On a side-note for another way around the issue:

-I have in the past created a new instance of the windows command-line shell executable that automatically runs a custom script, so everytime you launch the shell, the environment variable could be overriden.

-To do that you could just put the code you posted to change the environment variable into a batch script, stored wherever you like, then edit the shortcut(s) used to launch the shell by going to properties > then alter the Target box: %SystemRoot%\system32\cmd.exe /K "C:\Documents and Settings\Administrator\My Documents\customshellscript.cmd" (Obviously the part of the path after /K is the location of your custom script)

This way, if you are using openSSH over the console anyway, it will always have the homedrive set correctly.

Solution 3

Changing those environment variable's values is not "supported", at least it will not work as you expect because Windows changes them back.

According to this Microsoft knowledge base article (KB841343), you should use policies, if you need to change these settings. The article also contains links for how to do that (but personally, I never tried). Note that the article was originally written for Windows 2000, but I would strongly suspect, that it is still valid for current Windows versions.

Share:
55,685
Fantastic Mr Fox
Author by

Fantastic Mr Fox

I am a software engineer and roboticist. Building robots is my thing!

Updated on July 09, 2022

Comments

  • Fantastic Mr Fox
    Fantastic Mr Fox almost 2 years

    I found a way to change the default home directory of a user but I am having trouble with it.

    enter image description here

    enter image description here

    enter image description here

    Doing this will change the home drive to C:

    But then when I check the environment variable:

    enter image description here

    It is still H:, with a system restart the Enviroment variables in windows settings will also return to H:/

    I have also tried changing it like this:

    enter image description here

    Which appears to work but if i open a new cmd it will have reverted back to H:/

    Now I am trying to do this so that OpenSSH will recognise C as my home directory instead of H: which is a network drive, forcing OpenSSH not to work unless I cam connected to my university network via VPN.

    What can I do to set this permanently and in the eyes of OpenSSH?

  • Fantastic Mr Fox
    Fantastic Mr Fox over 11 years
    It doesnt really explain how to permanently change the variables, it says stuff about group permissions but no actual instructions, any chance to explain further?
  • Christian.K
    Christian.K over 11 years
    Sorry, no not really - as I said I never tried it. This might or might not help.
  • Fantastic Mr Fox
    Fantastic Mr Fox over 11 years
    That is a good answer, but its not really what i was hoping for, writing a batch file wont solve the problem of permanently changing the variable. How do you go about changing it so that windows doesn't change it back?
  • joocer
    joocer over 11 years
    There must be a bug somewhere. I've used SETX and ADSI to edit the HOMEDRIVE and rebooted my PC I get the following: ADSI recognizes it's own updates and SETX shows on the system properties screens (if you use /M it goes at the bottom, without /M at the top) but still the command line still shows C:. I now have four different home drives depending on what you look at.
  • Fantastic Mr Fox
    Fantastic Mr Fox over 11 years
    A bug with mine or a bug with yours, Are you saying that you got it to set permanently with SETX because i couldn't seem to.
  • joocer
    joocer over 11 years
    Bug with Windows, it's showing a value that doesn't match any source for that value. The registry matches what I keyed with SETX but that's different with what I saw on the command line.
  • Harry Johnston
    Harry Johnston over 11 years
    @Ben: writing a batch file eliminates the need to permanently change the variable, provided that (as your question implies) all that matters is what OpenSSH sees.
  • Harry Johnston
    Harry Johnston over 11 years
    +1 for an on-topic answer to an off-topic question, clever. :-) Of course, the OP hasn't said whether he has admin privilege, or even whether it's a local or an AD account, but your guesses are reasonable.
  • joocer
    joocer over 11 years
    I used this script on an undomained Windows 7 box, scripting updates to the local "Users and Groups" not a remote LDAP or AD. The OP appears to have admin rights, because he could update the environment vars, he just couldn't get them to stick.
  • Harry Johnston
    Harry Johnston over 11 years
    Actually if you look closely you'll see he was updating the user environment variables, which doesn't require admin privilege.