Persistent Environment Variables and PATH
Have you ever set a variable with set MY_VAR=Hello in Command Prompt, only to have it disappear when you closed the window?
Variables set with the set command are valid only in the current session and disappear when you close the window. If you want the value to be kept even after a restart, use the setx command. Understanding the difference between these two correctly is the first step to mastering environment variables.
What Are Environment Variables
Environment variables are a mechanism that holds setting values referenced by the operating system and applications as key-value pairs.
To display all current environment variables, run the following command.
set
To check a specific variable, use echo.
echo %PATH%

中小企業経営者のためのRPA入門 RPA導入を成功させる方法

60分でわかる! AIエージェント 超 入門

コマンドラインの黒い画面が怖いんです。

知識・才能ゼロでもらく~に月10万円稼ぐ! よくわかるAI副業超入門
Commonly Used System Environment Variables
| Variable | Description | Example |
|---|---|---|
%PATH% | Search path for executable files | C:\Windows\System32;... |
%TEMP% / %TMP% | Location for temporary files | C:\Users\user\AppData\Local\Temp |
%USERNAME% | Current user name | user |
%COMPUTERNAME% | Computer name | DESKTOP-XXXX |
%USERPROFILE% | Path of the user folder | C:\Users\user |
%APPDATA% | Application data folder | C:\Users\user\AppData\Roaming |
%WINDIR% | Windows folder | C:\Windows |
%SYSTEMROOT% | Windows system folder | C:\Windows |
The set Command (Session Only)
The set command creates or changes variables that are valid only within the current Command Prompt session.
set VariableName=Value
set MY_APP_HOME=C:\Apps\MyApp
echo %MY_APP_HOME%
To delete a variable, set its value to empty.
set MY_APP_HOME=
If you write set MY_VAR = Hello, the variable name becomes MY_VAR (with a trailing space) and the value becomes Hello (with a leading space). You can no longer reference it with %MY_VAR%, so write it without spaces around =, as in set MY_VAR=Hello. Enclosing the whole assignment in quotation marks, as in set "MY_VAR=Hello", prevents extra trailing spaces at the end of the line from getting into the value, so it is the recommended style in batch files.
Use set /a for numeric calculations.
set /a RESULT=10 + 5
echo %RESULT%
Variables set with set disappear when you close the Command Prompt window. Even when you use set inside a batch file, the variable disappears when that batch file finishes running (or at endlocal if you are using setlocal).
The setx Command (Persistent Settings)
The setx command writes the value to the Windows registry, so the value is kept even after you close the terminal or restart the PC.
setx VariableName "Value"
set uses VariableName=Value (the = is required), but setx uses VariableName "Value" (no =, separated by a space). This difference is easy to get wrong, so be careful.
Setting a User Environment Variable (Default)
setx MY_APP_HOME "C:\Apps\MyApp"
Setting a System Environment Variable (/M Option)
Adding the /M option sets it as a system environment variable that affects all users.
setx MY_APP_HOME "C:\Apps\MyApp" /M
To set a system environment variable by adding the /M option to setx, you must run as administrator. Without the privileges, an error occurs.
setx Does Not Affect the Current Session
An important point to note is that variables set with setx are not reflected in the current Command Prompt window. They become available after you open a new window.
setx NEW_VAR "Hello"
echo %NEW_VAR%
%NEW_VAR% is displayed as is because the variable has not been set in the current session. When you open a new window, Hello is displayed correctly.
Comparing set and setx
| Item | set | setx |
|---|---|---|
| Scope | Current session only | Persistent (valid after restart) |
| Syntax | set VAR=Value | setx VAR "Value" |
| Reflected in current session | Immediately | Not reflected (valid in a new window) |
| System-wide setting | Not possible | Possible with the /M option |
| Administrator privileges | Not required | Not required for user variables, required for system variables |
| Temporary use inside a batch file | Appropriate | Not suitable (a write occurs on every run) |
Adding to PATH
PATH is a special environment variable that lists the directories searched for executable files.
Temporarily Adding to PATH (Session Only)
set PATH=%PATH%;C:\MyTools
Permanently Adding to PATH (setx)
setx PATH "%PATH%;C:\MyTools"
%PATH% in Command Prompt is the combined value of the system PATH and the user PATH. If you save this to the user environment variable with setx PATH, the contents of the system PATH are copied redundantly into the user PATH. When changing the PATH value, we recommend the PowerShell method below.
The setx command limits the length of the value you can set to 1024 characters. If your current %PATH% is long, running setx PATH "%PATH%;NewPath" risks truncating PATH at 1024 characters and losing existing paths. Check the length with echo %PATH% before changing PATH.
Safely Adding to PATH (via PowerShell)
If your PATH is long or you want to add to it safely, using PowerShell is the reliable method. Because it reads only the user PATH and appends to it, you avoid duplicating the system PATH and the 1024-character truncation.
rem Add to the user PATH (avoids the 1024-character limit)
powershell -Command "[System.Environment]::SetEnvironmentVariable('PATH', [System.Environment]::GetEnvironmentVariable('PATH','User') + ';C:\MyTools', 'User')"
rem Add to the system PATH (requires administrator privileges)
powershell -Command "[System.Environment]::SetEnvironmentVariable('PATH', [System.Environment]::GetEnvironmentVariable('PATH','Machine') + ';C:\MyTools', 'Machine')"
If you change PATH frequently, using the GUI (System Properties) is the safest method. Press Win + R and type sysdm.cpl to open “System Properties,” where you can safely edit PATH from the “Environment Variables” button.
Practical Example in a Batch File
Here is an example of using set and setx for different purposes in an installation script.
@echo off
setlocal
rem Installation destination (use set for temporary references inside the batch file)
set INSTALL_DIR=C:\MyApp
echo Starting installation...
rem ... installation process ...
mkdir "%INSTALL_DIR%\bin" 2>nul
copy "myapp.exe" "%INSTALL_DIR%\bin\" >nul
rem Add to PATH so it can be used immediately in the current session
set PATH=%PATH%;%INSTALL_DIR%\bin
rem Set persistently so it is also valid in Command Prompt windows opened later
setx MY_APP_HOME "%INSTALL_DIR%"
setx MY_APP_VERSION "1.0.0"
echo.
echo Setup is complete.
echo Open a new Command Prompt to apply the environment variables.
endlocal
In an installation script, you may want to run a command right after installation. Because setx is not reflected in the current session, a practical pattern is to set the value temporarily with set and also set it persistently with setx for future sessions. Note that this script does not permanently add to PATH. To change PATH permanently, use the PowerShell method in the previous section.
Deleting a Persistent Environment Variable
setx has no option to delete a variable. To delete a user environment variable, remove it from the registry with reg delete.
reg delete "HKCU\Environment" /v MY_APP_HOME /f
For a system environment variable, run the following command as administrator.
reg delete "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v MY_APP_HOME /f
Because reg delete edits the registry directly, the change may not appear in Command Prompts opened from an already running Explorer until you sign out and back in. It also does not clear the variable in the current session, so run set MY_APP_HOME= if needed.
Viewing and Editing Environment Variables in the GUI
If you are not used to the command line, or to edit PATH safely, the GUI is convenient.
Press Win + R and type sysdm.cpl to open “System Properties.” From the “Environment Variables” button on the “Advanced” tab, you can view and edit user variables and system variables separately.
Summary
| Operation | Command |
|---|---|
| Set a temporary variable | set VariableName=Value |
| Set a persistent variable (user) | setx VariableName "Value" |
| Set a persistent variable (system) | setx VariableName "Value" /M (requires administrator privileges) |
| Temporarily add to PATH | set PATH=%PATH%;PathToAdd |
| Check a variable | echo %VariableName% |
| Display all variables | set |
| Edit in the GUI | Launch sysdm.cpl |