Setting global environment variables in windows programatically [duplicate] - windows

This question already has answers here:
How to set a variable for the current OS session only
(3 answers)
Closed 7 years ago.
As i am frequently moving desktop, I use a lot of portable apps.
I would, however like to alter/add global environment variables programtaically
on startup so that it is available to all consoles and windows in general.
Preferably It should work by running a script.
As the computer may be used later by another person the settings and variables must disappear when rebooted or powerloss occours.
Examples:
I would like to add imagemagick to the PATH on startup
Add to global CLASSPATH on startup.
Any surgestions?

Just set the traditional way. Go to the System Applet, set your user variables.

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.

Running emacs completely from usb on windows [duplicate]

This question already has answers here:
How can I have a portable Emacs?
(5 answers)
Closed 9 years ago.
In few weeks I'll be on a competition for website development.
I can use Emacs from a usb stick. I can't write to system drives anything so all Emacs configs and plugins are needed to be loaded from the usb stick. These are rules so I would rather not break them.
So I think this can be done with setting load-path or recompiling emacs to look config files from the usb but I no idea how this would be done in practice.
GNU provides compiled binary releases of Emacs for Windows. They require no installation or compilation.
Download the version you want (the latest at the time of writing is emacs-24.3-bin-i386.zip).
Optionally, verify its signature with GnuPG.
Extract it to your flash drive.
If a %HOME% environment variable exists, Emacs will use this as your home directory. Setting %HOME% to D:\ will cause Emacs to read its configuration from D:\.emacs or D:\.emacs.d\init.el. It will also cause package.el to install packages to D:\.emacs.d\elpa\.
If you are permitted to modify environment variables for your user, go to Computer ⟶ Properties ⟶ Advanced system settings, click the Environment Variables button, and add or modify HOME, setting it whatever drive letter your flash drive gets assigned. You will need to reboot (or at least log out and back in) before this variable takes effect.
If you are not allowed to modify this variable, you can use set HOME=D:\ in cmd.exe, then execute Emacs from that shell. You may want to write a short batch script to do this and add it to your flash drive.
To run Emacs, use bin\runemacs.exe.

setting the home directory in windows R [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Change path.expand location (Win 7)
I would like to change the directory that path.expand("~/") references on a windows system. Currently this goes to "C:/Users/home/Documents/" by default. How does one change this? Note that this is distinct from the working directory that is set with setwd()
You can change this by adding an R_USER variable to your Rprofile.site file.
Sys.setenv(R_USER="/my/desired/path/to/tilde")
This does not work on linux systems. See a related question: How to reset path.expand on tilde

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