- October 7, 2020
- Posted by: Surender Kumar
- Category: PowerShell
Manage Environment Variables using PowerShell
Table of Contents
Environment variables store the information about the operating system environment such as the operating system Path, the number of processors used by the operating system, and the location of temporary folders etc. There are basically two types of environment variables:
User Variables
These variables work for currently logged-on user. If you add a new variable under User variables, it will only work for currently logged-on user. When another user logs-on to the same computer, he/she will not be able to use the variable.
System Variables
These are global variables that work system-wide. If you add a new variable under System variables, it will work for all the users who logs on to that computer.
You can use Windows PowerShell to view and change both System and User environment variables on your Windows computer.
View and Change User Variables
To view and change the User variables, follow these steps:
- Launch Windows PowerShell console with administrator privilege.
- To view the current value of Path user variable, use the following command:
[System.Environment]::GetEnvironmentVariable("Path","User") -Split ";"
This command will list all the user variables under Path. The -Split “;” parameter causes each entry to display in new line making it easier to read and understand.
- To add the new value to Path user variable, use the following commands:
$path = [System.Environment]::GetEnvironmentVariable("Path","User") [System.Environment]::SetEnvironmentVariable("Path", $path + "C:\ADB;", "User") [System.Environment]::GetEnvironmentVariable("Path","User") -Split ";"
These commands will add a new value as “C:\ADB” to Path user variable.
You can see the newly added value marked with Red box.
If you want to add a variable temporarily, you can do so using following command. This variable will be removed when you close the PowerShell console:
$env:Path += ";C:\ADB"
View and Change System Variables
To view and change the System (Machine) variables, follow these steps:
- Launch Windows PowerShell console with administrator privilege.
- To view the current value of Path System variable, use the following command:
[System.Environment]::GetEnvironmentVariable("Path","Machine") -Split ";"
This command will list all the System variables under Path. Remember that System variables are referred to as Machine in this command.
- To add the new value to Path System variable, use the following commands:
$path = [System.Environment]::GetEnvironmentVariable("Path","Machine") [System.Environment]::SetEnvironmentVariable("Path", $path + "C:\ADB;", "Machine") [System.Environment]::GetEnvironmentVariable("Path","Machine")
These commands will add a new value as “C:\ADB” to Path System variable.
You can see the newly added value marked with Red box.
Create New System Variables
To create a completely new system variable, you can use the following command:
[Environment]::SetEnvironmentVariable("Variable_Name", "Variable_Value", "Machine")
For example, to create a Java Home system variable, you the use the following command:
[Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-17.0.2", "Machine")
Create New User Variables
To create a completely new user variable, you can use the following command:
[Environment]::SetEnvironmentVariable("Variable_Name", "Variable_Value", "User")