- Environment variables customize and control the behavior of PowerShell and the operating system.
- There are multiple methods and scopes for creating, listing, modifying, or deleting environment variables in PowerShell.
- Working correctly with environment variables improves script automation, security, and portability.

Environment variables in PowerShell have become an essential tool for both system administrators and developers. Their proper management allows for customizing the behavior of the operating system and various applications, facilitating automation, integrations, and teamwork. PowerShell , being a highly powerful and flexible command-line interface , offers several ways to access, modify, and persist environment variables, both for specific sessions and permanently.
Understanding how these variables work in PowerShell, the differences in their management depending on the operating system, and the most effective methods for listing, creating, or modifying them is essential to getting the most out of this console. Below, we'll dive into all the relevant aspects of manipulating environment variables in PowerShell, from basic theory to advanced examples, integrating all the best practices and essential tips compiled by experts and communities, as well as official documentation.
What are environment variables and what are they used for in PowerShell?
Environment variables are key-value pairs that store information used by both the operating system and running applications. They allow you to influence program behavior, facilitate communication between processes, and save persistent configurations (such as paths, preferences, credentials, etc.). In the context of PowerShell , these variables offer enormous flexibility for customizing scripts, automating tasks, and ensuring that processes inherit the correct environment when launched from the console.
For example, the PATH variable contains the list of directories that the system checks to find executables. This allows you to launch any program without typing its full path, as long as its folder is included in this variable. Other common environment variables include TEMP , USERNAME , and HOME (on Unix systems ), and PowerShell also has its own variables that are particularly useful, such as PSModulePath , POWERSHELL_TELEMETRY_OPTOUT , and many more.
Efficient management of these variables makes it possible to have environments adapted to different users, projects, or even specific phases of development and production, something highly valued in both small teams and large IT infrastructures.
Advantages of using environment variables in PowerShell
Using environment variables in PowerShell has many advantages, especially in scenarios where automation and script portability are key. These include:
- Flexibility: They allow you to change behavior without modifying the code, adapting scripts and programs to different contexts.
- Safety: El storage of sensitive data such as keys or tokens in environment variables avoids exposing it in the source code.
- Persistence: It is possible to maintain values between sessions or even save permanent variables for all users of the system.
- Process inheritance: Child processes can inherit environment variables from their parent, which is essential for orchestrating complex tasks.
Managing these variables correctly is therefore an essential skill for any professional working with PowerShell in Windows , Linux , or macOS environments.
Environment Variable Scopes in PowerShell
In PowerShell , environment variables exist in various scopes or levels, which determine their visibility and persistence:
- Scope of process: It only affects the current PowerShell session or process. Once the session is closed, the variables disappear.
- User scope: Variables are stored for the user who defines them and are available across all their sessions, but do not affect other users.
- Machine (system) scope: They are defined for all users and processes of the operating system, with maximum persistence and scope.
Modifying variables at the user and machine levels requires appropriate permissions, and these changes are often reflected in the Windows registry or in configuration files depending on the operating system.
How to list and query environment variables in PowerShell
There are several ways to view available environment variables. PowerShell offers quick and efficient methods for both obtaining a complete list and searching for specific variables:
- Full listing: The main command is Get-ChildItem Env:, which displays all environment variables and their values in the current session. Its aliases, such as gci Env:, dir Env: y ls Env:, they work the same.
- Individual consultation: To consult the value of a specific variable, the syntax is used $Env:VariableName. For example, $Env:PATH displays the value of the PATH variable.
- Filter results: You can combine Get-ChildItem Env: with Where-Object to search for variables whose name or value matches a given criterion.
Compared to the classic cmd , where the set command is used , PowerShell's options are much more powerful, as they allow you to easily customize and manipulate the output.
Create and modify environment variables in PowerShell
Creating or modifying environment variables in PowerShell is extremely simple. There are several possible methods, depending on the scope you want the variable to have:
Direct method with $Env:
The fastest and most commonly used way is to assign the value directly in the console:
$Env:MiVariable = "ValorEjemplo"
The change will only be valid for the current session. If you close the console, the variable will disappear.
Using Environment Provider Cmdlets
The PowerShell environment provider allows you to handle variables as if they were files or elements of a file system:
- Create new variable:
New-Item -Path Env:\MiVariable -Value 'ValorEjemplo' - Modify value:
Set-Item -Path Env:\MiVariable -Value 'NuevoValor' - Rename variable:
Rename-Item -Path Env:MiVariable -NewName NuevoNombre
These cmdlets only affect the current session unless persistent workarounds are used, which we'll discuss later.
.NET-based methods
PowerShell allows the use of .NET classes and methods. The System.Environment class is commonly used for environment variables . To define or modify variables persistently, the following is used:
::SetEnvironmentVariable('MiVariable', 'ValorEjemplo', 'User')
The third parameter indicates the scope: Process , User , or Machine . This allows variables to be set persistently for the user or the system.
Delete environment variables in PowerShell
Deleting an environment variable is just as easy as creating one. Depending on the method used, this can be done in several ways:
- Assigning empty value or $null:
$Env:MiVariable = ''o$Env:MiVariable = $null. You are removed from the current session. - Remove-Item:
Remove-Item -Path Env:\MiVariable. Deletes the variable from the session. - With .NET:
::SetEnvironmentVariable('MiVariable', '', 'User'). Delete the user variable in Windows.
It's important not to delete or overwrite system variables without knowing what effect they'll have, as some are essential for the proper functioning of the operating system or installed programs.
Advanced management: persistence and profiles
There are many occasions when it's useful to have an environment variable permanently available, even after restarting the computer or opening new sessions. To do this, you have several options depending on the operating system and purpose:
Edit the PowerShell profile
The PowerShell profile is a script that runs every time you open a console. You can add lines to define environment variables so they are always available.
# Ver ruta del perfil
echo $PROFILE
# Si no existe, créalo
new-item -path $PROFILE -ItemType File -Force
# Añade al perfil la declaración para persistir variables
$perfilContenido = @"
$Env:MiVariable = "Valor persistente"
"@
$perfilContenido | Add-Content -Path $PROFILE
This ensures that the variable will be available in every new PowerShell session for that user.
Using SetEnvironmentVariable() with scope
With `::SetEnvironmentVariable` you can create environment variables for the user or the entire system, which will persist after closing the console or restarting Windows. Additionally, some aspects related to managing environment variables in PowerShell are covered in this guide on managing GPOs in PowerShell.
::SetEnvironmentVariable('MiVariable', 'Valor', 'User')
::SetEnvironmentVariable('MiVariable', 'Valor', 'Machine')
The first command saves it only for the user, the second for everyone. Deletion is done the same as before, assigning an empty string.
Environment Variables in PowerShell: Practical Examples and Special Variables
In addition to traditional variables, PowerShell has its own variables that allow for further customization of the console environment. Some of the most important are:
- POWERSHELL_TELEMETRY_OPTOUT: Allows you to disable sending telemetry data to Microsoft.
- POWERSHELL_DISTRIBUTION_CHANNEL: Reports how the PowerShell instance was installed (manual modification is not recommended).
- POWERSHELL_UPDATECHECK: Controls the behavior of update notifications.
- PSExecutionPolicyPreference: Reflects the script execution policy in the current session (Windows only).
- PSModulePath: List of folders where PowerShell looks for modules and resources.
- PSModuleAnalysisCachePath: Path to the cache file for modules and cmdlets, useful for speeding up searches and imports.
- PSDisableModuleAnalysisCacheCleanup: Allows you to disable automatic clearing of the module cache.
Having these variables properly configured helps optimize the performance, security, and control of your PowerShell environments.
PATH, PATHEXT, and standard variables in Windows and other systems
Of all the environment variables, PATH is probably the most important. It stores the directories that the system explores when you run a command without a full path. In Windows , directories are separated by semicolons ( ;) , while in Unix systems , a colon (:) is used as the directory separator.
Another interesting variable is PATHEXT , which specifies which extensions are considered executable, thus allowing scripts in different languages to be launched simply by invoking their name.
On systems other than Windows, it is common to find standard XDG variables (for example, XDG_CONFIG_HOME , XDG_DATA_HOME or XDG_CACHE_HOME ) that PowerShell also recognizes if it is running on those platforms, following system standards.
How to work with environment variables in Linux and macOS using PowerShell
PowerShell is cross-platform, so you can use the same basic techniques on Unix-based systems. However, to make variables persist beyond the PowerShell session, you'll need to modify them in the appropriate initialization files. For example:
- /etc/environment: Global file in Linux where you can define persistent variables for all users.
- /etc/profile.d/: Folder where you can place scripts that set variables when starting a shell session.
- ~/.bashrc or ~/.zshrc: User- and shell-specific files where you can export variables that are loaded upon login.
Remember that for variables to have a global effect, you need administrator permissions and will need to restart your session or system.
Table of Contents
- 1 What are environment variables and what are they used for in PowerShell?
- 2 Advantages of using environment variables in PowerShell
- 3 Environment Variable Scopes in PowerShell
- 4 How to list and query environment variables in PowerShell
- 5 Create and modify environment variables in PowerShell
- 6 Delete environment variables in PowerShell
- 7 Advanced management: persistence and profiles
- 8 Environment Variables in PowerShell: Practical Examples and Special Variables
- 9 PATH, PATHEXT, and standard variables in Windows and other systems
- 10 How to work with environment variables in Linux and macOS using PowerShell
