The essentials

Quick reference

One focused task per row. Jump to the related section for complete, working examples.

UseSyntaxExamples
List process environmentGet-ChildItem Env: | Sort-Object NameView examples
Read one value$env:TEMPView examples
Set for current process$env:APP_MODE = 'development'View examples
Remove from current processRemove-Item Env:APP_MODEView examples
Read persistent user value[Environment]::GetEnvironmentVariable('APP_MODE', ` 'User')View examples
Set persistent user value[Environment]::SetEnvironmentVariable('APP_MODE', ` 'production', 'User')View examples
Remove persistent user value[Environment]::SetEnvironmentVariable('APP_MODE', $null, ` 'User')View examples
Read machine value[Environment]::GetEnvironmentVariable('Path', 'Machine')View examples
Split PATH safely$env:Path -split [IO.Path]::PathSeparatorView examples
Resolve all commandsGet-Command python -AllView examples
Expand percent variables[Environment]::ExpandEnvironmentVariables('%TEMP%\report.log')View examples
Start a fresh processStart-Process pwshView examples
Inspect module search paths$env:PSModulePath -split [IO.Path]::PathSeparatorView examples
Inspect runtime identity[Environment]::UserNameView examples

Windows environment variables are inherited snapshots. PowerShell's Env provider changes the current process; .NET APIs can persist user or machine values for future processes. Preserve PATH entry boundaries, avoid duplicate or nonexistent directories, and never use environment storage for secrets.

Step by step

Detailed examples

01

Treat the process environment as a local snapshot

The Env provider exposes only the current process environment. Changes affect child processes started later but cannot rewrite a parent or already-running application. In PowerShell 7.5 an empty string can be distinct from removal; assigning null removes the variable.

Set, pass, and remove a temporary value
$env:APP_MODE = 'testing'
pwsh -NoProfile -Command '$env:APP_MODE'
Remove-Item Env:APP_MODE -ErrorAction SilentlyContinue
Back to quick reference ↑
02

Read before writing user or machine scope

Environment.SetEnvironmentVariable writes persistent User or Machine stores; machine writes normally require elevation. New processes may combine scopes according to Windows rules, while existing processes keep old snapshots. Back up the exact previous value and avoid setx truncation and expansion surprises.

Back up and preview a user value
$name = 'APP_MODE'
$before = [Environment]::GetEnvironmentVariable($name, 'User')
[pscustomobject]@{ Name = $name; Scope = 'User'; Current = $before; Proposed = 'production' }
# Apply only after review:
# [Environment]::SetEnvironmentVariable($name, 'production', 'User')
Back to quick reference ↑
03

Preserve PATH entries as structured data

PATH is a delimiter-separated list searched in order, and duplicates or writable untrusted directories can change executable selection. Split into entries, normalize carefully, validate existence, and write back only after preserving user and machine components.

Audit current PATH resolution
$entries = $env:Path -split [IO.Path]::PathSeparator
$entries | ForEach-Object {
  [pscustomobject]@{ Path = $_; Exists = Test-Path -LiteralPath $_ }
}
Get-Command python -All | Select-Object CommandType, Source, Path
Back to quick reference ↑
04

Distinguish PowerShell interpolation from Windows expansion

PowerShell expands environment references in double-quoted strings. ExpandEnvironmentVariables processes percent-delimited references using the current process. Persistent values may contain expandable references whose behavior depends on how a process constructs its environment.

Compare expansion forms
$env:REPORT_DIR = "$env:TEMP\Reports"
$powerShellPath = "$env:REPORT_DIR\today.log"
$windowsPath = [Environment]::ExpandEnvironmentVariables('%REPORT_DIR%\today.log')
$powerShellPath
$windowsPath
Back to quick reference ↑
05

Inspect the environment of the launching system

Services, scheduled tasks, remoting endpoints, terminals, and desktop shells may start with different variables and accounts. User profiles are not a reliable configuration channel for noninteractive services. Define required values in the service or task configuration and log non-secret diagnostics at startup.

Capture a non-secret runtime baseline
[pscustomobject]@{
  Computer = $env:COMPUTERNAME
  User = [Environment]::UserName
  Temp = $env:TEMP
  CurrentDirectory = (Get-Location).Path
  PowerShell = $PSVersionTable.PSVersion.ToString()
} | Format-List
Back to quick reference ↑

Sources and further reading

References

Authoritative documentation used to verify and expand this cheat sheet.

  1. Microsoftabout_Environment_Variableslearn.microsoft.com
  2. MicrosoftEnvironment.SetEnvironmentVariable Methodlearn.microsoft.com
  3. MicrosoftEnvironment.GetEnvironmentVariable Methodlearn.microsoft.com
  4. Microsoftabout_Command_Precedencelearn.microsoft.com

Help us improve

Found a typo or missing example?

Tell us what would make this cheat sheet clearer, more complete, or more useful.

Share feedback