The essentials
Quick reference
One focused task per row. Jump to the related section for complete, working examples.
| Use | Syntax | Examples |
|---|---|---|
| List process environment | Get-ChildItem Env: | Sort-Object Name | View examples |
| Read one value | $env:TEMP | View examples |
| Set for current process | $env:APP_MODE = 'development' | View examples |
| Remove from current process | Remove-Item Env:APP_MODE | View 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]::PathSeparator | View examples |
| Resolve all commands | Get-Command python -All | View examples |
| Expand percent variables | [Environment]::ExpandEnvironmentVariables('%TEMP%\report.log') | View examples |
| Start a fresh process | Start-Process pwsh | View examples |
| Inspect module search paths | $env:PSModulePath -split [IO.Path]::PathSeparator | View examples |
| Inspect runtime identity | [Environment]::UserName | View 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
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.
$env:APP_MODE = 'testing'
pwsh -NoProfile -Command '$env:APP_MODE'
Remove-Item Env:APP_MODE -ErrorAction SilentlyContinue 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.
$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') 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.
$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 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.
$env:REPORT_DIR = "$env:TEMP\Reports"
$powerShellPath = "$env:REPORT_DIR\today.log"
$windowsPath = [Environment]::ExpandEnvironmentVariables('%REPORT_DIR%\today.log')
$powerShellPath
$windowsPath 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.
[pscustomobject]@{
Computer = $env:COMPUTERNAME
User = [Environment]::UserName
Temp = $env:TEMP
CurrentDirectory = (Get-Location).Path
PowerShell = $PSVersionTable.PSVersion.ToString()
} | Format-List Sources and further reading
References
Authoritative documentation used to verify and expand this cheat sheet.
Help us improve
Found a typo or missing example?
Tell us what would make this cheat sheet clearer, more complete, or more useful.



