PowerShell Environment Variables

Environment variables are named values that the operating system stores and makes available to every program and script. They hold system-wide information like the path to user folders, the system drive, and the computer name. PowerShell reads, sets, and deletes environment variables with the $env: prefix — no extra imports required.

What Are Environment Variables?

Picture a whiteboard on the wall of an office. Anyone in the building can walk up and read what is written. Environment variables work exactly the same way — the OS writes values like the username, temp folder path, or installed software locations, and every running program can read them.
+---------------------------+
|   Operating System        |
|                           |
|  USERNAME  = "Alice"      |
|  TEMP      = "C:\Temp"    |
|  PATH      = "C:\Windows" |
|  COMPUTERNAME = "PC-01"   |
+---------------------------+
         |
         |  Any script or app can read these
         v
+---------------------------+
|   PowerShell Script       |
|  $env:USERNAME  → "Alice" |
+---------------------------+

Reading Environment Variables

PowerShell uses the $env: drive to access environment variables. Reading them is as simple as using a regular variable.
# Read common environment variables
Write-Output $env:USERNAME        # Current logged-in user
Write-Output $env:COMPUTERNAME    # Machine name
Write-Output $env:USERPROFILE     # Path to user's home folder
Write-Output $env:TEMP            # Temporary folder path
Write-Output $env:SystemDrive     # Drive where Windows is installed (C:)
Write-Output $env:APPDATA         # Application data folder

Sample Output

Alice
PC-ALICE-01
C:\Users\Alice
C:\Users\Alice\AppData\Local\Temp
C:
C:\Users\Alice\AppData\Roaming

Listing All Environment Variables

# List every environment variable in the current session
Get-ChildItem Env:

# Filter and search
Get-ChildItem Env: | Where-Object Name -like "USER*"

Setting Environment Variables

Temporary (Session Only)

Setting a variable with $env: keeps it alive only for the current PowerShell session. Closing the terminal removes it.
$env:MY_APP_MODE = "Debug"
Write-Output $env:MY_APP_MODE   # Output: Debug

Permanent (System-Wide or User-Level)

Use [System.Environment]::SetEnvironmentVariable() to make a variable persist after the session ends.
# Set a permanent variable for the current user
[System.Environment]::SetEnvironmentVariable("MY_APP_MODE", "Production", "User")

# Set a permanent variable for the whole machine (requires admin rights)
[System.Environment]::SetEnvironmentVariable("MY_APP_MODE", "Production", "Machine")

Three Scope Levels for SetEnvironmentVariable

ScopeWho Sees ItAdmin Required
ProcessCurrent PowerShell session onlyNo
UserCurrent logged-in user — persists after rebootNo
MachineAll users on the computer — persists after rebootYes

Deleting Environment Variables

# Remove a session-level environment variable
Remove-Item Env:MY_APP_MODE

# Remove a permanent user-level variable
[System.Environment]::SetEnvironmentVariable("MY_APP_MODE", $null, "User")
Passing $null as the value deletes the permanent variable from the registry.

Reading the PATH Variable

The PATH variable holds a list of directories where Windows looks for executable programs. In PowerShell, it appears as one long string with entries separated by semicolons.
# View the full PATH
Write-Output $env:PATH

# View each entry on a separate line
$env:PATH -split ";" | ForEach-Object { Write-Output $_ }

Adding a Directory to PATH (Session Only)

$env:PATH += ";C:\MyTools"

Adding a Directory to PATH (Permanently)

$currentPath = [System.Environment]::GetEnvironmentVariable("PATH", "User")
$newPath = $currentPath + ";C:\MyTools"
[System.Environment]::SetEnvironmentVariable("PATH", $newPath, "User")

Using Environment Variables in Paths

Instead of hard-coding paths, scripts use environment variables to stay portable across different computers.
# Hard-coded — breaks on different machines
$logFile = "C:\Users\Alice\Documents\log.txt"

# Portable — works on any machine
$logFile = "$env:USERPROFILE\Documents\log.txt"
Write-Output $logFile   # Output: C:\Users\Alice\Documents\log.txt

Real-World Script: Setup Checker

function Check-Setup {
    $javaHome = $env:JAVA_HOME
    $pythonPath = $env:PYTHONPATH

    if ($javaHome) {
        Write-Output "Java found at: $javaHome"
    } else {
        Write-Output "Java is NOT configured. Set JAVA_HOME."
    }

    if ($pythonPath) {
        Write-Output "Python path: $pythonPath"
    } else {
        Write-Output "Python path is NOT set."
    }
}

Check-Setup

Environment Variable Access Methods Side by Side

MethodReadWritePersist?
$env:NAMEYesYesNo — session only
[System.Environment]::GetEnvironmentVariable()YesNoN/A
[System.Environment]::SetEnvironmentVariable()NoYesYes — User or Machine
Get-ChildItem Env:Yes (all)NoNo

Summary

Environment variables are key-value pairs the OS maintains and shares with all running programs. PowerShell accesses them through $env: for quick reads and writes, or through [System.Environment] methods for permanent changes. Using environment variables instead of hard-coded paths makes scripts reliable, portable, and easy to deploy on any machine.

Leave a Comment

Your email address will not be published. Required fields are marked *