Last Updated on August 7, 2025 by Arnav Sharma
Setting and managing environment variables in PowerShell is an essential skill, as these variables are used by the operating system and applications to determine various settings and behaviors. In PowerShell, you can create, modify, and remove environment variables, which can be scoped to the current process, the user, or the entire system. Here’s a comprehensive guide on how to handle environment variables in PowerShell.
Creating an Environment Variable
Set a New Environment Variable for the Current PowerShell Session:
To set an environment variable that only lasts for the duration of your current PowerShell session, use the following syntax: $env:VARIABLE_NAME = "value"
- For example, to create a new environment variable named
TEMPVARwith the valuetempValue:$env:TEMPVAR = "tempValue"
2. Create a System-Wide Environment Variable:
- To set the environment variable persistently, affecting the entire system (requires administrative rights):
[System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", [System.EnvironmentVariableTarget]::Machine)- For instance, creating a machine-scoped variable
MYVAR:[System.Environment]::SetEnvironmentVariable("MYVAR", "myValue", [System.EnvironmentVariableTarget]::Machine)
- For instance, creating a machine-scoped variable
3. Create an Environment Variable for the Current User:
- To set an environment variable that persists for your user profile:
[System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", "value", [System.EnvironmentVariableTarget]::User)
Reading Environment Variables
- Get the Value of an Environment Variable:
- You can retrieve the value of an environment variable using:
Get-ChildItem Env:VARIABLE_NAME
- Or simply by:
echo $env:VARIABLE_NAME
- Or simply by:
- You can retrieve the value of an environment variable using:
Removing an Environment Variable
Remove an Environment Variable from the Current Session:
- To delete an environment variable in the current PowerShell session:
Remove-Item Env:VARIABLE_NAME
- For example, removing
TEMPVAR:Remove-Item Env:TEMPVAR
- For example, removing
- To delete an environment variable in the current PowerShell session:
2. Delete a Persistent Environment Variable:
- To permanently remove a user or system variable:
[System.Environment]::SetEnvironmentVariable("VARIABLE_NAME", $null, [System.EnvironmentVariableTarget]::Scope)
- For example, to remove
MYVARfrom the machine scope:[System.Environment]::SetEnvironmentVariable("MYVAR", $null, [System.EnvironmentVariableTarget]::Machine)
- For example, to remove
Examples and Use Cases
Creating a New Path Variable:
- To add a new directory to the PATH environment variable in the current session:
$env:PATH += ";C:MyNewPath"
- To add a new directory to the PATH environment variable in the current session:
Using Environment Variables to Store Configuration:
- Set an
azure_resource_groupenvironment variable for use in a script:$env:azure_resource_group = "MyResourceGroup"
- Set an
Changing the PowerShell Profile Path:
- Modify the
PSModulePathenvironment variable to include a custom module directory:$env:PSModulePath += ";C:MyCustomModules"
- Modify the
Notes on Environment Variables in PowerShell
- Scopes: Environment variables in PowerShell can be set in three scopes:
Process,User, andMachine. Process-scoped variables exist only in the current PowerShell session, while user and machine-scoped variables persist outside of the current process. - Persistence: Changes to user or machine-scoped variables may require a restart of the PowerShell session or the system to take effect.
- Environment Provider: PowerShell provides the Environment Provider which allows you to work with environment variables using paths, much like file system paths.
- Importance: Environment variables are essential as they can influence the behavior of processes or the PowerShell session itself. They are commonly used to store paths, configuration settings, and other important data.
I help organisations secure their cloud infrastructure and stay ahead of evolving cyber threats. Microsoft MVP and Certified Trainer, author of Mastering Azure Security, and founder of arnav.au — a platform for practical Cloud, Cybersecurity, DevOps and AI content.
Frequently Asked Questions
Setting an environment variable for the current session using $env:VARIABLE_NAME = "value" only lasts until you close PowerShell, while system-wide variables set with [System.Environment]::SetEnvironmentVariable() persist across sessions and restarts. System-wide variables require administrative rights and affect all users and applications on the machine, whereas session variables only affect the current PowerShell process.
To set a persistent user-scoped environment variable, use the [System.Environment]::SetEnvironmentVariable() method with [System.EnvironmentVariableTarget]::User as the scope. This creates a variable that persists for your user profile across PowerShell sessions without requiring a system restart or administrative rights.
You can retrieve an environment variable's value using either `Get-ChildItem Env:VARIABLE_NAME` or the simpler syntax `echo $env:VARIABLE_NAME`. Both methods will display the current value of the specified environment variable in your PowerShell session.
To delete an environment variable from the current session only, use the command `Remove-Item Env:VARIABLE_NAME`. This removes the variable temporarily and it will be restored when you start a new PowerShell session, unless it was originally a persistent system or user variable.
User and machine-scoped environment variables may require a PowerShell session restart or system restart to take effect because running processes cache environment variable values when they start. Restarting ensures that PowerShell reads the updated variable values from the system registry.