代码之家  ›  专栏  ›  技术社区  ›  g.pickardou

退出并继续脚本(例如获取路径环境更改效果)[duplicate]

  •  0
  • g.pickardou  · 技术社区  · 7 年前

    如果我有一个PowerShell ISE实例正在运行,并且我安装了一些修改路径的东西,或者我在PowerShell之外以任何方式对其进行了修改,那么我需要重新启动PowerShell,让它看到更新的路径变量。

    有没有一种方法可以在不重新启动的情况下从PowerShell中重新加载路径?

    0 回复  |  直到 7 年前
        1
  •  230
  •   Community Mohan Dere    9 年前

    只是为了带来 Rob's comment

    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") 
    
        2
  •  73
  •   Shay Levy    12 年前

    尝试获取机器路径并将其分配给会话的路径。

    $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine")
    
        3
  •  28
  •   Peter Mortensen Pieter Jan Bonestroo    7 年前

    最简单的方法,使用巧克力(免费软件)。它适用于CMD和PowerShell。然后,您将能够使用一个简单的命令重新加载路径(使用变量展开):

    refreshenv
    

    从cmd安装(需要管理员权限):

    @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"
    

    用法示例:

    > SET JAVA_HOME=c:/java/jdk6
    > SET PATH=%JAVA_HOME%/bin
    > ECHO %PATH%
    c:/java/jdk6/bin
    
    > SET JAVA_HOME=c:/java/jdk8
    > refreshenv
    Refreshing environment variables from registry for cmd.exe. Please wait...Finished..
    > echo %PATH%
    c:/java/jdk8/bin
    
        4
  •  6
  •   Peter Mortensen Pieter Jan Bonestroo    7 年前

    mpen's answer ,下面是一个PowerShell函数:

    function refresh-path {
        $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") +
                    ";" +
                    [System.Environment]::GetEnvironmentVariable("Path","User")
    }
    

    那就打电话吧 refresh-path .

        5
  •  4
  •   Hashbrown    7 年前

    为了添加到其他答案中,您可以通过过滤确保不添加多余的连接,以防用户有空路径。

    $env:Path=(
        [System.Environment]::GetEnvironmentVariable("Path","Machine"),
        [System.Environment]::GetEnvironmentVariable("Path","User")
    ) -match '.' -join ';'
    

    或者,更有用的是,如果您正在运行一个添加到不同或多个环境变量的脚本,请使用函数将它们全部重置

    function resetEnv {
        Set-Item `
            -Path (('Env:', $args[0]) -join '') `
            -Value ((
                [System.Environment]::GetEnvironmentVariable($args[0], "Machine"),
                [System.Environment]::GetEnvironmentVariable($args[0], "User")
            ) -match '.' -join ';')
    }
    resetEnv Path
    resetEnv AppPath
    
        6
  •  3
  •   Peter Mortensen Pieter Jan Bonestroo    7 年前

    如果路径中包含会话开始时未定义的环境变量,则还需要展开这些变量:

    $env:Path = [System.Environment]::ExpandEnvironmentVariables([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
    

    对我来说,这在安装NVM之后很有用,NVM定义并将%NVM\u HOME%添加到路径中。

    要得出逻辑结论,可以使用此递归函数展开:

    function Expand-EnvironmentVariablesRecursively($unexpanded) {
        $previous = ''
        $expanded = $unexpanded
        while($previous -ne $expanded) {
            $previous = $expanded
            $expanded = [System.Environment]::ExpandEnvironmentVariables($previous)
        }
        return $expanded
    }
    

    然后使用:

    $env:Path = Expand-EnvironmentVariablesRecursively([System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User"))
    

    我已经 opened an issue 将此溶液添加到 refreshenv 从巧克力。

    推荐文章