Home » PowerShell » Set Environment Variable using PowerShell

Set Environment Variable using PowerShell

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 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 environment variable persistent.

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 create 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:

Get-ChildItem Env:GoHugo

The output of the above script to set an environment variable for the session and get it is:

PowerShell set environment variable for session
PowerShell set environment variable for the session

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 Variable for a user

To create an environment variable that will be available permanently for a user account, use [System.Environment] method to set an environment variable for a user.

[System.Environment]::SetEnvironmentVariable('GoHugo','C:\Hugo\bin',[System.EnvironmentVariableTarget]::User)

In the above PowerShell script, [System.Environment] uses the SetEnvironmentVariable method to set an environment variable for a user and its value will persist permanently.

[System. Environment] uses EnvironmentVariableTarget as a user to set up an environment for user account only.

Set an environment variable for the user
Set an environment variable for the user

Set Environment Variable for Machine

Using the [System. Environment] method, you can set an environment variable for the machine. It uses EnvironmentVariableTarget as a Machine to set up an environment for a machine only.

[System.Environment]::SetEnvironmentVariable('GoHugo','C:\Hugo\bin',[System.EnvironmentVariableTarget]::Machine)

Open the PowerShell app with run as administrator as setting up an environment variable for 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 command is:

Set environment variable for machine
Set environment variable for machine

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.

Conclusion

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.

Using the .Net framework library [System.Environment] method to set an environment variable for user and machine for permanent use.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.

Leave a Comment