Monday, 30 November 2026

How to set environment variable using powershell ?

To set an environment variable in PowerShell, use $env:VAR_NAME = "value" for the current session or [System.Environment]::SetEnvironmentVariable() to save it permanently. 
1. Temporary (Current Session Only)
Variables set this way apply only to your current terminal window and any programs launched from it. They disappear when you close PowerShell.
powershell
# Set a new variable or overwrite an existing one
$env:MY_VAR = "my_value"

# Append to an existing variable (like PATH)
$env:PATH += ";C:\new\folder"

2. Permanent (Across Restarts)
On Windows, use the .NET [System.Environment] class to save variables to the registry so they persist across sessions and reboots.
  • For your user account only:
powershell
[System.Environment]::SetEnvironmentVariable("MY_VAR", "my_value", "User")
  • System-wide for all users (requires running PowerShell as Administrator:
powershell
[System.Environment]::SetEnvironmentVariable("MY_VAR", "my_value", "Machine")

No comments:

Post a Comment