To set a temporary or 'per session' environment varible from a command line you can use:
set ‹var name›=‹value>
Make sure your spacing is exactly as shown above, if you add a space between the equal sign the command will fail.
Check: Link for a nice tutorial and info on environment variables.
Answer from akseli on Stack ExchangeTo set a temporary or 'per session' environment varible from a command line you can use:
set ‹var name›=‹value>
Make sure your spacing is exactly as shown above, if you add a space between the equal sign the command will fail.
Check: Link for a nice tutorial and info on environment variables.
If you are using PowerShell, then you need to use the $env prefix for creating an environment variable.
Here's an example:
$env:<var name> = '<value>'
windows - Set a persistent environment variable from cmd.exe - Stack Overflow
command line - Set Environment Variables Windows - Stack Overflow
Set windows environment variables with a batch file - Stack Overflow
Windows Environment Variables
How Do I Find Environment Variables in Windows 10?
To find environment variables in Windows 10, you can follow the steps described above to find the environment variable information tucked away inside the system's advanced settings.
Alternatively, if you just need to see what the variables are but don't need to change them, you can simply open a command-line interface by hitting Ctrl +Esc and typing "cmd" in the command box, then type "set" in the command window. This prints out all the environment variables that are set on your system.
Why Can't I Edit the Environment Variables?
There are several reasons that you may not be able to set these variables.
1. The first of your problems could be that you don't have Admin rights. To set or edit this function, you must be the Administrator of the system.
2. If you are the Admin, yet the edit function is greyed out, try accessing the Environment Variables by accessing the Control Panel from the Start menu. Click on Advanced System Settings, then click Environment Variables.
Videos
Note: After seeing lots of comments about setting environment variables without administrator rights in Windows 10, I think I have found a way. I was not administrator and could use PowerShell.
PowerShell method
You can list all environment variables with: Get-ChildItem Env:.
To get the value of a specific variable: $Env:PATH, where PATH is the name of the variable.
To set a variable: [Environment]::SetEnvironmentVariable("PATH", "C:\TestPath", "User"), the first parameter is the name of the variable, the second is the value, the third is the level of.
There are different ways to work with environment variables and certain quirks with them in PowerShell so consult the link for details.
Old method (no longer available in newer Windows 10 updates, use PowerShell or see other answers)
Go into Settings and click on System.

Then on the left side click About and select System info at the bottom.

In the new Control Panel window that opens, click Advanced system settings on the left.

Now in the new window that comes up, select Environment Variables... at the bottom.

