С помощью PowerShell вы можете получать, добавлять, удалять, или изменять значения переменных окружения (среды). В переменных окружения Windows хранит различную пользовательскую и системную информацию (чаще всего это пути к системным и временным папкам), которая используется операционной системой и приложениями, установленными на компьютере.
В Windows доступны несколько типов переменных окружения:
- Переменные окружения процесса – создаются динамически и доступны только в текущем запущенном процесс
- Пользовательские переменные окружения – содержат настройки конкретного пользователя и хранятся в его профиле (хранятся в ветке реестре
HKEY_CURRENT_USER\Environment
) - Системные переменные окружения – глобальные переменные окружения, которые применяются для все пользователей (хранятся в ветке
HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
)
Типы переменных окружения Windows указаны в порядке уменьшения приоритета. Т.е. значение переменной окружения %TEMP% в пользовательском профиле будет иметь больший приоритет, чем значение системной переменной окружения %TEMP%.
Для управления переменными окружениями обычно используется вкладка Advanced в свойствах системы. Чтобы открыть System Properties, выполните команду
SystemPropertiesAdvanced
и нажмите на кнопку Environment Variable.
В этом окне можно создать и отредактировать переменные среды для текущего пользователя или системные переменные окружения.
Чтобы вывести полный список переменных окружения и их значения в PowerShell, выполните команду:
Get-ChildItem Env:
Как вы видите, для доступа к переменным окружения в PowerShell используется отдельный виртуальный диск Env:, доступный через провайдер Environment.
Получить значение определенной переменной окружения Path:
Get-ChildItem env:Path
Т.к. переменные окружения, по сути, это файлы на виртуальном диске, нажатием кнопки TAB вы можете использовать автозавершение для набора имени переменной окружения.
Чтобы разбить значение переменной окружения на строки, выполните:
(Get-ChildItem env:Path).value -split ";"
Добавить значение в переменную окружения Path:
$Env:Path += ";c:\tools"
Однако это добавляет временное значение в переменную окружения
Path
. При следующей перезагрузке новое значение в переменной будет сброшено. Чтобы добавить постоянное значение в системную переменную окружения, используется такая конструкция:
$path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
[System.Environment]::SetEnvironmentVariable("Path", $path + ";C:\tools", "Machine")
[System.Environment]::GetEnvironmentVariable("Path","Machine") -Split ";"
Чтобы изменить пользовательскую переменную окружения, замените в предыдущих командах область Machine на User.
Несмотря на то, что фактически переменные окружения и их значения хранятся в реестре, прямое изменение их значений в реестре используется редко. Причина в том, что текущий процесс при запуске считывает значение переменных окружения из реестра. Если вы измените их, процесс не будет уведомлён об этом.
Если вам нужно из PowerShell изменить в реестре значение переменной окружения, используются команды:
$variable = Get-ItemPropertyValue -Path 'HKCU:\Environment\' -Name 'Path'
$add_path = $variable + ';C:\Git\'
Set-ItemProperty -Path 'HKCU:\Environment\' -Name 'Path' -Value $add_path
Вы можете создать локальную переменную окружения. По умолчанию такая переменная окружения будет доступна только в текущем процессе (PowerShell), из которого она создана. После того, как процесс будет закрыт – переменная окружения будет удалена.
$env:SiteName = 'winitpro.ru'
Get-ChildItem Env:SiteName
Если нужно создать глобальную системную переменную (нужны права администратора), используйте команду:
[System.Environment]::SetEnvironmentVariable('siteName','winitpro.ru',"Machine")
Очистить и удалить глобальную переменную окружения:
[Environment]::SetEnvironmentVariable('siteName', $null, "Machine")
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.
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 $value
In 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.
-
What Are Environment Variables
-
Set an Environment Variable With
Env:in PowerShell -
Set the
[System.Environment].NET Class in PowerShell -
Refresh Environment Variables on Current PowerShell Session
-
Conclusion
Using Windows PowerShell to set Windows environment variables, read environment variables, and create new environment variables is easy once we know the proper execution of commands.
PowerShell provides many different methods to interact with Windows environment variables from the $env: PSDrive and the [System.Environment] .NET class. This article will discuss setting environment variables and refreshing them in our current session using PowerShell.
What Are Environment Variables
As the name suggests, environment variables store information about Windows and applications’ environments.
We can access environment variables through a graphical interface such as Windows Explorer and plain text editors like Notepad, cmd.exe, and PowerShell.
Using environment variables helps us avoid hard-coding file paths, user or computer names, and more in our PowerShell scripts or modules.
Set an Environment Variable With Env: in PowerShell
We can create new environment variables with PowerShell using the New-Item cmdlet. But, first, provide the environment variable’s name in the Env:\<EnvVarName> format for the Value parameter, as shown below.
Example Code:
New-Item -Path Env:\TEST -Value WIN10-DESKTOP
We use the PowerShell cmdlet New-Item to create or update an environment variable named TEST within the Env:\ namespace. By specifying -Value WIN10-DESKTOP, we assign the value WIN10-DESKTOP to this variable.
This action impacts the current PowerShell session, making the TEST environment variable accessible to other scripts and commands within the session.
Output:
We can use the Set-Item cmdlet to set an environment variable or create a new one if it doesn’t already exist. For example, we can see below using the Set-Item cmdlet.
We can both make or modify an environment variable.
Example Code:
Set-Item -Path Env:TEST -Value "TestValue"
We use the PowerShell cmdlet Set-Item to modify the value of an existing environment variable named TEST within the Env:\ namespace. By specifying -Value "TestValue", we assign the string "TestValue" to the TEST environment variable.
This action directly affects the current PowerShell session, updating the value of TEST for use by other scripts and commands within the session.
Output:
Set the [System.Environment] .NET Class in PowerShell
The [System.Environment] will use a few different .NET static class methods. We don’t need to understand what a static way is.
We only need to understand how to use any of the techniques we are about to learn, and we will need first to reference the class ([System.Environment]) followed by two colons (::) then followed by the method.
To set the environment variable using the .NET class stated, use the SetEnvironmentVariable() function to set the value of an existing environment variable for the given scope or create a new environment variable if it does not already exist.
When setting variables in the process scope, we will find that the process scope is volatile and existing in the current session, while changes to the user and machine scopes are permanent.
Example Code:
[System.Environment]::SetEnvironmentVariable('TestVariable', 'TestValue', 'User')
We use the .NET method [System.Environment]::SetEnvironmentVariable() to set an environment variable named TestVariable with the value 'TestValue' in the user-specific environment. By specifying 'User' as the third parameter, we ensure that this variable is set within the user’s environment context.
This action impacts the environment settings for the current user, allowing other scripts and applications to access the TestVariable with its assigned value.
Refresh Environment Variables on Current PowerShell Session
To use our new set of environment variables in our PowerShell session, get the environment variable of the user profile and machine through the .NET class and assign it to the PowerShell environment variable.
Since environment variables are also considered PowerShell variables, we can assign values to them directly more straightforwardly.
Example Code:
$env:PATH = [System.Environment]::GetEnvironmentVariable("Path", "Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path", "User")
We are updating the $env:PATH environment variable by concatenating the values of the system and user Path environment variables retrieved using [System.Environment]::GetEnvironmentVariable(). We first retrieve the system Path variable using the "Machine" parameter and then append the user Path variable retrieved using the "User" parameter.
By combining these values with a semicolon separator, we ensure that both sets of paths are included in the updated $env:PATH.
Output:
Conclusion
In this article, we explored various methods for managing environment variables in PowerShell, focusing on setting and refreshing them using different techniques. We began by demonstrating how to create or modify environment variables using PowerShell cmdlets like New-Item and Set-Item, providing clear examples and explanations for each step.
Next, we delved into the use of the [System.Environment] .NET class to manipulate environment variables. Through the SetEnvironmentVariable() method, we learned how to set variables with specific scopes, understanding the implications of each scope on variable persistence.
Lastly, we covered the importance of refreshing environment variables within the current PowerShell session, emphasizing the use of [System.Environment]::GetEnvironmentVariable() to update variables dynamically.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
What is an environment variable in Windows? An environment variable is a dynamic “object” containing an editable value which may be used by one or more software programs in Windows.
In this note i am showing how to set an environment variable in Windows from the command-line prompt (CMD) and from the Windows PowerShell.
In the examples below i will set an environment variable temporary (for the current terminal session only), permanently for the current user and globally for the whole system.
Cool Tip: Add a directory to Windows %PATH% environment variable! Read More →
Set Environment Variable For The Current Session
Set an environment variable for the current terminal session:
# Windows CMD C:\> set VAR_NAME="VALUE" # Windows PowerShell PS C:\> $env:VAR_NAME="VALUE"
Print an environment variable to the console:
# Windows CMD C:\> echo %VAR_NAME% # Windows PowerShell PS C:\> $env:VAR_NAME
Cool Tip: List Windows environment variables and their values! Read More →
Set Environment Variable Permanently
Run as Administrator: The setx command is only available starting from Windows 7 and requires elevated command prompt. It works both for the Windows command-line prompt (CMD) and the Windows PowerShell.
Permanently set an environment variable for the current user:
C:\> setx VAR_NAME "VALUE"
Permanently set global environment variable (for all users):
C:\> setx /M VAR_NAME "VALUE"
Info: To see the changes after running setx – open a new command prompt.
Was it useful? Share this post with the world!
Introduction to PowerShell Environment Variables
Environment variables are predefined variables in an operating system, they are available in the form of key-value pairs which store important system-level or user-specific information, such as paths, user configurations, and system settings. These variables are accessible in PowerShell scripts and sessions, playing a significant role in tasks like configuring software, adjusting system paths, and managing user-specific settings. They function as global variables, accessible across the operating system and scripting environment. While PowerShell allows easy manipulation of these variables, it is important to be cautious when modifying or deleting them, as incorrect changes can lead to system instability or application failures. For instance, the PATH variable, which stores directories containing executable files, is critical for the system to locate and execute commands.
Listing PowerShell Environment Variables
In PowerShell, one of the most straightforward methods to list all environment variables is by using the Get-ChildItem cmdlet with the “Env:” drive. This command allows users to access and display the contents of the environment variable repository in a clean and organized format.
To list all the environment variables available in your PowerShell session, simply execute the following command.
Get-ChildItem Env:
The output will typically include a wide array of environment variables, some of which you may recognize.
- PATH: A list of directories where executable files are stored.
- TEMP: The path to the temporary file storage directory.
- USERPROFILE: The path to the current user’s profile directory e.g., C:\Users\Administrator.Milkyway.
- COMPUTERNAME: The name of the computer.
- OS: The operating system name e.g., Windows_NT.
- HOMEPATH: The home directory of the user.
Accessing and Printing Environment Variables
There are two primary methods to accomplish this: using the “$Env:<variable-name>” syntax or employing the “Get-ChildItem” cmdlet along with the variable name. For example, if you want to access the USERNAME variable, you will use the following command in PowerShell to get environment variable.
$Env:USERNAME
Simply executing this command will print the logged in user’s name to the console.
Second method is by using the Get-ChildItem cmdlet. You can specify the environment variable you want to check, as below.
(Get-ChildItem Env:USERNAME).Value
You can follow the same pattern to print other environment variables, such as PATH, COMPUTERNAME, USERPROFILE or TEMP.
$Env:Path
(Get-ChildItem Env:Path).Value
Setting PowerShell Environment Variables
The process of creating or updating environment variables is straightforward and can be accomplished using the below syntax.
$Env:<variable-name> = “<value>”
Creating Environment Variables
Creating a new environment variable is as simple as assigning a value to a variable name. For example, if you want to create an environment variable named “Foo” and set it to a specific value, you can use the following command.
$Env:Foo = “Hello”
You can confirm that the variable has been set correctly by accessing it using below cmdlet.
$Env:Foo
Updating Environment Variables
If the variable “Foo” already exists and you wish to update its value, you can use the same syntax.
$Env:Foo = “NewValue”
You can check the value of variable as below.
$Env:Foo
One important thing to note is that changes made to environment variables using the $Env: prefix only persist for the duration of the current PowerShell session. Once you close the session, any custom environment variables created or modified in that session will be lost. If you need to create or update environment variables that persist beyond the session, you will need to use the “SetX” command, which allows you to define environment variables at the user or system level. For example, to create a permanent environment variable named “Foo” with the same value, you can use the below cmdlet.
SetX Foo “MyVariableValue”
You can check the permanent variable by going into System PropertiesèAdvanced TabèEnvironment VariablesèUser Variables for current user.
Appending Values to Existing Environment Variables
Appending values to existing environment variables in PowerShell can be useful when you want to expand the current configurations without overwriting their existing values. One common scenario for this is with the PATH environment variable, where you may want to add additional directories for executable files. In PowerShell, the “+=” operator allows you to accomplish this seamlessly, this operator is specifically designed to append values to variables.
For example, if you want to append a new directory to the `Path` environment variable, you can use the following command.
$Env:Path += “;C:\Test\SecurityService”
The semicolon ( ; ) is important because it acts as a separator between individual paths in the PATH variable. Without it, the new path would be concatenated directly to the existing paths, which could lead to unintended results. To verify that the value has been successfully appended, you can print the updated variable using the below cmdlet.
$Env:Path
Scopes of PowerShell Environment Variables
Understanding the scopes of PowerShell environment variables is important for effective scripting and system configuration. Environment variables can exist in different scopes, which determine their availability and lifespan in different contexts.
Explanation of machine, user, and process scopes for environment variables.
The three primary scopes for environment variables are machine, user, and process. Each scope has its own rules regarding accessibility and inheritance, which can significantly influence how scripts and applications behave.
Machine Scope
Machine-scoped environment variables are system-wide variables that apply to all users and processes running on the computer. These variables are typically set by the operating system or an administrator and are persistent across sessions and reboots. To access or modify machine-scoped environment variables, you need administrative privileges.
User Scope
User-scoped environment variables, on the other hand, are specific to the individual user profile, and are visible to all processes running under that user’s profile. These variables are persistent but only available to the user who created them and can be modified without requiring administrator privileges. If one user sets a user-scoped variable, other users on the same machine do not have access to it.
Process Scope
Process-scoped environment variables exist only in the instance of the process or session, in which they were created. They are temporary and will disappear once the process ends. This scope is commonly used in scripts and applications where you want to define settings that are relevant only for the duration of execution.
How environment variables are inherited by child processes
Environment variables are inherited by child processes from their parent process. This means that if you create an environment variable in a PowerShell session (process scope), any external application or script launched from that session will inherit the variable. If you set a user-level environment variable, such as MY_USER_VAR, it will persist across PowerShell sessions, and any process launched under that user will inherit its value. If you set a machine-wide variable, such as MY_MACHINE_VAR, it will be inherited by every user on the system, and all processes will have access to that variable.
Modifying Environment Variables Across Platforms
When it comes to modifying environment variables, the approach can vary significantly between operating systems, particularly between Windows and Unix-like systems such as macOS and Linux. This variability includes syntax, case sensitivity, and available commands for modification.
Case sensitivity on macOS/Linux versus Windows.
One of the key differences between operating systems is how they handle the case sensitivity of environment variable names.
Windows
Environment variable names in Windows are not case-sensitive. For instance, PATH, Path, and path, all refer to the same environment variable. This means that you can reference or modify these variables without worrying about the case used in the commands.
macOS/Linux
Unix-like systems are case-sensitive when it comes to environment variable names. Thus, PATH and path would be recognized as two different variables. This means that scripts or commands must use the exact casing when dealing with environment variables to avoid confusion or errors.
Temporary vs Persistent Environment Variables
Environment variables in PowerShell can be categorized as either temporary or persistent depending on how they are set and the scope of the changes. Understanding the difference between these two types of environment variables is important for managing configurations and ensuring that variables last across sessions or system restarts.
How temporary variables work within a PowerShell session
Temporary environment variables are defined and used solely within the context of the current PowerShell session. These variables can be created and modified using the $Env: prefix. Once the PowerShell session is closed, any temporary variables will be lost, and their values will not be retained in subsequent sessions. Temporary variables are particularly useful when you need a quick configuration for the current session without affecting the global or user-specific settings.
Persistent Environment Variables
Persistent environment variables are stored within the system configuration and will remain available even after you close and reopen PowerShell or restart your computer. To create or modify persistent environment variables in PowerShell, you can use the .NET “System.Environment” class.
The syntax for adding or modifying a persistent environment variable are as follows.
[Environment]::SetEnvironmentVariable(‘VariableName’, ‘Value’, ‘Scope’)
The Scope parameter can be one of three values, Machine, User or Process.
For example, to create a persistent user-scoped environment variable named “Foo” with the value “Bar”, you can run the following command.
[Environment]::SetEnvironmentVariable(‘Foo’, ‘Bar’, ‘User’)
If you wanted to set a machine-scoped variable, you would run below command.
[Environment]::SetEnvironmentVariable(‘Foo’, ‘Bar’, ‘Machine’)
Above command will require administrative privileges, as machine-scoped changes affect all users on the system.
GetEnvironmentVariable()method shows the value of an environment variable. You can specify the name of the environment variable. Below is an example cmdlet.
[System.Environment]::GetEnvironmentVariable(‘Foo’)
May need to verify the above command for scope
Working with Environment Provider and Item Cmdlets
Using the environment provider to manage environment variables as a file system drive
PowerShell provides an environment provider that treats environment variables as a file system drive, allowing users to manage them much like files and directories. The Env: drive in PowerShell provides access to environment variables. Each environment variable is treated as an item in the Env: drive, and you can use standard cmdlets to interact with these variables. Below is an overview of the key cmdlets for working with the environment provider, such as New-Item, Set-Item, Get-Item, and Remove-Item.
New-Item
The New-Item cmdlet can be used to create a new environment variable, by following the below example syntax and cmdlet.
New-Item -Path Env:MY_NEW_VAR -Value “MyValue”
New-Item -Path Env: -Name “MyVariable” -Value “HelloWorld”
This command creates a new environment variable that will be available for the duration of the current session.
Set-Item
If you want to update the value of an existing environment variable or create it if it does not already exist, you can use the Set-Item cmdlet, using the below example cmdlet.
Set-Item -Path Env:MY_NEW_VAR -Value “UpdatedValue”
Set-Item -Path Env:MyVariable -Value “NewValue”
This command will set environment variable value to new. If variable does not exist, this command will create it with the specified value.
Get-Item
To retrieve the value of an existing environment variable, the Get-Item cmdlet can be used by following the below example cmdlet.
Get-Item -Path Env:MY_NEW_VAR
Get-Item -Path Env:MyVariable
This will return an object representing the environment variable, including its name and the value assigned to it.
Remove-Item
To delete an existing environment variable, you can use the Remove-Item cmdlet.
Remove-Item -Path Env:MY_NEW_VAR
Remove-Item -Path Env:MyVariable
This command will remove variable from the environment variables, you can use Get-Item cmdlet to verify. It is important to note that once this variable is removed, it cannot be recovered unless it is recreated.
Common PowerShell Environment Variables
PowerShell has several key environment variables that can affect its behavior and configuration. These variables are created and used to configure how PowerShell behaves, where it looks for modules, scripts, and several user preferences. Below are some of the key environment variables commonly used in PowerShell.
POWERSHELL_TELEMETRY_OPTOUT
This environment variable controls whether PowerShell sends telemetry data to Microsoft. If you set this variable to 1, it opts out of sending usage data and crash reports to Microsoft. By default, PowerShell sends anonymous usage data for improvement purposes, but you can opt out.
POWERSHELL_DISTRIBUTION_CHANNEL
Specifies the distribution channel through which PowerShell was installed. This can be useful for identifying whether PowerShell was installed via Windows Package Manager (winget), MSI, or other package managers. This helps Microsoft track which channels are most used.
POWERSHELL_UPDATECHECK
This environment variable controls whether PowerShell automatically checks for updates. If set to 1, PowerShell will check for updates to the shell. If set to 0, it disables automatic update checking. This is useful to manage how PowerShell handles updates.
PSExecutionPolicyPreference
This environment variable specifies the default PowerShell execution policy to use when running scripts. If it is set, it overrides the execution policy configured through Set-ExecutionPolicy. For example, setting this to RemoteSigned means only locally created scripts can run without signing.
PSModulePath
This variable contains the paths where PowerShell modules are stored and loaded from. It is a colon-separated list of directories that PowerShell searches to find modules. You can modify this variable to include additional directories for custom modules.
PSModuleAnalysisCachePath
This variable specifies the path where PowerShell stores module analysis cache. PowerShell uses module analysis to optimize performance when loading modules, and the cache stores parsed metadata about the modules. Setting a custom path allows for redirecting this cache location.
PSDisableModuleAnalysisCacheCleanup
If this variable is set to 1, PowerShell will disable the automatic cleanup of the module analysis cache. By default, PowerShell automatically clears unused analysis cache to save disk space, but this variable can be set to prevent that action.
There are several other environment variables that you can use to configure your environment or retrieve system-related information using PowerShell. Some of them are already mentioned already in this blog above, below are some more common environment variables.
- USERNAME: Returns the current user’s username.
- ProgramFiles: Points to the “Program Files” directory, usually C:\Program Files.
- ProgramFiles(x86): Points to the “Program Files (x86)” directory, typically used for 32-bit applications on a 64-bit system.
- ALLUSERSPROFILE: Points to the all-users profile directory (usually C:\ProgramData).
- WINDIR: Points to the Windows directory (usually C:\Windows).
Managing Environment Variables via the Control Panel
You can set or modify environment variables directly through the Windows Control Panel. By following the steps below, you can easily manage environment variables through the Windows Control Panel, customizing your system’s settings as needed.
- Open the Control Panel, by pressing “Windows + R” to open the Run dialog. Type “control” and hit Enter to open the Control Panel, or search for control panel in search bar beside Start button and select it.
- In the Control Panel, click on “System”. In the left sidebar, click on “Advanced system settings”.
- In the System Properties window, click on the “Environment Variables*” button located in the bottom right corner.
- In the Environment Variables window, you will see two sections, “User variables” (for the current user) and “System variables” (for all users).
- To create a new variable, click New under the respective section.
- To edit an existing variable, select the variable from the list and click Edit.
- To delete a variable, select it and click Delete.
- If you are creating or editing a variable, enter the “Variable name” and “Variable value” in the respective fields. For variables like Path, you can add new paths by separating them with semicolons. Below is the screenshot of PATH variable.
- After making your changes, click OK to close each dialog, saving your modifications.
- For your changes to take effect, you may need to restart any open applications or command prompts.
Troubleshooting Common Issues with PowerShell Environment Variables
Environment variables in PowerShell can be tricky to work with, especially when dealing with scopes, persistence, or trying to set invalid values. Below are some common issues users face when working with environment variables.
Setting an Environment Variable to an Empty String
When you attempt to set an environment variable to an empty string, you may not see the expected behavior. Instead of clearing the variable, it might still hold its previous value. Setting an environment variable to an empty string does not remove it, it just assigns an empty value. To completely remove the environment variable instead of setting it to an empty string, you can use the Remove-Item cmdlet.
Incorrect Scope Used for Setting Environment Variables
When setting an environment variable, if you specify the wrong scope (User vs. Process vs. Machine), the variable might not be available where you expect it to be. To set the variable for the user or system scope permanently, you can use the below cmdlet.
[System.Environment]::SetEnvironmentVariable(‘Variable’, ‘Value’, ‘scope’)
Changes Not Reflected After Setting Environment Variables
After modifying an environment variable, you might find that changes do not immediately reflect in subprocesses or other applications. Environment variables are cached in the process. When you modify an environment variable in one session, other sessions may not see the change until restarted. To make sure that other applications see the changes, restart them. In some cases, for applications to pick up the changes, a system restart may be necessary.
Permission Errors When Setting System Variables
When trying to set a system-level environment variable, you may encounter an “access denied” error. Modifying system environment variables typically requires administrator privileges. Run PowerShell as an administrator.
Variables Not Persisting Across Sessions
You set an environment variable expecting it to persist across sessions, but it disappears.
If the variable is set only in the current session using “$env:”, it will not persist after closing PowerShell. Make sure you are setting the variable at the user or system level using System.Environment class or registry editor.
Mismatch in Case Sensitivity
Environment variable names are case-insensitive on Windows, but you might encounter case-sensitivity issues when working with cross-platform scripts such as in PowerShell Core on Linux or macOS. When writing scripts that will run on multiple platforms, make sure that you consistently use the correct case for environment variable names.
Best Practices for managing Environment Variables
Managing environment variables effectively is crucial for maintaining a stable and efficient working environment in PowerShell. Below are some best practices for using environment variables efficiently across platforms.
Use Descriptive Names
Choose clear and descriptive names for your environment variables to make them easily identifiable. For example, use MY_APP_CONFIG_PATH instead of something vague like CONFIG_PATH. This makes it easier for others (and future you) to understand the role of the variable. Another example would be, instead of “Var_Database”, use “DatabaseConnectionString” which is more descriptive.
Limit Scope Appropriately
Understand the different scopes of environment variables (Process, User, Machine) and set the scope according to your needs. Limit variables to the smallest needed scope to avoid potential conflicts. Use process scope for temporary or session-specific variables. Use user scope for variables that should be available to the current user across sessions but not to other users. Use machine scope for system-wide configurations that should be accessible by all users and processes, such as database connection strings or system-wide configuration files.
Document Your Variables
Keep a documentation file or comments in your scripts explaining the purpose of each environment variable, how it is used, and any dependencies. This will help others or yourself in the future to understand your setup.
Avoid Hardcoding Sensitive Data
Rather than hardcoding sensitive information like API keys, passwords, or values into your scripts, use environment variables to make your scripts more flexible and portable. This allows the same script to run on different machines with different configurations.
Check for Existing Variables
Before creating a new environment variable, check if it already exists to avoid unintentional overrides. This ensures that your scripts or applications do not fail due to missing environment variables.
Use Profile Scripts for Persistency
For environment variables that you want to have persist across sessions, consider placing them in your PowerShell profile. This way, they will be set every time you open a new PowerShell session.
Cleaning Up Unused Environment Variables
Remove unused environment variables to prevent clutter and potential conflicts, especially in long-running sessions or large systems. You can remove environment variables using PowerShell’s Remove-Item cmdlet. This helps maintain a clean environment and avoids accidental use of outdated or irrelevant variables.
Test in a Safe Environment
Before making changes to key environment variables, especially in a production environment, test them in a development or staging environment to avoid disruptions.
Be careful with Path modifications
When modifying the PATH variable, make sure you do not inadvertently remove important paths. Always append (+=) rather than overwrite (=) and make a backup of the existing PATH if necessary.
Consider Cross-Platform Compatibility
If your scripts will run in different environments (Windows, Linux, macOS), consider the case sensitivity of environment variable names on Linux and macOS. Windows environment variables are case-insensitive, but Linux/macOS environment variables are case-sensitive. Test scripts on all target platforms to make sure environment variables work correctly and adjust your approach accordingly.
FAQs
- How to set an environment variable using PowerShell?
To set an environment variable, you can use the following syntax.
$Env:VariableName = “Value”
Replace VariableName with the desired name of your variable and Value with the value you want to assign to it, as example below.
$Env:MY_VAR = “MyValue”
2. How do I list all variables in PowerShell?
To list all variables in your PowerShell session, you can use:
Get-ChildItem Env:
This command will show all currently defined variables in the session, including environment variables.
3. How to check if an environment variable exists in PowerShell?
To check if a specific environment variable exists, you can use the following command.
Test-Path Env:MY_VAR Replace MY_VAR with the name of the environment variable you want to check. If the variable exists, it will return True; if not, it will return False.
Since 2012, Jonathan Blackwell, an engineer and innovator, has provided engineering leadership that has put Netwrix GroupID at the forefront of group and user management for Active Directory and Azure AD environments. His experience in development, marketing, and sales allows Jonathan to fully understand the Identity market and how buyers think.
