Using $env:Path variable in PowerShell to set environment variable for the session or temporary. Use
[System. Environment] method in PowerShell to set environment variable permanently.
An environment variable contains a value that can be used by an application in the Windows operating system. It contains name-value pair.
In this article, we will discuss in PowerShell how to set an environment variable for a user session, append the value to the variable, and set the environment variable persistent.
PowerShell Set Environment Variable for Session
Use $Env: Path to create an environment variable for the session and its scope will be for the current session and process scope.
$Env:GoHugo = "C:\Hugo\bin\"
$Env:GoHugo creates an environment variable path and sets the value to it using = (assignment operator).
$Env: Path variable temporarily sets the environment variable for the session and after closing the PowerShell windows, it doesn’t retain the variable value.
Using the Get-ChildItem in PowerShell, you can get the environment variable as follows:
The output of the above PowerShell script get an environment variable for the session and print environment variable path as:
Close the PowerShell window and open it again.
Run the Get-ChildItem command again to get the environment variable created in the above step.
It will give an error message as the environment variable remains available for that process scope only.
PS C:\> Get-ChildItem Env:GoHugo Get-ChildItem : Cannot find path 'GoHugo' because it does not exist. At line:1 char:1 + Get-ChildItem Env:GoHugo + ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (GoHugo:String) [Get-ChildItem], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetChildItemCommand PS C:\>
Use Powershell to Set Environment Variables for a user
To create an environment variable permanently for a user account, use [System.Environment] method to set an environment variable for a user.
In the above PowerShell script, [System.Environment] uses the SetEnvironmentVariable method to set an environment variable permanently for a user and its value will persist permanently.
[System. Environment] uses EnvironmentVariableTarget as a user to set up an environment for user accounts only.
Set Environment Variable for Machine
Using the [System. Environment] method, you can set an environment variable for the machine. It uses EnvironmentVariableTarget a Machine to set up an environment for a machine only.
Open the PowerShell app with run as administrator as setting up an environment variable for the machine requires changes in a registry and may throw an error if the user account doesn’t have access to the requested registry.
The output of the above script to set environment variable path using the command is:
Cool Tip: How to run PowerShell as a System account!
Append new value to Environment Variable
To append a new value to an environment variable, use += and specify the variable value.
Let’s consider an example to append a new value to the environment variable using $Env: Path as follows
$Env:Path += "C:\go\bin\"
It will append value C:\go\bin to the environment variable Path and will be available for the current session only.
Use dir env: to print the environment variable name and their path.
I hope the above article to set an environment variable for a user and machine is helpful to you.
Use $Env:Path to set an environment variable for the current session only and process scope. After closing the PowerShell terminal, it doesn’t store the value.
Use the .Net framework library [System.Environment] method in PowerShell to set an environment variable permanently for the user and machine for permanent use.
You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.