Using CMD SET command is not saving - windows

Ahead of time I am a newb at programming/scripting. I mainly research what I want and just edit it to fit my needs when possible.
Environment:
Windows 7
CMD
Usergroup = admin
Occurs when launching as user(admin) or launching cmd as administrator
When I use CMD to change an environment setting, it does not save to the system.
Example:
C:\Users\admin>SET TESTVARIABLE = "This is a test"
C:\Users\admin>SET
TESTVARIABLE = "This is a test"
C:\Users\admin>_
close out cmd, reopen, use SET and the testvariable is no longer there.
Going through System > Advanced System Settings > Environment Variables > System Variables is the only way I can positively make changes to the variables.
This is also affected by any .bat files I make. Mine don't actually get applied when they run but other peoples that I copy(entire .bat file, not just text/context).
This is occuring on my (all at work) desk pc(win10) and 2x Remote Desktop (win7). I am admin and I'm using admin users on these RDP's.
Anyone have any idea what is happening, what i'm missing, how I can research it or fix it?

Variables set using the "set" command are only available in the cmd's instance. To save variables persistently see this: Set a persistent environment variable from cmd.exe

Related

Setting environment changes?

I would like to ask you if it's normal that my "Setting environment" changes after each system reboot or even if I just close my cmd console.
for example, I'm using python 3.5, when I wan to use python or to uses pip under the cmd I got an error like :
C:\>python
'python' is not recognized as an internal or external command,
operable program or batch file.
To fix this, I use :
set PATH=%PATH%;C:\Python35
It works after, but as I said before, once I restart my computer, or I close the console, I've the same problem !!!
Thanks in advance for your help and comments. I just would like to inform you that I'm using Windows 7 - 64bits.
You need to add this path in System (Windows+Pause key), Advanced, Environment variables. There you have two sections, System and User, in System, edit the PATH key.
The next time you start a console the path will be present.
Alternaly, if you don't want to modify the setting there (or you have no rights) you could start the app with a batch file which sets the path before launching the app.
In Ruby you can adapt the environment variables from within the script itself by using ENV eg ENV['path'] += 'C:\\Python35'
, I'm sure Python can do this too but wouldn't know how. In your use case this won't help of course.
The SETX command will set the variable permanently. Use SETX /? for information. Set a persistent environment variable from cmd.exe
For Python, many developers use venv which is included with the Python install. https://docs.python.org/3/library/venv.html?highlight=venv#module-venv

Run command to go to user's documents folder (similar to %appdata%)?

Putting %appdata% into the run window will open up the Appdata folder, but what is the command to open the User's Documents folder, even when someone else logs in? I thought it was something like %userdir%, but I can not find out what it is. Not sure what to search for in Google either so I am at a loss.
There is no environment variable for this. You'll need to set one. Here's a list of all Windows default environment variables:
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds_shelloverview.mspx?mfr=true
*This is for XP, but should be the same for newer versions of Windows
For a single terminal session, you can just use
set USERDOCS=%USERPROFILE%\Documents
For all terminal sessions, you'll need to set a system environment variable. You can do this using the GUI or using setx -m USERDOCS %USERPROFILE\Documents, as described here.
The command to make this happen would be
explorer %USERDOCS%
But if you wanted to make an alias to that (i.e. a openuserdocs command), I can update and explain.

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

Setting permanently variables in windows %username%

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%

Resources