Powershell

How to Set Environment Variable in PowerShell

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

Creating Windows environment variables, reading and setting new environment variables is much easier in PowerShell than using the Graphical User Interface (GUI). You may need to utilize the Control Panel to edit system variables while using the GUI method. Once you understand the trick, setting environment variables in PowerShell becomes simple.

What are Environment Variables in Windows?

Environment variables hold information related to the Window and its applications. Simple text editors like Notepad, Graphical applications like Windows Explorer, command prompt, and PowerShell can access the environment variables. The information about the Windows Operating System is saved in the Environmental variable in PowerShell. For example, the number of processors, system drives, operating system path where OS was installed, user profile path, etc.

Unlike other variables in PowerShell, child processes inherit environment variables such as the local background sessions and module members’ tasks. As a result, environment variables are ideal for storing values required by both parent and child processes. Also, using environment variables in your PowerShell scripts allows you to avoid hard-coding user or computer names, file paths, and much more.

Scope of Environmental Variables in Windows

In Windows, there exist three different scopes for environment variables.

  • User: In the scope of the user, environment variables are linked to the user who is currently executing the process.
  • Machine: In the machine scope, environment variables are connected to the current Windows instance. Any user account can view these variables, but they can only be modified, deleted, or set by someone having elevated access. With the same name, user variables overwrite the machine scope variables.
  • Process: In the process scope, environmental variables consist of variables created dynamically by Windows with other machine and user scope variables.

Checking Environment Variable in PowerShell

The PowerShell Environment provider allows you to add, retrieve, change, and delete environment variables and their values in PowerShell. Env is the name of a drive created by the Environment Provider. In PowerShell, this drive uncovers all environment variables on a Windows system. Because Env is included in the name, we may use the Drive symbol with the Env: value, which is equivalent to registry values. You can utilize the “Get-ChildItem” cmdlet to see a list of all user environment variables on your system as follows:

> Get-ChildItem Env:

With the Get-PSDrive command, you can retrieve a list of all the drives associated with the current session. So, execute this command to confirm the existence of the newly created “Env” drive:

> Get-PSDrive

Setting Environment Variable in PowerShell

In the first method, we will append the existing variable to the environment variable list. To do so, add the path or a value to the PSModulePath environment variable. Utilize the below-given command to check the list of existing environmental variable paths:

> dir Env:\PSModulePath

Here, all paths are combined with the “;” symbol, which can confuse you.

Use the “-split ‘:’” option for viewing each path separately.

> (dir Env:\PSModulePath).value -split ';'

Follow the below-given command syntax to add the environmental variable path in the existing list. Here, “$env” is the built-in variable used to access the environmental variable in PowerShell.

$env:[variable_name] += ";[value]"

We will append the “c:\temp” in the list using the “+=” operator. You can specify the path according to your file system. After that, view the “PSModulePath” list to know if the path is added to the list or not:

> $env:PSModulePath += ";c:\temp"
> $env:PSModulePath -split ';'

The assignment operator “=” is needed to set the environmental variable in PowerShell. You can append the value to an existing environment variable using the “+=” operator; otherwise, a new environment variable will be generated.

For instance, our system does not have an environment variable named AZURE_RESOURCE_GROUP. So, we will test out the creation of this environmental variable:

> $env:AZURE_RESOURCE_GROUP = 'MyTestResourceGroup'
> dir env:

The output declares that “AZURE_RESOURCE_GROUP” is added to the list of the environmental variable with the value “MyTestResourceGroup.”

Utilize the below-given command to verify its existence on your system:

> $env:AZURE_RESOURCE_GROUP

You can also change the value of your environment variable in the following way:

> $env:AZURE_RESOURCE_GROUP = 'NewGroup'

Again, check the environmental variable.

> $env:AZURE_RESOURCE_GROUP

The output shows that the value of the environment variable is changed!

Conclusion

Environment variables can collect information about system execution or store data between reboots and sessions. You can now easily manage environment variables using PowerShell in various ways, whether you want to check the built-in Windows operating system environment variables or create a new one. In this article, we practically showed how to set environment variables in PowerShell. Feel free to try them out!

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.