我发现设置PATH环境变量只影响旧的命令提示符。PowerShell似乎有不同的环境设置。如何更改PowerShell (v1)的环境变量?
注意:
我希望我的更改是永久性的,这样我就不必每次运行PowerShell时都设置它。PowerShell有配置文件吗?比如Unix上的Bash配置文件?
我发现设置PATH环境变量只影响旧的命令提示符。PowerShell似乎有不同的环境设置。如何更改PowerShell (v1)的环境变量?
注意:
我希望我的更改是永久性的,这样我就不必每次运行PowerShell时都设置它。PowerShell有配置文件吗?比如Unix上的Bash配置文件?
当前回答
有很多附加或覆盖的例子。下面是一个在powershell (Linux, Ubuntu 18.04和pwsh 7.1.3)上预先设置路径的示例
$ENV:PATH = "/home/linuxbrew/.linuxbrew/bin:$ENV:PATH"
我特别添加了linuxbrew (homebrew for linux) bin目录,使其优先于已安装的系统。它帮助我解决了一个问题,虽然这是最有帮助的地方,但它也让我“尝试”。
注意:是Linux的路径分隔符,而在Windows(或者至少是我的Windows)上,你会使用;通常是powershell。
其他回答
我试着优化了SBF和Michael的代码,使其更紧凑。
我依赖于PowerShell的类型强制,它会自动将字符串转换为enum值,所以我没有定义查找字典。
我还取出了根据条件将新路径添加到列表中的块,这样工作只完成一次,并存储在变量中以供重用。
然后,根据$PathContainer参数,它会被永久地应用到Session中。
我们可以将代码块放在函数或ps1文件中,直接从命令提示符调用。我选择了DevEnvAddPath.ps1。
param(
[Parameter(Position=0,Mandatory=$true)][String]$PathChange,
[ValidateSet('Machine', 'User', 'Session')]
[Parameter(Position=1,Mandatory=$false)][String]$PathContainer='Session',
[Parameter(Position=2,Mandatory=$false)][Boolean]$PathPrepend=$false
)
[String]$ConstructedEnvPath = switch ($PathContainer) { "Session"{${env:Path};} default{[Environment]::GetEnvironmentVariable('Path', $containerType);} };
$PathPersisted = $ConstructedEnvPath -split ';';
if ($PathPersisted -notcontains $PathChange) {
$PathPersisted = $(switch ($PathPrepend) { $true{,$PathChange + $PathPersisted;} default{$PathPersisted + $PathChange;} }) | Where-Object { $_ };
$ConstructedEnvPath = $PathPersisted -join ";";
}
if ($PathContainer -ne 'Session')
{
# Save permanently to Machine, User
[Environment]::SetEnvironmentVariable("Path", $ConstructedEnvPath, $PathContainer);
}
# Update the current session
${env:Path} = $ConstructedEnvPath;
我对DevEnvRemovePath.ps1做了类似的事情。
param(
[Parameter(Position=0,Mandatory=$true)][String]$PathChange,
[ValidateSet('Machine', 'User', 'Session')]
[Parameter(Position=1,Mandatory=$false)][String]$PathContainer='Session'
)
[String]$ConstructedEnvPath = switch ($PathContainer) { "Session"{${env:Path};} default{[Environment]::GetEnvironmentVariable('Path', $containerType);} };
$PathPersisted = $ConstructedEnvPath -split ';';
if ($PathPersisted -contains $PathChange) {
$PathPersisted = $PathPersisted | Where-Object { $_ -ne $PathChange };
$ConstructedEnvPath = $PathPersisted -join ";";
}
if ($PathContainer -ne 'Session')
{
# Save permanently to Machine, User
[Environment]::SetEnvironmentVariable("Path", $ConstructedEnvPath, $PathContainer);
}
# Update the current session
${env:Path} = $ConstructedEnvPath;
到目前为止,它们似乎是有效的。
在PowerShell中,可以通过输入以下命令导航到环境变量目录:
Set-Location Env:
这将把您带到Env:>目录。在这个目录中:
要查看所有环境变量,输入:
Env:\> Get-ChildItem
要查看特定的环境变量,输入:
Env:\> $Env:<variable name>, e.g. $Env:Path
要设置一个环境变量,输入:
Env:\> $Env:<variable name> = "<new-value>", e.g. $Env:Path="C:\Users\"
要删除一个环境变量,输入:
Env:\> remove-item Env:<variable name>, e.g. remove-item Env:SECRET_KEY
更多信息请参见关于环境变量。
更改实际的环境变量可以通过 使用env:命名空间/驱动器信息。例如,这个 代码将更新path环境变量:
$env:Path = "SomeRandomPath"; (replaces existing path)
$env:Path += ";SomeRandomPath" (appends to existing path)
让改变永久化
有办法让环境设置永久存在,但是 如果你只从PowerShell中使用它们,它可能 使用Powershell配置文件脚本更好。
每次Powershell的一个新实例启动时,它都会查找特定的脚本文件(称为配置文件),如果它们存在,就执行它们。您可以编辑这些概要文件中的一个来定制您的环境。
要了解这些配置文件脚本在您的计算机中的位置,请键入:
$profile
$profile.AllUsersAllHosts
$profile.AllUsersCurrentHost
$profile.CurrentUserAllHosts
$profile.CurrentUserCurrentHost
你可以编辑其中一个,例如,输入:
notepad $profile
如果在PowerShell会话期间,您需要查看或临时修改PATH环境变量,您可以键入以下命令之一:
$env:Path # shows the actual content
$env:Path = 'C:\foo;' + $env:Path # attach to the beginning
$env:Path += ';C:\foo' # attach to the end
你也可以用以下方法永久地修改用户/系统环境变量(即在shell重新启动时保持不变):
修改系统环境变量
[Environment]::SetEnvironmentVariable
("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
修改用户环境变量
[Environment]::SetEnvironmentVariable
("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)
使用注释-添加到系统环境变量
[Environment]::SetEnvironmentVariable(
"Path",
[Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin",
[EnvironmentVariableTarget]::Machine)
如果你不想写类型,基于字符串的解决方案也是可能的
[Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\bin", "Machine")