Use this when programs can't find tools or settings in environment variables.
Environment Variables · Command Prompt
Environment variables hold settings like PATH, TEMP and USERNAME for processes. In cmd use %NAME% and set or setx; system and user variables are stored in the registry and inherited by new processes.
1. set <NAME> — value in this window?
2. Persist: setx (only new windows see it).
3. Machine scope: setx /M as admin.
4. Delayed expansion in loops: setlocal enabledelayedexpansion and !var!.
5. Open a new window to verify.
variable set (session or persistent) → expanded at parse time (%var%) or run time (!var!) → passed to children
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Common mistakes Narrow the fault
setxtruncating PATH — use the System Properties dialog or PowerShell.- Expecting running processes to see new variables (only new processes do).
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- PATH order decides which executable runs.
echo %PATH%— PATH in CMDset— environment variableswhere <name>— command resolutionecho %ERRORLEVEL%— exit codesetx <NAME> <value>— persist a user variable
ReferenceWhy it matters1 section · depth on demand
How it works
Windows keeps environment variables at two scopes: machine (HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment) and user (HKCU\Environment). A new process inherits the merged set from its parent; user PATH is appended to machine PATH. Changes reach only processes started afterwards (Explorer is notified by setx and the System Properties dialog). Temporary changes in a shell (set X=1, $env:X='1') last only for that shell.
ReferenceImportant terms & boundaries2 sections · depth on demand
In depth
Scope, persistence and who sees the change
| Method | Scope | Persistent | Current window sees it |
|---|---|---|---|
set VAR=value (cmd) | Current process | No | Yes |
$env:VAR = 'value' (PowerShell) | Current process | No | Yes |
setx VAR value | User | Yes | No — new processes only |
setx VAR value /M | Machine (admin) | Yes | No |
[Environment]::SetEnvironmentVariable('VAR','value','User'/'Machine') | User/Machine | Yes | No |
New values reach processes started after the change by Explorer; services need a restart, and some long-running parents (for example an already open terminal host) need restarting too.
The PATH truncation trap
The setx reference notes the 1024-character limit when assigning values. setx PATH "%PATH%;C:\Tools" also expands the merged machine + user PATH into the user PATH, duplicating entries. Safer:
$p = [Environment]::GetEnvironmentVariable('Path','User')
[Environment]::SetEnvironmentVariable('Path', ($p.TrimEnd(';') + ';C:\Tools'), 'User')
Where values are stored
| Scope | Registry |
|---|---|
| Machine | HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment |
| User | HKCU\Environment |
| Volatile (per logon) | HKCU\Volatile Environment |
Process PATH = machine PATH followed by user PATH. A tool found in the wrong version usually comes from an earlier directory in the machine PATH.
Diagnose "command not found" or wrong version
where.exe python
Get-Command python -All | Select Source
$env:Path -split ';' | Select-String -SimpleMatch 'Python'
Expandable values
Values of type REG_EXPAND_SZ (%USERPROFILE%\bin) are expanded when a process starts. Writing them as REG_SZ through a script makes %USERPROFILE% appear literally in PATH — check the type with reg query HKCU\Environment /v Path.
Commands
set :: cmd: list
setx PATH "%PATH%;C:\Tools" :: persistent user (truncates at 1024 chars!)
[Environment]::SetEnvironmentVariable('APP_MODE','prod','Machine')
$env:APP_MODE
Documented example, not captured output.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Command Prompt → Environment Variables. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands · confidence 94%