- 1 year ago
How to set environment vars permanently with a shell script in Windows
To set environment variables permanently in Windows using a shell script, you can use the setx
command. The setx
command allows you to set environment variables that persist across sessions.
Here's an example of how you can use a shell script to set environment variables permanently in Windows:
batch@echo off
REM Set the environment variable using setx
setx MY_VARIABLE "my_value"
REM Display a message to indicate success
echo Environment variable set successfully.
In the above example, replace MY_VARIABLE
with the name of your desired environment variable and "my_value"
with the value you want to assign to the variable.
After running the script, the environment variable will be set permanently, and it will be available in future sessions.
Please note that changes made to environment variables using setx
will not take effect in the current session but will be available in subsequent sessions. If you need the variable to be available immediately in the current session, you can use the set
command instead of setx
, but keep in mind that the variable will not persist after the session ends.
Also, ensure that you run the shell script with administrative privileges to have the necessary permissions to set the environment variables permanently.