Set Environment Variables Windows - windows

First off, I'd like to apologise if this has already been covered. It's difficult to filter through all the existing issues similar to this one.
So in Windows there is two (as far as I'm aware) ways to set environment variables. First one is through the Command Prompt and the second is through System Properties in the Control Panel.
The former would be more desirable for me however I have an issue where the variable only lasts and exists in that session. This gets considerably annoying when I have to re-set my Java path every time.
The latter is what I have to resort to, it takes time and once in a blue moon I'll forget where to locate the menu.
Is there something I have to do in the Command Prompt to get it set permanently? Possibly a flag or switch to append to the command?

The SETX command can modify environment variables persistently:
Setx provides the only command-line or programmatic way to directly
and permanently set system environment values.
It's quite a powerful tool but read the notes carefully on that page though because there are a few little catches to using it.

Related

How to make PowerShell recognize changes in environment variables?

When I change any environment variable in Windows using GUI (System Properties/Environment Variables/System Variables) changes are reflected immediately in cmd terminal, but PowerShell shows an old value.
Here cmd recognized that I changed JAVA_HOME value from C:\Users\nikif\.jdks\openjdk-19.0.1 to C:\Users\nikif\.jdks\corretto-16.0.2
PowerShell still shows the previous value - C:\Users\nikif\.jdks\openjdk-19.0.1
Is there any command that will make PowerShell recognize the environment variable change made in GUI?
Or maybe there is a better way to change the environment variable from PowerShell?
Thank you.
This all should really be a comment (I tried above), but attempting to explain why the question, as it stands, is too vage requires just more explaination.
Processes get their environment when started by the OS. They basically get a copy the environment as it was at that point in time. When you start a process you can (using the respective APIs) pass additional or altered environment variables.
During the existance (runtime) of a process you cannot change the environment.
Shells are a common exception, as they provide specific syntax that does that (set NAME=VALUE in CMD.EXE and $env:NAME="VALUE" in PowerShell, for example). An application itself could also do such stuff from its code using respective APIs. But in general, there is no way to change variables from the outside.
One process will never see the changes done to the environment in a different process. For example, if you have to separate CMD.EXE sessions running and change a variable in one of them, the other will not know. The environment is private to each process.
A potential exception is the (global/system) environment variables you can set using the Computer/Properties/Environment setting (applet). The system will send a WM_SETTINGCHANGE window message indicating the that environment has changed. Applications can register to this message and act accordingly.
However, neither PowerShell nor CMD.EXE do seem to listen to this message (it would require a (hidden) Window anyway and both are console applications). And frankly, it would be not good if they did. Consider CMD.EXE just execuing a batch file and a variable (say PATH) changes underneath - security issues and general havoc all over.

Supporting two shells in windows

I'd like to write my own (very simple) explorer.exe alternative that I could actively switch between without having to restart my computer.
Is it possible to run two shells simultaneosly (or to write a program that temporarily disables the current shell)?
If not, is it possible to stop explorer without it restarting itself, and have my shell start itself instead?
Edit
More info: I'd like to write a simple productivity tool for myself. I want to set up a very simple task manager that prevents me from starting/opening/using anything but a whitelisted set of applications I list ahead of time. Locking me into that set of apps for whatever time period I've set. If there's another (better) way to prevent people from shutting down my app, switching from my app (with alt-tab, etc) I'm all ears.
Note: I'm fine with the app/shell/whatever being escapable by restarting my computer. I just want to make it massively inconvenient to switch to being distracted, and I wanted to learn a bit more about the Windows API.
See this question for details about writing a shell.
No, there can only be one real shell process (SetShellWindowEx only works when there is no other shell process) WH_SHELL can be used by other processes and it might be enough for your needs (Maybe in combination with IShellExecuteHook)
When explorer.exe is started and it detects a different shell it will not display the taskbar, just a file browser window. Explorer also looks at the shell value in the registry IIRC. You might also want to look into the shift to exit trick.

Regularly change environment variables

I'm a developer and I work on some applications which need different environments.
One of my problems is related to the OS environment variables.
On Windows 7, do you know if there's a way to easily/regularly change the environment variables ?
Of course, I could change it manually but I'm looking for an easier solution.
Changes should be "permanent" till my next "task switch".
Create a set of batch files that set the variables the way you need them, then run the appropriate one for your task.
The setx command line tool will set them permanently.
http://technet.microsoft.com/en-us/library/cc755104(v=ws.10).aspx
I think Win+Break is a real time saver in getting to any System Config screen.
Further more, you can import/export system/user environment variables from the registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
HKEY_CURRENT_USER\Environment
Keep in mind that changing these will in not alter your running processes' environments. My procedure for this:
Killing explorer.exe from the Task Manager, and then
Run-ning a new explorer.exe (from the File menu in that same Task Manager)
will usually do what you want. Note that this 'soft' reload does not include long-running background processes.
Bonus Tip
use Process Explorer to view exactly what environment a running process is using:

Why don't programs on Windows usually add themselves to the PATH variable?

I couldn't even count the number of times I've had to manually add a directory. Is there a security reason for it, or do developers/installers not think about it?
Usually a program is started via a link, which has the path to the executable hard coded within it. There's no need to add to the general $PATH unless the program will be commonly executed from a command line.
What, exactly, are you trying to do? Windows, not being a relic of the steam age, has a number of built in features that usually make fooling with the Path environmental variable unnecessary.

How are Environment Variables set in Windows (Not as easy as it sounds) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
See Update Below
Let me explain the situation:
In the System Control Panel, I setup JAVA_HOME as C:\Java\JDK1.6. However, when I went into a terminal window to see what's going on, I got this:
echo %JAVA_HOME%
D:\Program Files\Java\jre6
Where in the hell is this getting set, and why isn't it picking up the value I set it in the control panel? Other environment variables I put in are set, but it looks like something is overriding it.
Word of Warning:
Although I'm an Administrator on the system, they've put all sorts of goofy restrictive policies on it. For example, I can't set my Recycle Bin not to warn me when I delete something. The Property setting isn't there when you right click on the Recycling Bin. I also can't do regedit. To set the environmental properties, I setup a MCC Console, and pull up the System Control Panel thorough there.
Update
Yes, I know the environment variables don't get reset until I open a new command line prompt. However, this is something I set a couple weeks ago, and the machine had been booted a few times since.
I have it set in the System Variables (the lower box). If I put it in the User Variables (the upper box), it does get set correctly, but my PATH is set incorrectly if I put %JAVA_HOME% in the path.
Going back through my questions and tying up all the loose ends...
Turned out that the administrators disabled the ability to set PATH on the system. the admins had a policy that if they didn't understand something, they locked it down. And, since they didn't understand much, they locked everything down.
This was a government office, and the admins locked down anything that didn't involve writing bureaucratic regulations or memos in Microsoft Office that no one bothers to read.
Unfortunately, I was a developer there, so I kept running into these walls. My supervisor quit, and took me to his new site. I'm glad I'm out of there.
When you change the environment variable via the control panel, it only changes the environment in the process that it is running.
When a process starts it will "inherit" the environment of the parent at the time it was launched. Changes afterwards are not propagated to child processes.
You may have to relaunch your terminal, or to be sure, log off then back on.
You have to open a new command prompt to actually "see" the new variable (or call the set command in the cmd window for temporary changes or setx for permanent changes).
Environment variables are inherited from the parent process when a process is started. When you change or add a variable in the Control Panel, your shell (i.e. the main explorer.exe process) gets to see the change immediately, but not any other already running process. When you start a new process with explorer.exe as the parent process, e.g. by double-clicking a file or chosing Start+Run, the newly created process will also see the updated environment variable(s).
A nice tool for analyzing such kind of things is Process Explorer. It shows you the relationship between parent and child processes and double-clicking a running process shows you a detailed dialog with a tab listing all environment variables of the current process.
The problems is, all executables are in bin folder so you should set %JAVA_HOME%\bin inside your PATH variable.
You were almost there my friend, it usually happens.
Marce

Resources