Powershell

Get Environment Variables In Powershell

An environment variable is an object which contains a value that is used by various software programs. These variables store the information related to the operating system’s environment and processors used by the operating system. Environment variables can be used for different configurations, such as pipeline, system, and software configurations.

One can add new environment variables using the Graphical User Interface of windows which is very common. However, in this guide, we will provide an enriched detail of handling environment variables using Windows PowerShell.

How to Check the Existing Environment Variables using PowerShell?

At first, you must start your Windows PowerShell. Go to “search” in the taskbar and type “PowerShell“. Right-click on the found field and click on “Run as Administrator“.

After getting the PowerShell with administrator privileges, write the following command to get all environment variables stored in the operating system.

> Get-ChildItem Env:

Once you execute the command, it will list down all the environment variables available:

How to Add New Environment Variables using PowerShell?

After getting the list of environments variables, you can add new environment variables to the existing list. If the variable already exists, one can append the value to the current variables.

As you can see, there is no environment variable with the name “AAA_ENV_VAR“: You have to use assignment operator “=” to assign a new value to the environment variable you are going to make. Here, we are assigning value name ‘TESTVARIABLE‘ to the new variable; execute the command given below to add a new environment variable:

> $env:AAA_ENV_VAR = 'TESTVARIABLE'

After adding, check the list of environment variables. You will get this newly generated variable. To check the list of the existing environment variables; run the following command in PowerShell:

> Get-ChildItem Env:

How to Append Value to Existing Environment Variable using PowerShell?

If you want to append to the current value of the environment variable, you can do it with the help of the command given below. Make sure to write the correct name of the variable. You can add a colon or semicolon to separate the current and appended value of the variable.

> $env:AAA_ENV_VAR += ': CHILDTESTVARIABLE'

You can verify the change by using the following command:

> dir env:

How to Replace the Existing Value of Environment Variables?

If you want to replace the value of an existing variable, you do not need to add a new variable. However, the command is the same as adding a new variable. Still, if the variable already exists with the same name, it will replace the existing value with the new value you have provided. The command given below will help replace the value of existing value:

> $env:AAA_ENV_VAR = 'NewValue'

The above command will add ‘NewValue’ to the existing variable “AAA_ENV_VAR“. You can view the list of environment variables to verify the change:

> dir env:

How to Create a Backup File of Environment Variables in Windows?

As the environment variables keep sensitive information related to the operating system, adding new variables or changing the values of existing variables can be dangerous. So, it is good practice to create the backup of the existing variables.

Copy and paste the following command to get the backup of the environment variables in the “C” drive or wherever you want. You have the choice to select the format of the output file, here we are creating the backup in the “.txt” file:

> Get-ChildItem env: | Select Key,Value | Export-Csv C:\Temp\backupenvvariables.txt -NoTypeInformation

Once the command is executed successfully, look inside the directory “C:\Temp” to verify that the backup file has been created or not. To do this using PowerShell, run the following command:

> Get-ChildItem C:\Temp

The output is shown below, and you can check that a text file with the name “backupenvvariables” is created in the selected directory:

How to Remove Environment Variable using PowerShell?

Environment variables set by your machine have some vital tasks to perform, so deleting them can halt the performance of your operating system. However, some variables are created by the users that have nothing to do with your device. So if you want to remove any variable, you have to execute the following command with the variable name:

> Remove-Item Env:\AAA_ENV_VAR

This PowerShell cmdlet will remove “AAA_ENV_VAR” from the list of existing environment variables.

Conclusion:

Environment variable keeps various kinds of information which refers to multiple functions of the OS. You can get the information needed by checking the list of variables that stores the type of information.

In this guide, we have provided the different operations that you can perform on environment variables. Environment variables contain different types of information related to your operating system. If you want to add new variables into the environment variable directory, do the changes carefully, because if you manipulate any built-in variable, the result may force your operating system to act up, or you may not get the required value on calling the variable.

About the author

Adnan Shabbir