Setting Windows PowerShell environment variables

前端 未结 18 1741
日久生厌
日久生厌 2020-11-22 11:03

I have found out that setting the PATH environment variable affects only the old command prompt. PowerShell seems to have different environment settings. How do I change the

18条回答
  •  伪装坚强ぢ
    2020-11-22 11:28

    You can also modify user/system environment variables permanently (i.e. will be persistent across shell restarts) with the following:

    Modify a system environment variable

    [Environment]::SetEnvironmentVariable
         ("Path", $env:Path, [System.EnvironmentVariableTarget]::Machine)
    

    Modify a user environment variable

    [Environment]::SetEnvironmentVariable
         ("INCLUDE", $env:INCLUDE, [System.EnvironmentVariableTarget]::User)
    

    Usage from comments - add to the system environment variable

    [Environment]::SetEnvironmentVariable(
        "Path",
        [Environment]::GetEnvironmentVariable("Path", [EnvironmentVariableTarget]::Machine) + ";C:\bin",
        [EnvironmentVariableTarget]::Machine)
    

    String based solution is also possible if you don't want to write types

    [Environment]::SetEnvironmentVariable("Path", $env:Path + ";C:\bin", "Machine")
    

提交回复
热议问题