我发现设置PATH环境变量只影响旧的命令提示符。PowerShell似乎有不同的环境设置。如何更改PowerShell (v1)的环境变量?

注意:

我希望我的更改是永久性的,这样我就不必每次运行PowerShell时都设置它。PowerShell有配置文件吗?比如Unix上的Bash配置文件?


当前回答

在@ali Darabi的回答中编辑注册表键对我来说是最好的,但是 当我没有Powershell的权限时。所以我直接在regedit里编辑。

我想在这个回答中进一步展开这个主题。

重新启动Powershell也不足以传播更改。我必须打开任务管理器并重新启动explorer.exe以触发注册表的重新加载。

导航注册表可能相当乏味,所以为了保持用户友好的体验,你可以从Powershell执行这个:

REG ADD "HKCU\Software\Microsoft\Windows\CurrentVersion\Applets\Regedit" /v "LastKey" /d "HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment" /f; regedit

它将最后打开的窗口设置为某个注册表路径,这样当您下次打开regedit时,它将以适当的键打开。

其他回答

在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

更多信息请参见关于环境变量。

这些脚本是幂等的(可以运行多次)。 它们更新Windows路径和当前/未来的Powershell会话:

永久添加路径

    $targetDir="c:\bin"
    $oldPath = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    $oldPathArray=($oldPath) -split ';'
    if(-Not($oldPathArray -Contains "$targetDir")) {
        write-host "Adding $targetDir to Machine Path"
        $newPath = "$oldPath;$targetDir" -replace ';+', ';'
        [System.Environment]::SetEnvironmentVariable("Path",$newPath,"Machine")
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","User"),[System.Environment]::GetEnvironmentVariable("Path","Machine") -join ";"
    }
    write-host "Windows paths:"
    ($env:Path).Replace(';',"`n")

永久删除路径

    $targetDir="c:\bin"
    $oldPath = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    $oldPathArray=($oldPath) -split ';'
    if($oldPathArray -Contains "$targetDir") {
        write-host "Removing $targetDir from Machine path"
        $newPathArray = $oldPathArray | Where-Object { $_ –ne "$targetDir" }
        $newPath = $newPathArray -join ";"
        [System.Environment]::SetEnvironmentVariable("Path",$newPath,"Machine")
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","User"),[System.Environment]::GetEnvironmentVariable("Path","Machine") -join ";"
    }
    write-host "Windows paths:"
    ($env:Path).Replace(';',"`n")

有很多附加或覆盖的例子。下面是一个在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。

如果在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

更改实际的环境变量可以通过 使用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