Environment Variables¶
Linux¶
Abstract
Most modern Linux distributions use Systemd and it's environment variables become accessible to both terminal and GUI apps. Ensure your system uses it before proceeding.
User¶
Applying Changes
Changes in environment.d require you to log out and log back in, or restart the user systemd instance.
System¶
Windows¶
Add a New Variable¶
User variable
[System.Environment]::SetEnvironmentVariable("VAR_NAME", "Value", [System.EnvironmentVariableTarget]::User)
System variable
[System.Environment]::SetEnvironmentVariable("VAR_NAME", "Value", [System.EnvironmentVariableTarget]::Machine)
Add to PATH¶
PowerShell does not have a built-in cmdlet to safely append to the PATH without manually managing semicolons. The safest approach is to create a reusable function.
Profile Setup
Add the following function to your $PROFILE so it is always available in your PowerShell sessions.
function Add-Path {
param(
[Parameter(Mandatory=$true)]
[string]$NewPath,
[System.EnvironmentVariableTarget]$Scope = [System.EnvironmentVariableTarget]::User
)
$current = [System.Environment]::GetEnvironmentVariable("Path", $Scope)
$updatedParts = ($current -split ';' | Where-Object { $_ }) + $NewPath
$seen = [System.Collections.Generic.HashSet[string]]::new([System.StringComparer]::OrdinalIgnoreCase)
$deduped = $updatedParts | Where-Object { $seen.Add($_) }
[System.Environment]::SetEnvironmentVariable("Path", ($deduped -join ';'), $Scope)
}