TExcelTAD
Home › PowerShell › PowerShell Environment Variables

PowerShell Environment Variables

Environment variables are name/value pairs that the operating system hands to every process: the search PATH, the user's home folder, the language, plus any settings your tools need (JAVA_HOME, NODE_ENV, API endpoints). PowerShell exposes them through the special $env: prefix and the Env: drive.

Reading environment variables

powershell · try it yourself
$env:HOME
$env:TZ
$env:LANG
"[$env:THIS_DOES_NOT_EXIST]"
$null -eq $env:THIS_DOES_NOT_EXIST
/tmp
UTC
C.UTF-8
[]
True

A missing variable is simply $null. On Windows, names are case-insensitive ($env:path = $env:PATH); on Linux and macOS they are case-sensitive, so always use the exact spelling.

Common variables differ by platform:

Meaning Windows Linux / macOS
home folder $env:USERPROFILE $env:HOME
user name $env:USERNAME $env:USER
computer name $env:COMPUTERNAME $env:HOSTNAME (shell-dependent)
temp folder $env:TEMP $env:TMPDIR (often unset)
program search path $env:Path (;-separated) $env:PATH (:-separated)

PowerShell's own $HOME variable and [System.IO.Path]::GetTempPath() work on every OS, so prefer them in portable scripts.

Setting and removing

Assigning to $env:NAME creates or changes a variable for the current process and every program it starts afterwards:

powershell · try it yourself
$env:APP_MODE = 'staging'
$env:APP_MODE

# A child process inherits it
sh -c 'echo "child sees APP_MODE=$APP_MODE"'

$env:APP_MODE += '-eu'
$env:APP_MODE

Remove-Item Env:APP_MODE          # or: $env:APP_MODE = $null
"after removal: [$env:APP_MODE]"
staging
child sees APP_MODE=staging
staging-eu
after removal: []

The change disappears when the PowerShell session ends. That's ideal for configuring a tool just for one script run.

The Env: drive

Environment variables also appear as items on the Env: drive, so the item cmdlets work on them. This is how you list or search them:

powershell · try it yourself
$env:TAD_REGION = 'north'
$env:TAD_TIER = 'gold'

Get-ChildItem Env:TAD_* | Sort-Object Name | ForEach-Object { "$($_.Name)=$($_.Value)" }
Test-Path Env:TAD_TIER
(Get-Item Env:TAD_REGION).Value
TAD_REGION=north
TAD_TIER=gold
True
north

Working with PATH

PATH is a list of folders, separated by ; on Windows and : elsewhere. Split it with the platform's separator instead of hard-coding one:

powershell · try it yourself
$sep = [System.IO.Path]::PathSeparator
"separator: $sep"

$env:PATH = "/opt/tools/bin$sep$env:PATH"      # put a folder first
$folders = $env:PATH -split [regex]::Escape($sep)
$folders[0]
$folders -contains '/usr/bin'
separator: :
/opt/tools/bin
True

Using environment variables for configuration

A common pattern: read settings from the environment, with sensible defaults, so the same script runs on a laptop, in CI and in production:

powershell · try it yourself
function Get-AppConfig {
    [pscustomobject]@{
        ApiUrl   = $env:API_URL ?? 'http://localhost:5000'
        Retries  = [int]($env:API_RETRIES ?? 3)
        LogLevel = $env:LOG_LEVEL ?? 'Info'
    }
}

Get-AppConfig | Format-List

$env:API_URL = 'https://api.example.com'
$env:API_RETRIES = '5'
Get-AppConfig | Format-List

ApiUrl   : http://localhost:5000
Retries  : 3
LogLevel : Info


ApiUrl   : https://api.example.com
Retries  : 5
LogLevel : Info

Environment values are always strings; convert them, as [int] does here for Retries.

Persistent variables (Windows)

$env: changes last only for the session. To store a variable permanently for a user on Windows, use .NET's SetEnvironmentVariable with a target; on Linux and macOS, add an export line to your shell profile or $PROFILE instead.

text
# Windows only: persists in the registry, visible to NEW processes
[Environment]::SetEnvironmentVariable('APP_MODE', 'production', 'User')
[Environment]::GetEnvironmentVariable('APP_MODE', 'User')
production

# Machine-wide (needs an elevated session)
[Environment]::SetEnvironmentVariable('APP_MODE', 'production', 'Machine')

Warning: Don't store passwords or API keys in plain environment variables on shared machines, and never print them in logs. Use a secret store such as the Microsoft.PowerShell.SecretManagement module or your CI system's secrets.

Test yourself

How do you read the environment variable PATH in PowerShell?

  1. $PATH
  2. %PATH%
  3. $env:PATH
  4. env.PATH

How long does $env:MODE = 'test' last?

  1. Forever
  2. Until reboot
  3. For the current process and its child processes
  4. Only for the next command

What type is the value of an environment variable?

  1. Whatever was assigned
  2. Always a string
  3. Int32
  4. Object