EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell set environment variable
 

PowerShell set environment variable

Updated March 6, 2023

PowerShell set environment variable

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

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

[Environment]::SetEnvironmentVariable(String, String)

The above command creates, modifies, or deletes the environment variable stored in the current process.

[Environment]::SetEnvironmentVariable(String, String,EnvironmentVariableTarget)

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,

Get-ChildItem Env:

Output:

PowerShell set environment variable output 1

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.

value

Get-PSDrive

PowerShell set environment variable output 2

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.

Get-PSProvider

Output:

PowerShell set environment variable output 3

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.

dir Env:\PSModulePath

Output:

PowerShell set environment variable output 4

We can use the split method to get the paths properly in the console,

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

Output:

PowerShell set environment variable output 5

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 ';'

Output:

PowerShell set environment variable output 6

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'

Output:

output 7

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")

Output:

output 8

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'

Output:

output 9

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")

Output:

output 10

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'

Output:

output 11

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.

[System.Environment]::SetEnvironmentVariable('AZResourceGroup','Automationtest')

Output:

output 12

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.

[Environment]::SetEnvironmentVariable(String, String,EnvironmentVariableTarget)

$path = $env:AZResourceGroup + ";AutomationTest2"
[Environment]::SetEnvironmentVariable('AZresourceGroup',$path)

Output:

output 13

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.

Recommended Articles

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 –

  1. PowerShell Sleep
  2. Windows PowerShell ISE
  3. PowerShell Escape Character
  4. PowerShell SubString

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW