Skip to content

Commands to View and Configure Windows Environment Variables

Windows environment variables are read from an environment block that is created when a process starts. This article explains in detail the scope of environment variables (process, user, system), how they are persisted, how to view and configure them using CMD and PowerShell, and clarifies exactly how user and system environment variables differ and how they are actually seen from each context.


VariableExampleDescription
<<VAR_NAME>>PATHName of the environment variable to operate on
<<VALUE>>C:\ToolsValue to set

Step 1: Scope and storage locations of environment variables

Section titled “Step 1: Scope and storage locations of environment variables”
TypeStorage locationScopePersistenceNotes
Process environment variablesMemoryCurrent process onlyTemporaryCMD: set / PowerShell: $env:
User environment variablesHKCU\EnvironmentCurrent userPersistentsetx / .NET API
System environment variables (Machine)HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\EnvironmentAll usersPersistentsetx /M / .NET API (admin)

Process, user, and system are separate layers

Section titled “Process, user, and system are separate layers”
  • Process environment variables: effective only in the current shell/process
  • User environment variables: specific to the current user profile
  • System environment variables: shared by all users
  • PATH often appears as the concatenation of “system PATH + user PATH”

Because “what you see depends on where you look from”, you must decide the intended scope before setting any value.


Step 2: Viewing and setting environment variables in CMD (%VAR%)

Section titled “Step 2: Viewing and setting environment variables in CMD (%VAR%)”
Terminal window
set
Terminal window
echo %<<VAR_NAME>>%

2-3. Temporarily set a process environment variable (current CMD only)

Section titled “2-3. Temporarily set a process environment variable (current CMD only)”
Terminal window
set <<VAR_NAME>>=<<VALUE>>

2-4. Set persistent environment variables with setx

Section titled “2-4. Set persistent environment variables with setx”
Terminal window
setx <<VAR_NAME>> "<<VALUE>>"
Terminal window
setx <<VAR_NAME>> "<<VALUE>>"

System scope (/M required, administrator only)

Section titled “System scope (/M required, administrator only)”
Terminal window
setx <<VAR_NAME>> "<<VALUE>>" /M

Characteristics of setx

  • Does not update the current CMD session
  • Values containing %PATH% are expanded at runtime and stored as fixed text
  • Long PATH values can be truncated (therefore not recommended for PATH)

Step 3: How to verify user and system environment variables in CMD

Section titled “Step 3: How to verify user and system environment variables in CMD”
Terminal window
reg query HKCU\Environment
Terminal window
reg query "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

Using reg query lets you see the raw value per scope, so you can accurately check whether PATH is concatenated, overridden, or truncated.


Step 4: Safe editing of PATH (warnings when using CMD)

Section titled “Step 4: Safe editing of PATH (warnings when using CMD)”

The following is a dangerous pattern and is not recommended:

Terminal window
# setx PATH "%PATH%;C:\Tools"

Reasons:

  • %PATH% is expanded and saved as a fixed string at that moment
  • REG_EXPAND_SZ may be converted to REG_SZ, causing loss of %SystemRoot% and other placeholders
  • PATH may be truncated if it is too long

For editing PATH, you should prefer PowerShell with the .NET API (see below) instead of setx.


Step 5: Viewing and setting environment variables in PowerShell ($env:VAR)

Section titled “Step 5: Viewing and setting environment variables in PowerShell ($env:VAR)”
Terminal window
Get-ChildItem Env:
Terminal window
$env:<<VAR_NAME>>

5-3. Temporarily set a process environment variable

Section titled “5-3. Temporarily set a process environment variable”
Terminal window
$env:<<VAR_NAME>> = "<<VALUE>>"

Step 6: Most accurate way to view user and system variables in PowerShell

Section titled “Step 6: Most accurate way to view user and system variables in PowerShell”
Terminal window
Get-ItemProperty "HKCU:\Environment"
Terminal window
Get-ItemProperty "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

Section titled “Step 7: Setting persistent variables with PowerShell (.NET API / recommended)”
Terminal window
[System.Environment]::SetEnvironmentVariable("<<VAR_NAME>>","<<VALUE>>","User")
Terminal window
[System.Environment]::SetEnvironmentVariable("<<VAR_NAME>>","<<VALUE>>","Machine")

Advantages of this method

  • Prevents PATH truncation
  • Preserves REG_EXPAND_SZ where appropriate
  • Updates values in a safe and predictable way

Section titled “Step 8: Safely editing the PATH variable (.NET API / strongly recommended)”
Terminal window
$add = "C:\Tools"
$current = [System.Environment]::GetEnvironmentVariable("PATH","Machine")
$new = ($current.TrimEnd(';') + ';' + $add).Trim(';')
[System.Environment]::SetEnvironmentVariable("PATH",$new,"Machine")

9-1. Delete from the current PowerShell process only

Section titled “9-1. Delete from the current PowerShell process only”
Terminal window
Remove-Item Env:<<VAR_NAME>> -ErrorAction SilentlyContinue

9-2. Delete persistent values (remove from registry)

Section titled “9-2. Delete persistent values (remove from registry)”
Terminal window
Remove-ItemProperty -Path "HKCU:\Environment" -Name "<<VAR_NAME>>" -ErrorAction SilentlyContinue
Terminal window
Remove-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" -Name "<<VAR_NAME>>" -ErrorAction SilentlyContinue

Terminal window
Get-ChildItem Env:<<VAR_NAME>>
Terminal window
echo %<<VAR_NAME>>%

10-3. Immediately load the persistent value into the current PowerShell session

Section titled “10-3. Immediately load the persistent value into the current PowerShell session”
Terminal window
$env:<<VAR_NAME>> = [System.Environment]::GetEnvironmentVariable("<<VAR_NAME>>","User")

Step 11: Bulk setting multiple environment variables (user and system)

Section titled “Step 11: Bulk setting multiple environment variables (user and system)”
Terminal window
# User environment
$vars = @{
"APPDATA_DIR" = "C:\AppData"
"LOG_PATH" = "C:\Logs"
"TOOLS" = "C:\Tools"
}
foreach ($v in $vars.GetEnumerator()) {
[System.Environment]::SetEnvironmentVariable($v.Key,$v.Value,"User")
}
# System environment (administrator)
$sysVars = @{
"JAVA_HOME" = "C:\Program Files\Java\<<VALUE>>"
}
foreach ($v in $sysVars.GetEnumerator()) {
[System.Environment]::SetEnvironmentVariable($v.Key,$v.Value,"Machine")
}

  • Process, user, and system environment variables exist independently.
  • CMD and PowerShell may show different scopes or combined values (especially for PATH).
  • To see the true user and system values, inspect HKCU/HKLM directly.
  • setx can persist values but has serious drawbacks for PATH and is not recommended for it.
  • The .NET API (SetEnvironmentVariable) is the safest and most reliable way to manage environment variables.