Still the same as ever: It’s in the old-style control panel’s “System” thingy. You can reach it with WinBreak or by right-clicking the Start button.
From there, select “Advanced system settings” → “Environment Variables”.
Or you can do it the hard way and find some other entry point to the old-style control panel, like the Network and Sharing Center or the Desktop folder(!).
To make the environment variable accessible globally you need to set it in the registry. As you've realised by just using:
set NEWVAR=SOMETHING
you are just setting it in the current process space.
According to this page you can use the setx command:
setx NEWVAR SOMETHING
setx is built into Windows 7, but for older versions may only be available if you install the Windows Resource Kit
We can also use "setx var variable /M" to set the var to system environment variable level instead of user level.
Note: This command should be run as administrator.
Use the SETX command (note the 'x' suffix) to set variables that persist after the cmd window has been closed.
For example, to set an env var "foo" with value of "bar":
setx foo bar /m
Though it's worth reading the 'notes' that are displayed if you print the usage (setx /?), in particular:
On a local system, variables created or modified by this tool will be available in future command windows but not in the current CMD.exe command window.
On a remote system, variables created or modified by this tool will be available at the next logon session.
In PowerShell, the [Environment]::SetEnvironmentVariable command.
The MSDN documentation for environment variables tells you what to do:
To programmatically add or modify system environment variables, add them to the HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment registry key, then broadcast a WM_SETTINGCHANGE message with lParam set to the string "Environment". This allows applications, such as the shell, to pick up your updates.
You will of course need admin rights to do this. I know of no way to broadcast a windows message from Windows batch so you'll need to write a small program to do this.
This batch file must be run as administrator to update system environment variable Path and set/update system environment variable JAVA_HOME.
@ECHO OFF
:: %ALLUSERSPROFILE% = C:\ProgramData
:: %ProgramFiles% = C:\Program Files
:: %USERPROFILE% = C:\Users\Ruben
:: %SystemDrive% = C:
:: %SystemRoot% = C:\WINDOWS
:: No spaces in paths
:: CMD inherits the environment variables when it is start from Windows shell.
:: CMD must be restarted to inherit the updated system environment variables.
:: Use console 2 https://sourceforge.net/projects/console/
REM Assign all Path variables
SET "PHP=%SystemDrive%\wamp\bin\php\php5.4.16"
SET "SYSTEM32=%SystemRoot%\System32"
SET "ANT=%USERPROFILE%\Downloads\apache-ant-1.9.0-bin\apache-ant-1.9.0\bin"
SET "GRADLE=%SystemDrive%\tools\gradle-1.6\bin"
SET "ADT=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\eclipse\jre\bin"
SET "ADTTOOLS=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\sdk\tools"
SET "ADTP=%SystemDrive%\tools\adt-bundle-windows-x86-20130219\sdk\platform-tools"
SET "YII=%SystemDrive%\wamp\www\yii\framework"
SET "NODEJS=%ProgramFiles%\nodejs"
SET "CURL=%SystemDrive%\tools\curl_734_0_ssl"
SET "COMPOSER=%ALLUSERSPROFILE%\ComposerSetup\bin"
SET "GIT=%ProgramFiles%\Git\cmd"
SET "JAVA_HOME=%ProgramFiles%\Java\jdk1.7.0_21"
REM Set JAVA_HOME variable as system environment variable.
setlocal EnableExtensions DisableDelayedExpansion
set "JaveHomeReg="
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v JAVA_HOME 2^>nul') do (
if /I "%%G" == "JAVA_HOME" (
set "JaveHomeReg=%%I"
if defined JaveHomeReg goto SetJavaHome
)
)
:SetJavaHome
setlocal EnableDelayedExpansion
if /I "!JaveHomeReg!" == "!JAVA_HOME!" endlocal & goto SetSystemPath
endlocal
if exist %SystemRoot%\System32\setx.exe (
%SystemRoot%\System32\setx.exe JAVA_HOME "%JAVA_HOME%" /M >nul
) else (
%SystemRoot%\System32\reg.exe ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v JAVA_HOME /t REG_SZ /d "%JAVA_HOME%" >nul
)
REM Update the system Path variable.
:SetSystemPath
set "SystemPath="
for /F "skip=2 tokens=1,2*" %%G in ('%SystemRoot%\System32\reg.exe QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v Path 2^>nul') do (
if /I "%%G" == "Path" (
set "SystemPath=%%I"
if defined SystemPath goto ProcessPath
)
)
:ProcessPath
setlocal EnableDelayedExpansion
rem Is the system environment variable PATH not present or has an empty value?
if not defined SystemPath set "PathToSet=%%SystemRoot%%\System32;!PHP!;!NODEJS!;!COMPOSER!;!YII!;!GIT!" & goto UpdatePath
endlocal
rem It is not possible to add folder paths to the system environment
rem variable PATH if this variable as stored in the registry contains
rem already a folder path with an equal sign or enclosed in double quotes.
set "PathCheck=%SystemPath:"=%"
for /F "eol=| tokens=1 delims==" %%I in ("%PathCheck%") do if not "%%I" == "%PathCheck%" set "ErrorChar==" & goto ErrorPath
setlocal EnableDelayedExpansion
if "!PathCheck!" == "!SystemPath!" goto CheckPath
endlocal
set "ErrorChar=""
goto ErrorPath
rem Determine if the system environment variable PATH ends already
rem with a semicolon in which case no additional semicolon must
rem be added left to the folder paths to add.
:CheckPath
if "!SystemPath:~-1!" == ";" (set "Separator=") else set "Separator=;"
set "PathCheck=!SystemPath!%Separator%"
set "PathToSet=!PathCheck!"
rem For each folder path to add check if the folder path without or with a
rem backslash at the end with a semicolon appended for an entire folder path
rem check is already in the system PATH value. This code does not work on
rem path to add contains an equal sign which is fortunately very rare.
if not "!PathCheck:%PHP%;=!" == "!PathCheck!" goto CheckNodeJS
if not "!PathCheck:%PHP%\;=!" == "!PathCheck!" goto CheckNodeJS
set "PathToSet=!PathToSet!!PHP!;"
:CheckNodeJS
if not "!PathCheck:%NODEJS%;=!" == "!PathCheck!" goto CheckComposer
if not "!PathCheck:%NODEJS%\;=!" == "!PathCheck!" goto CheckComposer
set "PathToSet=!PathToSet!!NODEJS!;"
:CheckComposer
if not "!PathCheck:%COMPOSER%;=!" == "!PathCheck!" goto CheckYII
if not "!PathCheck:%COMPOSER%\;=!" == "!PathCheck!" goto CheckYII
set "PathToSet=!PathToSet!!COMPOSER!;"
:CheckYII
if not "!PathCheck:%YII%;=!" == "!PathCheck!" goto CheckGit
if not "!PathCheck:%YII%\;=!" == "!PathCheck!" goto CheckGit
set "PathToSet=!PathToSet!!YII!;"
:CheckGit
if not "!PathCheck:%GIT%;=!" == "!PathCheck!" goto CheckUpdate
if not "!PathCheck:%GIT%\;=!" == "!PathCheck!" goto CheckUpdate
set "PathToSet=!PathToSet!!GIT!;"
:CheckUpdate
rem Do nothing if no folder path to add to system environment variable PATH.
rem Remove otherwise the semicolon from the end of the PATH string to set.
if "!PathToSet!" == "!PathCheck!" goto EndBatch
set "PathToSet=!PathToSet:~0,-1!"
:UpdatePath
set "UseSetx=1"
if not "!PathToSet:~1024,1!" == "" set "UseSetx="
if not exist %SystemRoot%\System32\setx.exe set "UseSetx="
if defined UseSetx (
%SystemRoot%\System32\setx.exe Path "!PathToSet!" /M >nul
) else (
set "ValueType=REG_EXPAND_SZ"
if "!PathToSet:%%=!" == "!PathToSet!" set "ValueType=REG_SZ"
%SystemRoot%\System32\reg.exe ADD "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /f /v Path /t !ValueType! /d "!PathToSet!" >nul
)
goto EndBatch
:ErrorPath
echo(
echo ERROR: Folder paths to add to system environment variable PATH are:
echo(
setlocal EnableDelayedExpansion
echo !PHP!
echo !NODEJS!
echo !COMPOSER!
echo !YII!
echo !GIT!
echo(
echo The system environment variable PATH contains the character '%ErrorChar%'.
echo This procedure for updating the system environment variable PATH
echo does not support updating the variable PATH with that character.
echo(
echo The folder paths must be manually added to the system environment variable PATH.
echo Please click on Windows Start button, type on keyboard the word environment,
echo and click on suggested item "Edit the system environment variables".
echo Add or edit the environment variable PATH in the lower list of system
echo environment variables and append the folder paths as displayed here
echo which are not already in the list of folder paths.
echo(
:EndBatch
endlocal
endlocal
PAUSE
The code for the update of the system environment variable Path is derived from:
What is the best practice for adding a directory to the PATH environment variable on Windows?
I come at this question with a Linux perspective. Usually setting an environment variable in Linux ($myVar=1) only sets it for the current process but not any child process that it spawns.
To allow any child process to read a variable you need to export envVar=2. In Windows the set command already does this for you. This is generally what you want.
The setx command sets a variable permanently for the current user, but strangely enough this is not reflected in the current process, you will need to open another cmd.exe window for it to take effect.
C:\> set foobar=1
C:\> powershell "echo ${env:foobar}"
1
C:\> setx barfoo 2
SUCCESS: Specified value was saved.
C:\> powershell "echo ${env:barfoo}" # not present
C:\>
Also notice the strictly necessary syntax difference between set and setx.