Set Environment Variable in PowerShell

Set environment variable in PowerShell

Windows Environmental Variables and Their Scope

There are three different scopes for environment variables in Windows.

  • Set the Session Environment Variable
  • Set an Environment Variable for a User Using PowerShell
  • Set the Machine Environment Variable

Before moving towards solutions, we must know the environment variables and why we use them.

System administrators can access a wealth of information about the Windows operating system using the environment variables. Default environment variables can be read and updated, and new environment variables, separated into user scope and system scope, can be created.

PowerShell makes creating Windows environment variables and reading and setting new environment variables much easier than the graphical user interface (GUI). An environment variable stores a value that a Windows application can access.

PowerShell saves information about the Windows operating system in the Environmental variable. For instance, the number of processors, system drives, user profile path, etc. Environment variables in PowerShell are stored in the Env:"drive", which is accessible via the PowerShell environment provider, a subsystem of PowerShell. This is a virtual file system, not a physical drive.

Setting a Session Environment Variable

Using $Env: Path

Use $Env: Path to set the environment variable for the session. Now, its scope will be for the current session and process scope.

In the above command, the $Env:Path temporarily sets the environment variable for the session, but it does not retain the variable value after closing the PowerShell windows. So, for example, this will set the Hugobin environment variable to C:\Hugo\bin for the current session.

To make the environment variable persistent across sessions, you can use the Export-ModuleMember cmdlet to export the environment variable from the PowerShell profile script. Here’s an example:

It will set the Hugobin environment variable to C:\Hugo\bin and make it available to all sessions. In PowerShell, we can use the Get-ChildItem cmdlet to print all the environment variables as follows:

We can use += and write the variable value as follows to append a new value to an environment variable:

The above command will append the value D:\go\bin to an environment variable path and will be available only for this session.

Using the Set-Item cmdlet

Set-Item cmdlet can also be used to set or change any environment variable. You can also create environment variable using this method.

The cmdlet above will set the environment variable Hugobin to C:\Hugo\bin

Setting an Environment Variable for a Current User

Use the SetEnvironmentVariable() method of the [System.Environment] class to set an environment variable for a user who is currently using PowerShell.

For this section, we used the SetEnvironmentVariable() method, which belongs to the [System.Environment] class, to set an environment variable for that user only who is currently using PowerShell.

Here, the first argument to the SetEnvironmentVariable() method is the name of the environment variable, the second argument is the value to set for the environment variable, and the third argument is the EnvironmentVariableTarget that specifies the scope of the environment variable.

In this case, the EnvironmentVariableTarget is set to User, which means that the environment variable will be set for the current user only. We can also use the SetEnvironmentVariable() method to set the Machine environment variable; let’s see how we can do that.

Setting the Machine Environment Variable

Use the SetEnvironmentVariable() method of the [System.Environment] class to set an environment variable for the machine.

This command is similar to the previous one but sets an environment variable for the Machine using the SetEnvironmentVariable() method of the [System.Environment] class. It creates an environment for a single machine by using EnvironmentVariableTarget as a Machine, which means that the environment variable will be set for all users on the machine.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *