Issue
I'm surprised that I didn't get the answer for this common scenario after Googling for while...
How can an environment variable in be set in PowerShell if it does not exist?
Solution
The following code defines environment variable FOO
for the current process, if it doesn't exist yet.
if ($null -eq $env:FOO) { $env:FOO = 'bar' }
# Alternatively:
if (-not (Test-Path env:FOO)) { $env:FOO = 'bar' }
In PowerShell (Core) 7.1+, which has null-coalescing operators, you can simplify to:
$env:FOO ??= 'bar'
Note: Environment variables are strings by definition. If a given environment variable is defined, but has no value, its value is the empty string (''
) rather than $null
. Thus, comparing to $null
can be used to distinguish between an undefined environment variable and one that is defined, but has no value. However, note that assigning to environment variables in PowerShell / .NET makes no distinction between $null
and ''
, and either value results in undefining (removing) the target environment variable.
Note: If the environment variable is created on demand by the assignment above ($env:FOO = ...
), it will exist for the current process and any child processes it creates only Thanks, PetSerAl.
The following was mostly contributed by Ansgar Wiechers, with a supplement by Mathias R. Jessen:
On Windows[*], if you want to define an environment variable persistently, you need to use the static SetEnvironmentVariable()
method of the [System.Environment]
class:
# user environment
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'User')
# system environment (requires admin privileges)
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'Machine')
Note that these definitions take effect in future sessions (processes), so in order to define the variable for the current process as well, run $env:FOO = 'bar'
in addition, which is effectively the same as [Environment]::SetEnvironmentVariable('FOO', 'bar', 'Process')
.
When using [Environment]::SetEnvironmentVariable()
with User
or Machine
, a WM_SETTINGCHANGE
message is sent to other applications to notify them of the change (though few applications react to such notifications).
This doesn't apply when targeting Process
(or when assigning to $env:FOO
), because no other applications (processes) can see the variable anyway.
[*] On Unix-like platforms, attempts to target persistent scopes User
or Machine
are quietly ignored, as of .NET (Core) 5, and this non-support for defining persistent environment variables is unlikely to change, given the lack of a unified mechanism across Unix platforms.
Answered By - mklement0 Answer Checked By - Mary Flores (PHPFixing Volunteer)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.