EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PowerShell Tutorial PowerShell set environment variable
Secondary Sidebar
PowerShell Tutorial
  • Variables
    • PowerShell Variables
    • PowerShell Environment Variables
    • PowerShell set environment variable
    • Hashtable in PowerShell
    • Set Variable in PowerShell
  • Basics
    • PowerShell Restart Service
    • PowerShell comment
    • PowerShell Map Network Drive
    • PowerShell Append to File
    • PowerShell print
    • What is PowerShell
    • Uses Of Powershell
    • PowerShell Empire
    • PowerShell Parameter
    • PowerShell Stop Service
    • PowerShell Versions
    • How To Install PowerShell
    • PowerShell uninstall module
    • How to Use PowerShell?
    • PowerShell Logging
    • PowerShell Tools
    • PowerShell Commands
    • PowerShell Version Command
    • PowerShell Administrator
    • PowerShell Modules
    • PowerShell Registry
    • PowerShell block Comment
    • PowerShell Verbs
    • PowerShell list
    • PowerShell add user to group
    • PowerShell Write to Console
    • Variable in PowerShell
    • PowerShell New Line
    • PowerShell prompt for input
    • PowerShell File Extension
    • Powershell Remotesigned
    • PowerShell Write to File
    • PowerShell Ping
    • PowerShell wget
    • PowerShell Global variable
    • PowerShell Get-ADGroup
    • Array in PowerShell
    • PowerShell Multidimensional Array
    • PowerShell Array of Strings
    • PowerShell? join array
    • Useful PowerShell Scripts
    • String in PowerShell
    • PowerShell Switch Statement
    • PowerShell Function Parameters
    • PowerShell vs PowerShell ISE
    • PowerShell test-connection
    • PowerShell Test-NetConnection
    • PowerShell GUI
    • PowerShell Variable in String
    • PowerShell Active Directory
  • Operators
    • PowerShell Operators
    • Comparison Operators in PowerShell
    • Logical Operators in PowerShell
    • PowerShell Boolean
    • PowerShell Like Operator
  • cmdlet
    • PowerShell Wait
    • PowerShell Match
    • cmdlets in PowerShell
    • Start PowerShell from cmd
    • Add-Content in PowerShell
    • Get Help in PowerShell
    • PowerShell Copy-Item
    • PowerShell Remove-Item
    • PowerShell Move-Item
    • Get Command in PowerShell
    • PowerShell Run Command
    • Windows PowerShell ISE
    • Windows Powershell Commands
    • WinRM PowerShell
    • PowerShell Date
    • Powershell Write-Host
    • PowerShell Get-ChildItem
    • PowerShell Sort-Object
    • PowerShell Where Object
    • PowerShell Set-Content
    • PowerShell Set-Location
    • PowerShell Invoke-Command
    • PowerShell Invoke-Webrequest
    • PowerShell Get-Location
    • PowerShell Get-Date
    • PowerShell Get-Service
    • PowerShell Test-Path
    • Powershell Module Path
    • PowerShell Out-File
    • PowerShell if File Exists
    • Powershell Copy File
    • PowerShell Delete File
    • PowerShell New-Item
    • PowerShell Rename-Item
    • PowerShell ComputerName
    • PowerShell Get-Content
    • PowerShell Get-Item
    • PowerShell Get-ADUser
    • PowerShell Grep
    • PowerShell Concatenate String
    • PowerShell Get-Process
    • PowerShell Count
    • PowerShell pause
  • Control Statements
    • If Statement in PowerShell
    • If Else in PowerShell
    • Else If in PowerShell
    • Loops in PowerShell
    • For loop in PowerShell
    • PowerShell While Loop
    • PowerShell do while
    • PowerShell Loop through Array
    • PowerShell add to array
    • PowerShell ForEach Loop
    • PowerShell Break
    • PowerShell Continue
    • Switch Case in PowerShell
    • PowerShell If-Not
    • Try-catch in PowerShell
  • Functions
    • PowerShell Functions
    • PowerShell String Functions
    • powershell nslookup
    • PowerShell here string
    • PowerShell Wildcards
    • Regex in PowerShell
    • PowerShell not like
    • PowerShell Filter
    • PowerShell Sleep
    • PowerShell where
    • PowerShell join string
    • PowerShell Exit
    • PowerShell null
    • PowerShell Dictionary
    • PowerShell Location
    • PowerShell Start-Service
    • PowerShell is not digitally signed
    • PowerShell Uptime
    • PowerShell Create Directory
    • PowerShell Trim
    • PowerShell Join-Path
    • PowerShell Execution Policy
    • PowerShell SubString
    • PowerShell Format Table
    • PowerShell Import Module
    • PowerShell ForEach Object
    • PowerShell Alias
    • PowerShell Scheduled Task
    • PowerShell Convert String to Date
    • PowerShell Split String
    • PowerShell Multiline String
    • PowerShell MultiLine Comment
    • PowerShell Rename Folder
    • PowerShell Delete Folder
    • PowerShell String Replace
    • PowerShell join
    • PowerShell xcopy
    • PowerShell Base64
    • PowerShell Tail
    • PowerShell User List
    • PowerShell remove User from group
    • PowerShell JSON Format
    • PowerShell Send Mail
    • PowerShell Convert to String
    • PowerShell Start-Process
    • PowerShell change directory
    • PowerShell Open File
    • PowerShell Batch File
    • PowerShell ZIP
    • PowerShell unzip
    • PowerShell XML
    • PowerShell XML Parsing
    • Remote PowerShell
    • PowerShell Escape Character
    • PowerShell scriptblock
    • PowerShell Executable Location
    • PowerShell Import-CSV?
    • PowerShell Export CSV
  • Interview Questions
    • PowerShell Interview Questions

Related Courses

Shell Scripting Course

All in One Data Science Courses

Data Visualization Courses

PowerShell set environment variable

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.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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
Popular Course in this category
PowerShell Training (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |   4+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Shell Scripting Training (4 Courses, 1 Project)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
Data Visualization Training (15 Courses, 5+ Projects)4.7
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more