In Windows, the operating system and various apps use environment variables to store system information, such as paths to system folders, configuration settings, and information about the system environment. Users can use the Control Panel graphical interface (SystemPropertiesAdvanced.exe > Environment Variables) or the PowerShell console to add, edit, or delete environment variables and their values.

Add, edit, or delete environment variables and their values with PowerShell
In PowerShell, system environment variables can be accessed through a virtual drive called “Env:”.
Get-PSDrive -PSProvider "Environment"

To list all Windows environment variables, simply display the contents of this drive using the dir command:
dir Env:
or
Get-ChildItem ENV:
Environment variables are stored in a flat namespace, where each entry is represented as a [variable]:[value] pair.
To get the value of a specific variable, specify its name after a colon:
$Env:windir
Or:
Get-ChildItem -Path Env:windir

The most commonly used environment variables in PowerShell scripts are PATH, TEMP, USERPROFILE, COMPUTERNAME, OS, and HOMEPATH.
Some environment variables can contain multiple values separated by semicolons. The following command displays a list of values in a convenient format:
($Env:Path) -split ';'

Note that $Env:Path shows the effective PATH variable for the current process. To view the User and Machine PATH values separately, use:
[Environment]::GetEnvironmentVariable("Path","Machine")
[Environment]::GetEnvironmentVariable("Path","User")
With PowerShell, you can change the values of environment variables. For example, to add a new value to the PATH environment variable, run the command:
$Env:Path += ";C:\Git\"
Note that the command mentioned above modifies only the PATH variable of the current PowerShell process.
However, the new value of the environment variable will only be available within the current PowerShell session. The new value will reset once you close the PowerShell console or reboot the computer.
Environment variables scopes
The point is that environment variables have three scopes:
- Process scope โ environment variables available only to the current PowerShell process. For example:
$Env:MyVariable = “Test”
The variable will be available only in the current session and will disappear when the process exits. - Machine scope (system-wide environment variables) โ computer environment variables (stored in the registry under HKEY_LOCAL_MACHINE\ SYSTEM\ CurrentControlSet\ Control\ Session Manager\ Environment)
- User scope โ current user environment variables (registry key HKEY_CURRENT_USER\Environment).
Make environment variable’s value permanent
Only environment variables set at the user or machine level are persistent. In order to make an environment variable persistent, you must update it in the User or Machine scope. This can be done by modifying the corresponding registry values/using the .NET SetEnvironmentVariable() method.
Although environment variables can be modified directly in the registry, we recommend using the .NET Environment class as it’s generally a safer and more portable method. It also notifies Windows about the environment variable change, allowing newly launched apps to pick up the updated values more reliably than direct registry edits.
You can add a new value to the machine-level PATH variable using the following example:
$currentPath = [Environment]::GetEnvironmentVariable(
"Path",
"Machine"
)
$newPath = $currentPath + ";C:\Git"
[Environment]::SetEnvironmentVariable(
"Path",
$newPath,
"Machine"
)
Although the .NET Environment class is the recommended method, you can also modify the PATH variable directly in the registry:
$envpath = 'HKLM:\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\' $cur_value = Get-ItemPropertyValue -Path $envpath -Name 'Path' $new_value = $cur_value + ';C:\Git\' Set-ItemProperty -Path $envpath -Name 'Path' -Value $new_value
Existing processes do not automatically receive updated environment variables. You may need to start a new PowerShell session, sign out and back in, or restart apps to see the changes.
Before appending a new path, you need to check if it already exists to avoid duplicate entries. After completion, check if the new value has been added to the registry.
Note. Direct registry modification can change how expandable variables (such as %SystemRoot%) are stored in the PATH value. Keep in mind that the .NET Environment.SetEnvironmentVariable() method is generally safer because it preserves environment variable semantics and avoids registry value type issues.

Create a New Environment Variable Using the .NET Environment Class
You can create a new environment variable by using the built-in System.Environment class in .NET. This command adds a new global system environment variable named SITE:
$var_name = 'SITE' $var_value = 'CA' [System.Environment]::SetEnvironmentVariable($var_name,$var_value,[System.EnvironmentVariableTarget]::Machine)

To add an environment variable to the user scope, replace Machine with User:
[System.Environment]::SetEnvironmentVariable($var_name,$var_value,[System.EnvironmentVariableTarget]::User)
Use the SetEnvironmentVariable() method to remove an environment variable:
[Environment]::SetEnvironmentVariable("SITE", $null, "Machine") What is the difference between Process, User, and Machine environment variables?
Windows supports three environment variable scopes:
- Process โ available only to the current process or PowerShell session.
- User โ available only to the current user account.
- Machine โ available system-wide for all users.
User and Machine variables are persistent, while Process variables are temporary.
Does modifying $Env:Path permanently change the PATH variable?
No. Changes made through $Env:Path affect only the current PowerShell session. The original value is restored when the session ends or the computer restarts.
What is the recommended way to make an environment variable change permanent?
Use the .NET Environment.SetEnvironmentVariable() method. It is generally safer than directly editing the registry and automatically notifies Windows about the change so newly launched applications can pick up updated values.
Why is editing the PATH variable directly in the registry not recommended?
Direct registry edits can change how expandable variables such as %SystemRoot% are stored. This may convert expandable values into plain text and alter the registry value type. The .NET method preserves environment variable semantics and avoids these issues.
Do running applications automatically receive updated environment variables?
No. Existing processes continue using the environment variables they received at startup. You may need to restart applications, open a new PowerShell session, or sign out and back in to see changes.
