How to set environment variables in Windows? The post How to set environment variables in Windows? appeared first on The Automation Code.
What is an environment variable in Windows?
In Windows, an environment variable is a map/hash kind of variable with a Key-Value pair that stores the values of the variable inside the operating system so that the applications or processes can use it.
There are three types of environment variables.
Machine Level (Permanent) User Level (Permanent) Process Level (Temporary)Process level has the highest preference while the User level is the second and third is the machine level. So if the same variable is set across the three levels with different values then the Process level will be given the first preference value.
I won’t go into much detail. You can find the brief documentation on environment variables on the MS site.
You can set environment variables through GUI or programmatically. I have discussed here a few methods to set environment variables.
From Control Panel Using PowerShell Using Command Prompt Using Python Using GoLangPlease Note: In this article, we are setting Temporary and Permanent variables (all 3 levels) in the first two methods (Control Panel and PowerShell), and with CMD we will set Process variables only.
Python and Golang are covered in the next article, considering the article’s length.
Let’s discuss each method below.
a. From System Properties or Control Panel
To change environment-specific variables, open System Properties -> Advanced -> Environment Variables.
You can update/add here both user-level and machine-level variables. To set the process-specific variables use the command line.
b. Using PowerShell
b.1 . Process Level
You can set environment variables at the process level but that would be temporary for that session. When you close the session the environment variable will be disappeared.
To set the environment variable at the process level, use the below command syntax.
$env:variableName = "Variable Value"Example,
PS C:\> $env:azureRG = "TestRG"To get the variable value, type the variable name
$env:azureRGYou can also use, New-Item or Set-Item to create or change the environment variable value. (Please note: Set-Item value creates a new environment variable that doesn’t exist).
New-Item Env:\azureSub -Value "TestSub"The above command will create a new environment variable named “azureSub” with the Value “TestSub“.
Set-Item Env:\azureSub -Value "TestSub2"Set-Item command will modify the value to “TestSub2“.
To set the environment variable persistently, you need to store it at either user-level (only available to that user profile) or machine-level (available for all user profiles).
b.2. Machine Level
Below is the code syntax for machine-level variables.
[Environment]::SetEnvironmentVariable('variable Name', 'variable Value', 'Machine')For example,
[Environment]::SetEnvironmentVariable('azureSub', 'TestSub', 'Machine')You can check from the GUI as well.
To remove the variable, just set its value to NULL.
[Environment]::SetEnvironmentVariable('azureSub', '', 'Machine')b.3. User-Level
To set the environment variable at the current user profile level, change the last value from Machine to User.
[Environment]::SetEnvironmentVariable('azureSub', 'TestSub', 'User')To remove a variable value,
[Environment]::SetEnvironmentVariable('azureSub', '', 'User')c. using Command Prompt
To set the process level environment variable (session variable), you need to use the below command.
set variableName=VariableValueFor example,
set azSub=TestSubTo display variable values,
set azsubTo remove a variable,
set azsub=Setting up environment variables using Python and GoLang will be covered in the next article.
The post How to set environment variables in Windows? appeared first on The Automation Code.