Regularly change environment variables - windows

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:

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.

Electron get or set Environment variables in Windows?

I know that from JavaScript it is not possible to get or set Windows Environment Variables. I have deployed a Electron app as a executable (as suggested here), is it possible now to get or modify a Windows Enviroment Variable. If it is can someone point me in the right direction ?
You can use NodeJs to read the enviroment variables via process.env you can read all methods and possabilities on the process documentation.
Example:
console.log(process.env.PATH)
Writing environment variables in a running process is always temporary. You can set environment variables by calling/spawning system commands/tools like setx under windows.
Persisting them does not change the running environment in the process for that you have to restart the process. So you need both, set the variable as described above and in addition persist it with system tools/commands.

Set Environment Variables 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.

OS X: Attach to a running process and read its environment variables

Is it possible (and if so, how?) to read a running process's environment variables. To be more specific, I am concerned with environment variables set by the process itself (using setenv() or similar), not the general user session environment variables set when the process is launched.
I figured that DTrace would be the tool for this job, but the only solution I've found online (from this mailing list thread) appears to be specific to Solaris because the script fails to execute on my Mac.
Perhaps Instruments exposes this functionality somehow and I just haven't found it yet?
I have also tried Tasks Explorer, which is a nice little application, and it shows process info including environment variables, but it does not show any environment variables set by the process itself, which is what I'm after.
Any help would be much appreciated!
I suppose you could try next way:
Inject you dylib into running process.
To read environment variables from injected dylib.
Via IPC (RPC, Shared Memory, etc.) send Environment variable into you application.
By the way, I have a plan to add this type of environment variables exploring into my Tasks Explorer.
Next links will help you with injection:
https://github.com/comex/inject_and_interpose
SIMBL/Bundle/dylib injection into Dock.app

How to modify the PATH variable definitely through the command line in Windows

I would like to make a .bat file that would add some string at the end of the value of the Windows PATH variable. Warning, I want this change to be definitive, not working only for the current session.
Does somebody know of a way to do this ? As much as possible it should not be dependent on the version of Windows
Sorry for the long answer, but a short answer on your question is impossible.
First of all you should understand how environment variables works. There are some places in the registry like HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment where the environment variables will be hold. On start-up the operation system reads this registry keys. Then one windows process creates another windows process. The parent process can give to the client process any set of environment variables. If the parent process doesn't do this, the child process inherits environment variables of the parent processes.
To be able update environment variables of a running process with respect of WM_WININICHANGE or WM_SETTINGCHANGE messages. A windows application can interpret this messages and reread the current environment variables from the registry HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment and HKEY_CURRENT_USER\Environment. So you can in general change registry values under HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment or HKEY_CURRENT_USER\Environment and send
SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment");
It would be much better to use SendMessageTimeout instead of SendMessage, but the idea will stay the same. The problem is that other processes must not wait for the message and do something. Most console application have no message loop and don't do anything if you send such messages.
So it is important to understand that there is no simple way to update environment variables of all processes without restarting of the computer. You should have a clear understanding of this and reduce your question a little.
If you update the environment in registry and send SendMessage (HWND_BROADCAST, WM_SETTINGCHANGE, 0, (LPARAM)"Environment") then new processed created by Explorer.exe will be have new environment variables, but cmd.exe will not do this.
If you want to update environment variables of the current cmd.exe inside a batch run you can do the following: You can create a new CMD file for example t.cmd in %TEMP% directory, write in the file SET PATH=%PATH%;C:\BlaBla and then use call %TEMP%\t.cmd and dell %TEMP%\t.cmd to update environment variables of the current cmd.exe.
To be exactly there are more places which are used to build environment variables of new created processes. This are subkeys of the key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths and %SystemRoot%\System32\autoexec.nt file. One will be used for processes created by ShellExecute and ShellExecuteEx (for example Explorer.exe) and another for console applications.
If you only care about new process instances, and you really want it to be done via a batch file, then setx is what you're looking for.
/M will change the PATH in HKEY_LOCAL_MACHINE instead of HKEY_CURRENT_USER.
i.e. a system variable, instead of user's.
example: SETX /M PATH "%PATH%;C:\your path with spaces"
If you want to directly change an environment variable for currently running processes, well, yeah, that's complicated and apparently not recommended:
Altering the environment variables of a child process during process creation
is the only way one process can directly change the environment variables of
another process. A process can never directly change the environment variables
of another process that is not a child of that process.
Otherwise, like Oleg says, programmatically the best way is to change the registry and send WM_SETTINGCHANGE and hope the apps are nice enough to pick it up.

Resources