如果不存在,如何在Powershell中设置一个env变量?

时间:2021-06-28 23:12:56

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?

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

1 个解决方案

#1


26  

The following code defines environment variable FOO, if it doesn't exist yet.

以下代码定义了环境变量FOO,如果它尚不存在的话。

if (-not (Test-Path env:FOO)) { $env:FOO = 'bar' }

Note: This newly defined environment variable will only exist for the current process and any child processes it creates (e.g., when you start a new PowerShell session from the ISE). Thanks, PetSerAl.

注意:此新定义的环境变量仅适用于当前进程及其创建的任何子进程(例如,从ISE启动新的PowerShell会话时)。谢谢,PetSerAl。

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

以下内容主要由Ansgar Wiechers提供,Mathias R. Jessen补充说:

If you want to define an environment variable persistently, you need to use the static SetEnvironmentVariable() method of the [System.Environment] class:

如果要持久定义环境变量,则需要使用[System.Environment]类的静态SetEnvironmentVariable()方法:

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

请注意,这些定义在将来的会话(进程)中生效,因此为了定义当前进程的变量,另外运行$ env:FOO ='bar',这实际上与[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.

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

See also.

也可以看看。

#1


26  

The following code defines environment variable FOO, if it doesn't exist yet.

以下代码定义了环境变量FOO,如果它尚不存在的话。

if (-not (Test-Path env:FOO)) { $env:FOO = 'bar' }

Note: This newly defined environment variable will only exist for the current process and any child processes it creates (e.g., when you start a new PowerShell session from the ISE). Thanks, PetSerAl.

注意:此新定义的环境变量仅适用于当前进程及其创建的任何子进程(例如,从ISE启动新的PowerShell会话时)。谢谢,PetSerAl。

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

以下内容主要由Ansgar Wiechers提供,Mathias R. Jessen补充说:

If you want to define an environment variable persistently, you need to use the static SetEnvironmentVariable() method of the [System.Environment] class:

如果要持久定义环境变量,则需要使用[System.Environment]类的静态SetEnvironmentVariable()方法:

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

请注意,这些定义在将来的会话(进程)中生效,因此为了定义当前进程的变量,另外运行$ env:FOO ='bar',这实际上与[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.

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

See also.

也可以看看。