如果 PowerShell 中的环境变量不存在,如何设置它? [英] How do I set an env variable in PowerShell if it doesn't exist?

查看:39
本文介绍了如果 PowerShell 中的环境变量不存在,如何设置它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很惊讶在谷歌搜索了一段时间后我没有得到这个常见场景的答案......

I'm surprised that I didn't get the answer for this common scenario after Googling for while...

如果环境变量不存在,如何在PowerShell中设置?

How can an environment variable in be set in PowerShell if it does not exist?

推荐答案

以下代码为当前进程定义环境变量FOO,如果它还不存在的话.

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' }

PowerShell (Core) 7.1+ 中,它具有 null-coalescing 运算符,您可以简化为:

In PowerShell (Core) 7.1+, which has null-coalescing operators, you can simplify to:

$env:FOO ??= 'bar'

注意:根据定义,环境变量是字符串.如果给定的环境变量是定义的,但没有值,它的值是空字符串('') 而不是 $null.因此,比较 $null 可用于区分 undefined 环境变量和 已定义但没有值的环境变量.但是,请注意分配到环境变量在PowerShell/.NET没有区分$null''either 值会导致取消定义(删除)目标环境变量.

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.

注意:如果环境变量是由上面的赋值按需创建的($env:FOO = ...),它将存在于当前进程和它创建的任何子进程 谢谢,PetSerAl.

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.

以下内容主要由 Ansgar Wiechers 贡献,由 Mathias R. Jessen:

The following was mostly contributed by Ansgar Wiechers, with a supplement by Mathias R. Jessen:

Windows[*] 上,如果您想定义环境变量永久,您需要使用静态 SetEnvironmentVariable() 方法的 [System.Environment]类:

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')

请注意,这些定义在未来会话(进程)中生效,因此为了当前进程定义变量也是如此,运行 $env:FOO = 'bar' in addition,这实际上与 [Environment]::SetEnvironmentVariable('FOO', 'bar', '进程').

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').

[Environment]::SetEnvironmentVariable()UserMachine 一起使用时,WM_SETTINGCHANGE 消息是发送到其他应用程序以通知他们更改(尽管很少有应用程序对此类通知做出反应).
这不适用于定位 Process(或分配给 $env:FOO 时),因为无论如何其他应用程序(进程)都无法看到该变量.

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.

另见.

[*] 在 Unix 类平台上,针对持久作用域 UserMachine 的尝试会被悄悄忽略,截至.NET (Core) 5,并且由于缺乏跨 Unix 平台的统一机制,这种不支持定义持久环境变量不太可能改变.

[*] 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.

这篇关于如果 PowerShell 中的环境变量不存在,如何设置它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