我感到惊讶的是,在谷歌搜索之后我没有得到这个常见情况的答案......
如果环境变量不存在,如何在PowerShell中设置?
答案 0 :(得分:38)
以下代码定义了环境变量FOO
,如果它尚不存在。
if (-not (Test-Path env:FOO)) { $env:FOO = 'bar' }
注意:此新定义的环境变量仅适用于当前进程以及它创建的任何子进程(例如,当您从ISE启动新的PowerShell会话时)。 谢谢,PetSerAl。
以下内容主要由Ansgar Wiechers提供,并由Mathias R. Jessen补充:
如果您想持久定义环境变量 ,则需要使用SetEnvironmentVariable()
类的静态[System.Environment]
方法:
# user environment
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'User')
# system environment (requires admin privileges)
[Environment]::SetEnvironmentVariable('FOO', 'bar', 'Machine')
请注意,这些定义在 future 会话(进程)中生效,因此按顺序定义当前进程的变量< / em> ,另外运行$env:FOO = 'bar'
,这与[Environment]::SetEnvironmentVariable('FOO', 'bar', 'Process')
实际上相同。
将[Environment]::SetEnvironmentVariable()
与User
或Machine
一起使用时,会向其他应用程序发送WM_SETTINGCHANGE
消息,以通知他们此更改(尽管很少有应用程序对此做出反应)这样的通知)
这在定位Process
时(或在分配给$env:FOO
时)不适用,因为无论如何其他应用程序(进程)都无法看到该变量。
答案 1 :(得分:0)
代码
function Set-LocalEnvironmentVariable {
param (
[Parameter()]
[System.String]
$Name,
[Parameter()]
[System.String]
$Value,
[Parameter()]
[Switch]
$Append
)
if($Append.IsPresent)
{
if(Test-Path "env:$Name")
{
$Value = (Get-Item "env:$Name").Value + $Value
}
}
Set-Item env:$Name -Value "$value" | Out-Null
}
function Set-PersistentEnvironmentVariable {
param (
[Parameter()]
[System.String]
$Name,
[Parameter()]
[System.String]
$Value,
[Parameter()]
[Switch]
$Append
)
Set-LocalEnvironmentVariable -Name $Name -Value $Value -Append:$Append
if ($Append.IsPresent) {
$value = (Get-Item "env:$Name").Value
}
if ($IsWindows) {
setx "$Name" "$Value" | Out-Null
return
}
$pattern = "\s*export[ \t]+$Name=[\w]*[ \t]*>[ \t]*\/dev\/null[ \t]*;[ \t]*#[ \t]*$Name\s*"
if ($IsLinux) {
$file = "~/.bash_profile"
$content = (Get-Content "$file" -ErrorAction Ignore -Raw) + [System.String]::Empty
$content = [System.Text.RegularExpressions.Regex]::Replace($content, $pattern, [String]::Empty);
$content += [System.Environment]::NewLine + [System.Environment]::NewLine + "export $Name=$Value > /dev/null ; # $Name"
Set-Content "$file" -Value $content -Force
return
}
if ($IsMacOS) {
$file = "~/.zprofile"
$content = (Get-Content "$file" -ErrorAction Ignore -Raw) + [System.String]::Empty
$content = [System.Text.RegularExpressions.Regex]::Replace($content, $pattern, [String]::Empty);
$content += [System.Environment]::NewLine + [System.Environment]::NewLine + "export $Name=$Value > /dev/null ; # $Name"
Set-Content "$file" -Value $content -Force
return
}
throw "Invalid platform."
}
在 Windows 上,您可以使用:
在 Linux 上,我们可以将 export VARIABLE_NAME=Variable value 添加到文件 ~/.bash_profile。对于新的 bash 终端,该进程执行位于 ~/.bash_profile 中的这些指令。
在类似于 Linux 的 MacOS 上,但如果你有 zsh 终端,文件是 .zprofile,如果默认终端是 bash,文件是 .bash_profile。在我的函数代码中,如果您愿意,我们需要添加默认终端的检测。我假设默认终端是 zsh。
示例
#Set "Jo" value to variable "NameX", this value is accesible in current process and subprocesses, this value is accessible in new opened terminal.
Set-PersistentEnvironmentVariable -Name "NameX" -Value "Jo"; Write-Host $env:NameX
#Append value "ma" to current value of variable "NameX", this value is accesible in current process and subprocesses, this value is accessible in new opened terminal.
Set-PersistentEnvironmentVariable -Name "NameX" -Value "ma" -Append; Write-Host $env:NameX
#Set ".JomaProfile" value to variable "ProfileX", this value is accesible in current process/subprocess.
Set-LocalEnvironmentVariable "ProfileX" ".JomaProfile"; Write-Host $env:ProfileX
输出
参考资料
检查About Environment Variables
Shell initialization files
ZSH: .zprofile, .zshrc, .zlogin - What goes where?