Pcornel
Browse topics
Options
Recent
Favorites
Windows · Technical reference

Environment Variables · Command Prompt

Direct answer

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.

When to use this

Use this when programs can't find tools or settings in environment variables.

First 30 seconds

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.

Operational verification

variable set (session or persistent) → expanded at parse time (%var%) or run time (!var!) → passed to children

Context & applicabilityCommon mistakes · platform/version boundaries
Platform / version applicability
Windows execution, automation and observability
applies
Structured applicability is explicit Topic metadata; it does not infer compatibility beyond the stored rows.
At a glance

Operational reference

Fast context before you go deeper.

PlatformWindows execution, automation and observability
Scope / prerequisiteUse this when programs can't find tools or settings in environment variables.
What next?3 suggested next steps
Evidence1
On this pageJump to a specific section
Operational workflow

Verification & Troubleshooting

Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.

1 steps Observe → verify → isolate → change minimally → repeat the original test.
ObservedRecord what the original test actually shows

Capture the concrete output, symptom, packet, log or counter before changing anything.

Step 1 Common mistakes Narrow the fault
  • setx truncating PATH — use the System Properties dialog or PowerShell.
  • Expecting running processes to see new variables (only new processes do).
Evidence references

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.

observation · 1
Key points
  • PATH order decides which executable runs.
Key checks
  • echo %PATH% — PATH in CMD
  • set — environment variables
  • where <name> — command resolution
  • echo %ERRORLEVEL% — exit code
  • setx <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

MethodScopePersistentCurrent window sees it
set VAR=value (cmd)Current processNoYes
$env:VAR = 'value' (PowerShell)Current processNoYes
setx VAR valueUserYesNo — new processes only
setx VAR value /MMachine (admin)YesNo
[Environment]::SetEnvironmentVariable('VAR','value','User'/'Machine')User/MachineYesNo

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

ScopeRegistry
MachineHKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
UserHKCU\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.

Source basisSource attribution states what this section is based on; it is not independent proof that the current environment matches it.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Technical verification history

Verification history & basis

When and against what this Topic was checked. Later text edits do not count as a new check.

2026-09-14 · vendor docWindows execution, automation and observability current

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%
Confirmation

Confirm action

Continue?