PowerShell: Environment variables
October 4, 2025
tipsHere are three ways to add an environment variable while using PowerShell on Windows.
Temporary Variable
Set a variable for the current session.
$env:MyVariable = "MyValue"
For example, you can temporary set the Hugging Face token like to
$env:HF_TOKEN = "your-token-here"
User Persisted
Persist across sessions but only for the current user.
[Environment]::SetEnvironmentVariable("MyVariable", "MyValue", "User")
"User" ensures it applies only to the current user.
System-Wide
Persist across sessions and apply to all users on the system.
[Environment]::SetEnvironmentVariable("MyVariable", "MyValue", "Machine")
"Machine" ensures it applies system-wide. Administrative privileges needed to set system-wide variables.
Check if a variable is set
To check if the variable is set, use:
Get-ChildItem Env:MyVariable
Or simply:
$env:MyVariable
Print all environment variables
To print all currently available environment variables
Get-ChildItem Env: