Recently, someone from a PowerShell community asked me about setting environment variables in PowerShell. PowerShell provides different cmdlets to do this. Let me share with you here how to set and get environment variables in PowerShell.
To set an environment variable in PowerShell, you can simply use the $env variable followed by the variable name and assign it a value. For example, to set the variable “MyVariable” to “Hello, World!”, you would use the command $env:MyVariable = "Hello, World!". This method sets the variable temporarily, making it available only in the current PowerShell session.
Set Environment Variable in PowerShell
Environment variables are dynamic values that affect the way processes and applications run on a computer. They can store information such as file paths, system configurations, and user preferences. In Windows, environment variables can be user-specific or system-wide.
Now, let me show you how to set environment variables in PowerShell.
PowerShell offers multiple ways to set environment variables. Let’s look at a few common approaches.
1. Using the $env Variable
The simplest way to set an environment variable in PowerShell is by using the $env variable followed by the variable name. Here’s an example:
$env:MyVariable = "Hello, World!"In this case, we set the value of the environment variable named “MyVariable” to “Hello, World!”. This method sets the variable temporarily, meaning it will only be available in the current PowerShell session.
You can see the script after I executed it.

Check out PowerShell Print Variable
2. Using the Set-Item Cmdlet
Another way to set an environment variable in PowerShell is by using the Set-Item cmdlet. This cmdlet allows you to modify the value of an existing variable or create a new one. Here’s an example:
Set-Item -Path Env:\MyVariable -Value "Hello, PowerShell!"In this case, we use the Set-Item cmdlet to set the value of “MyVariable” to “Hello, PowerShell!”. The -Path parameter specifies the location of the environment variable, and the -Value parameter sets its value.
3. Set Persistent Environment Variables
If you want to set an environment variable that persists across PowerShell sessions and system restarts, you can use the [Environment]::SetEnvironmentVariable() method. Here’s an example:
[Environment]::SetEnvironmentVariable("MyVariable", "Persistent Value", "User")In this case, we set the value of “MyVariable” to “Persistent Value” for the current user. The third parameter specifies the scope of the variable, which can be “User” or “Machine”.
Check out Get Computer Name in PowerShell
PowerShell: set environment variable for session
Temporary environment variables are set for the duration of the PowerShell session. Once the session is closed, these variables are discarded.
To set a temporary environment variable, use the $env: prefix followed by the variable name and the assignment operator (=). Here’s an example:
$env:MyTempVar = "TemporaryValue"This command sets an environment variable named MyTempVar with the value TemporaryValue.
PowerShell: set environment variable permanently
Permanent environment variables persist across PowerShell sessions and system reboots. To set a permanent environment variable, you need to use the [System.Environment]::SetEnvironmentVariable method.
To set a user-specific environment variable, use the following command:
[System.Environment]::SetEnvironmentVariable("MyUserVar", "UserValue", "User")This command sets an environment variable named MyUserVar with the value UserValue for the current user.
Check out Set Variables in PowerShell
PowerShell: set environment variable machine level
To set a system-wide environment variable, use the following command. Note that you need administrative privileges to set system-wide variables:
[System.Environment]::SetEnvironmentVariable("MySystemVar", "SystemValue", "Machine")This command sets an environment variable named MySystemVar with the value SystemValue for the entire system.
Get Environment Variable in PowerShell
Now that we know how to set environment variables let’s explore how to retrieve their values in PowerShell.
1. Using the $env Variable
To get the value of an environment variable in PowerShell, you can simply use the $env variable followed by the variable name. Here’s an example:
$value = $env:MyVariable
Write-Host $valueIn this case, we retrieve the value of “MyVariable” and store it in the $value variable. We then use Write-Host to display the value.
I executed the above PowerShell script, and you can see the output in the screenshot below:

2. Using the Get-ChildItem Cmdlet
Another way to get environment variables in PowerShell is by using the Get-ChildItem cmdlet with the Env: drive. Here’s an example:
Get-ChildItem Env:This command retrieves all the environment variables defined in the current session and displays their names and values.
3. Using [System.Environment]
For a .NET approach, you can use:
[System.Environment]::GetEnvironmentVariable("MY_VARIABLE", "User")This retrieves the value of a specific environment variable for the current user. Replace “User” with “Machine” for system-wide variables.
Conclusion
In this tutorial, I explained how to set and get environment variables in PowerShell. We covered different methods, including using the $env variable, the Set-Item cmdlet, and the [Environment]::SetEnvironmentVariable() method. We also discussed how to retrieve the values of environment variables in PowerShell using the $env variable and the Get-ChildItem cmdlet.
You may also like:
- Restart a Computer Using PowerShell
- How to Change Directory in PowerShell?
- Remove Environment Variables in PowerShell
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.