下面的代码定义了环境变量FOO
对于当前进程(如果尚不存在)。
if ($null -eq $env:FOO) { $env:FOO = 'bar' }
# If you want to treat a *nonexistent* variable the same as
# an existent one whose value is the *empty string*, you can simplify to:
if (-not $env:FOO) { $env:FOO = 'bar' }
# Alternatively:
if (-not (Test-Path env:FOO)) { $env:FOO = 'bar' }
# Or even (quietly fails if the variable already exists):
New-Item -ErrorAction Ignore env:FOO -Value bar
In PowerShell(核心)7.1+, 其中有空合并运算符 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_Operators#null-coalescing-assignment-operator-,您可以简化为:
$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; similarly, in cmd.exe
set FOO=
results in removal/non-definition of variable FOO
, and the GUI dialog (accessible via sysdm.cpl
) doesn't allow you to define a variable with an empty string either. However, the Windows API (SetEnvironmentVariable https://learn.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-setenvironmentvariable) does permit creating environment variables that contain the empty string.
On Unix-like platforms, empty-string values are allowed too, and the native, POSIX-compatible shells (e.g, bash
and /bin/sh
) - unlike PowerShell - also allow you to create them (e.g, export FOO=
). Note that environment variable definitions and lookups are case-sensitive on Unix, unlike on Windows.
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 https://stackoverflow.com/users/4003407/petseral.
The following was mostly contributed by Ansgar Wiechers https://stackoverflow.com/users/1630171/ansgar-wiechers, with a supplement by Mathias R. Jessen https://stackoverflow.com/users/712649/mathias-r-jessen:
On Windows[*], if you want to define an environment variable persistently, you need to use the static SetEnvironmentVariable() https://learn.microsoft.com/en-US/dotnet/api/System.Environment.SetEnvironmentVariable method of the [System.Environment] https://learn.microsoft.com/en-US/dotnet/api/System.Environment class:
# user environment
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'User')
# system environment (requires admin privileges)
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'Machine')
请注意,这些定义生效于future会话(进程),所以按顺序定义变量current过程as well, run $env:FOO = 'bar'
此外,这实际上等同于[Environment]::SetEnvironmentVariable('FOO', 'bar', 'Process')
.
When using [Environment]::SetEnvironmentVariable()
with User
or Machine
, a WM_SETTINGCHANGE https://msdn.microsoft.com/en-us/library/windows/desktop/ms725497%28v=vs.85%29.aspx 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.
也可以看看:创建和修改环境变量 https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-powershell-1.0/ff730964(v=technet.10)(TechNet 文章)。
[*] On Unix-like platforms, attempts to target the persistent scopes - User
or Machine
- are quietly ignored, as of .NET (Core) 7, and this non-support for defining persistent environment variables is unlikely to change, given the lack of a unified mechanism across Unix platforms.