Introduction to PowerShell set environment variable.
PowerShell Environmental variable stores the Windows Operating System information like the Operating System path where it installed, System Drive, User profile path, number of Processors, etc. They can be used by another program or the child parent processes because they are easier to work. Setting the environment variable using the PowerShell is the easy way, and for that, we can use either the simple variable appending method, the .Net method, or the Set-Item cmdlet.
Below are the methods to set the environment variable.
By adding/appending in a variable
$Env:<variable-name> = "<new-value>"
The above command syntax creates a new environment variable with the value specified. To append the variable, use the (+) symbol.
$Env:<variable-name> += "<new-value>"
Using Set-Item cmdlet
Below command, syntax sets the existing variable name. We can also create a new variable without providing the existing variable name inside the Value parameter.
Set-Item -Path Env:<variable-name> -Value ($Env:<variable-name> + ";<new-value>")
Using the Set-Content Method
In this method, we can use this cmdlet directly by providing a path of the environment variable or by setting the location first to the environment path and then changing the variable directly.
Set-Content -Path env:<variable-name> -Value "ValueName"
Using the .Net class method
The above command creates, modifies, or deletes the environment variable stored in the current process.
The above command creates, modifies, or deletes the environment variable stored in the current process or the Windows operating system registry key.
How does the Environment variables work in Windows?
Environment variables are stored in the variable called Env, and it can be accessed using Get-ChildItem (alias: dir) to get all the values stored in the variable. For example,
If you notice in the above command, the environment variable ends with the (:) symbol, and it indicates the drive symbol but Env: is the specific drive, and you can get all the drives associated in the current session using the Get-PSDrive command.
You can notice that Env is present in the Name, and so we can access it using the Drive symbol with Env: value similar to registry values. To check all the drives attached in the current session, use the Get-PSProvider command.
When we set the environmental variables, we need to use the Env: symbol to access that particular variable with Syntax’s above-described methods.
To set the environment variable persistently on Windows OS, you need to use Computer Properties -> Advanced System Settings -> Advanced Tab -> Environment Variable. You can add or edit the existing environment variable in the user or Machine scope, and then windows write this variable in the registry to make a permanent change, and it remains even after windows restarts.
Similarly, you can set the environment variable in the PowerShell user profile to make it persistent.
Examples of PowerShell set environment variable.
Here are the following examples mention below
Example #1 – Append variable method.
In this method, we append the existing variable to the environment variable list. For example,
We need to add another path to the PSModulePath environment variable. The existing paths are as shown below.
We can use the split method to get the paths properly in the console,
(dir Env:\PSModulePath).value -split ';'
To add the new path, we simply append the variable.
$env:PSModulePath += ";c:\temp"
When you check the variable of the PSModulePath, it should show C:\temp.
$env:PSModulePath -split ';'
This is just a temporary add; when you restart the computer, the above-added path will disappear.
To set the new variable, you can directly use the name of the variable and the new value, as shown in the syntax section.
$env:AZSubscription = 'Visual Studio Subscription'
The above command will add the new environment variable called AZSubscription and set its value to ‘Visual Studio Subscription’.
Example #2 – Using Set-Item cmdlet.
We can also use the Set-Item command to set the environment variable value. For example, to append the value to the above AZSubscription environment variable, we can use the below command.
Set-Item -Path Env:\AZSubscription -Value ($env:AZSubscription + ";AutomationLab")
To create a new environment value using Set-Item, we can directly provide the new path and the new value.
Set-Item -Path Env:\AZResourceGroup -Value 'AutomationRG'
Example #3 – Set-Content cmdlet
We can use the below command to append the path in the environment variable using the Set-Content cmdlet. Here, we are providing the existing environment variable to append in the Value parameter.
Set-Content -Path Env:\AZResourceGroup -Value ($Env:AZResourceGroup + ";AutomationRG2")
Similarly, to add the new environment variable using the Set-Content cmdlet, we can directly use the new variable name in the -Path parameter and value for it in the -Value parameter.
Set-Content -Path Env:\AZResourceGroup -Value 'NewAutomationRG'
If we use the existing variable without appending the value in the -Value parameter, it will wipe out old values and set the new value, as shown in the above example.
Example #4 – Using the .Net class method
In this method, we are using PowerShell .Net class called [System.Environment] to set the value or to create a new value for the Environment variable.
First, we will set the new value for the environment.
We can also use alias [Environmet] instead of [System.Environment]. To append the new value using this method, we will use the below syntax, which was mentioned in the Syntax section.
$path = $env:AZResourceGroup + ";AutomationTest2"
Conclusion – PowerShell set environment variable
Environment variables are very powerful in terms of retrieving details about the operating system, hardware, and application. Some programming languages like Java, .NET use the environment variable so that the application directly retrieve the data and work with them. They can also create an environment variable for ease of use.
This is a guide to PowerShell set environment variable. Here we discuss How does the Environment variables work in Windows along with the Examples. You may also have a look at the following articles to learn more –