Setting permanently variables in windows %username% - windows

i need to set permanently the %username% variable on windows is that possible?
I mean if I do on command prompt set username=UPPERCASE I can see that the variable is changed, however as expected this does work just in that command prompt, If I open another one the username variable is the original.
I tried to find the correspondent registry value but I did not find it.
I need some automatic way to do it.
Thanks!

When each process starts, the process gets a copy of the parent process' environment variables. So, if you used Windows Explorer to start your command prompt. You get a copy of Explorer.exe's environment variables, but when you edit it in cmd.exe, you don't edit the value for the rest of the system.
That said, Windows provides an event that processes can subscribe to so they can be told that there is a new value for environment variables. If you are interested, I can try to dig it up. I've used it before for the Path environment variable and think it may apply to your problem.
Sounds like you should create a bat file. This will prompt the user for their username and put it in the %USERNAME% environment variable.
SET /p USERNAME=What is your UserName?
putty /user:%username%

Related

Set external computer IP to environment variable

I have an external computer with an IP address like 100.12.5.7 and I would like to set a path to the environment variable like so:
setx MEDIA \\\100.12.5.7\d\media\
When I tried the above command, cmd prompt me that the variable is set successfully but when I do an echo %MEDIA%, it only shows me %MEDIA% instead of the full path. Any idea how I could fix this?
Setx doesn't affect the current console. Use Set and Setx together. Set, unlike Setx, requires an equal sign.
The reason setx can't affect the current console is that a copy of the starting program's environment is passed to programs it starts (usually Explorer's environment either direct or indirect via CMD). So all changes are local to that program. Setx sets registry keys, so only new programs can read them, and that really mean CMD.exe, as that rereads the registry keys.

Seting the PATH environment variable via cmd

I tried to add to the PATH environment variable ";C:\my_EXE" so I can run the programs I put there via cmd (windows 8).
I tried this command:
set PATH=%PATH%;C:\my_EXE
but it changed the PATH environment variable only until the CMD window was closed.
I searched on Google and I found this command:
setx PATH "%PATH%;C:\my_EXE"
that is supposed to set it forever, but it also works only until the CMD window has closed
and it made something like this:
[new PATH]=[old PATH]X2
It appears only in a new cmd and not in system properties(!=cmd, there is the PATH with my new path and not X2)
Why does this happen?
How can I set the PATH environment variable without problems?
There are two persistent PATH variables, the per-machine variable and the per-user one. They get appended together to produce the actual environment variable. (Environment variables in the PATH are also expanded at this point.)
That's why you're getting the path doubled up, because you've set the per-user persistent variable to include everything from the environment variable (which already includes everything from the per-machine persistent one).
You can use setx with the /m parameter to set the per-machine persistent variable, but this isn't ideal:
If the per-user persistent variable is set, its contents will be copied into the per-machine persistent variable, which is likely to be inappropriate;
If the persistent variable references other environment variables, the references will be replaced with the current value of those variables. If the referenced variables change, the PATH will no longer follow those changes. (To be honest, though, most of the time this won't matter: the feature isn't commonly used.)
Instead, consider using pathman which is specifically designed to manipulate paths. You can get pathman.exe from the Windows Server 2003 Resource Kit Tools download.
Note that both setx and pathman may hang if there are any unresponsive GUI applications running, even if the application window is hidden. The best way to minimize this risk is to reboot the machine immediately before running any script that uses setx or pathman.
You should use setx command with "/m" parameter.

How to get Windows OS Environment Variable from System Property

I am encountering a strange problem about System Property environment variable.
As you know, we can open Computer->Property->Advanced System Settings->Advanced->System Environment Variables->User Variables for admin
I change Variable TEMP' value to C:\mytemp\ and click OK to save it.
However, In command line, when I echo %TEMP%, it still show original path, C:\Users\admin\AppData\Local\Temp.
When I input %TEMP% in explorer, it will go to C:\mytemp
Now I want to user API to get the path, GetEnvironmentVariable() will also return path which is same as echo %TEMP% in command line. how can I get the path as exploer did.
Thanks in advance.
PS. OS is Win7 x86
Changing environment path through Computer->Property->Advanced System Settings->Advanced->System Environment Variables->User Variables will take effect immediately. However, please be noted that those programs which are running will not get the latest environment variable.
So I thought that you simply didn't reopen a new cmd for echo.
Open a new cmd, and echo again, the new path will be shown.
The environment variables are set when the process runs and are not updated.
To verify this, open a command prompt, and type "echo %TEMP%". Keep the command prompt open and change the %TEMP% variable. No matter what, entering "echo %TEMP%" will print the old values.
Now open a new command prompt and "echo %TEMP%", the new command prompt will print the new values.
As a workaround, you can spin up a new thread to check the current setting at that particular time.

Permanently Change Environment Variables in Windows

I found a way to change the default home directory of a user but I am having trouble with it.
Doing this will change the home drive to C:
But then when I check the environment variable:
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:
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?
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.
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.
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.
Back in the days of DOS the environment variables were part of the Program Segement which meant that you basically got a local snapshop of the variables limited to the scope of that program.
I'm guessing Windows hasn't changed this and the environment variables available to the CMD window only have the scope of that window and any further windows it spawns. This is supported by this little experiment:
in a CMD window type
set homedrive=h:\
and then test it's updated by typing
set h
from this same window type
start cmd
and in this new window type
set h
when I've done this I get HOMEDRIVE=H:\
if I then just open a new CMD window from the start menu and do the same SET H I get HOMEDRIVE=C:\
I don't know anything about OpenSSH but I suspect like the START CMD got the updated HOMEDRIVE environment variable, if you write a batch to update the environment variable and then execute OpenSSH that'll work.
I had a similar issue, the HOMEDRIVE variable was set to U: which was a drive we no longer used. This was just for one particular user. I went to that user's profile in AD and clicked the Attribute Editor tab. There was an attribute for HOMEDRIVE which I changed from U: to C: - when the user logged in again, it was set correctly. I also updated the HomeDirectory attribute here.
This issue had been really frustrating me trying to find a solution, but I have found what I think is the definitive answer and posted it at:
https://stackoverflow.com/a/60235759/12903197
You need to run 'net user USERNAME /homedir:PATH' where USERNAME is the name of the user you are trying to change and PATH is the drive letter and full path to the desired home folder, which must already exist.
On windows 8:
Hit "windows key".
type "system environment variables".
Allow "Advance system settings" to make changes.
Select "Advance" tab.
Click on "Enviroment Variables...".
Double click on "Path" from "User variable for XXXXXXXX"
Add at the end the variable the new path that you want separated by semicolon.
e.g. C:\Program Files\;C:\Python27\
shareeditdelete (1)flag

How to write a script/batch file on windows?

I am using a machine where I do not have administrator privileges and I need to set the e.g. JAVA_HOME each time I boot.
Thus, I'd like to write a script that sets the desired classpath automatically.
How do I do this?
You surely can set the environment variables with a batch file each time you need them, but bear in mind that this will only work for the current process (i. e. the cmd instance you're invoking the batch from) and processes spawned from there. I think you'd rather want persistent environment variables.
Now, in the UNIX world you'd just put them into your shell startup script. In Windows ... not exactly that way.
Right click on "My Computer", select "Properties".
There, go to the "Advanced" tab
Click the button labeled "Environment variables"
You can set JAVA_HOME and GRAILS_HOME under "User variables" there.
You would only need administrator access for changing the system environment variables.
You would want a file with .bat extension for Windows.
SET JAVA_HOME=path
SET GRAILS_HOME=path
On Windows, you could write a .BAT file (BATch), and then simply double-click it each time you log in. a .BAT file is simply a plain text file filled with any commands you would normally issue at the command prompt, e.g.:
cd c:\Temp
c:
set PATH=%PATH%;c:\Program Files\Some Directory\Some Subdirectory
You can optionally precede each line with the # symbol to stop it being echoed to the command window when it executes.
Try calling the following :
SETX JAVA_HOME path
SETX GRAILS_HOME path
If you're allowed they will be set in your profile and you won't need to run them again hopefully. Other wise you'll need to follow #Android's answer

Resources