Setting Windows PowerShell path variable

2018-12-31 18:19发布

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 environment variables for PowerShell (v1)?

Note:

I want to make my changes permanent, so I don't have to set it every time I run PowerShell. Does PowerShell have a profile file? Something like Bash profile on Unix?

12条回答
看淡一切
2楼-- · 2018-12-31 18:51

Most answers aren't addressing UAC. This covers UAC issues.

First install PowerShell Community Extensions: choco install pscx via http://chocolatey.org/ (you may have to restart your shell environment).

Then enable pscx

Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser #allows scripts to run from the interwebs, such as pcsx

Then use Invoke-Elevated

Invoke-Elevated {Add-PathVariable $args[0] -Target Machine} -ArgumentList $MY_NEW_DIR
查看更多
不再属于我。
3楼-- · 2018-12-31 18:58

This sets the path for the current session and prompts the user to add it permanently:

function Set-Path {
    param([string]$x)
    $Env:Path+= ";" +  $x
    Write-Output $Env:Path
    $write = Read-Host 'Set PATH permanently ? (yes|no)'
    if ($write -eq "yes")
    {
        [Environment]::SetEnvironmentVariable("Path",$env:Path, [System.EnvironmentVariableTarget]::User)
        Write-Output 'PATH updated'
    }
}

You can add this function to your default profile, (Microsoft.PowerShell_profile.ps1), usually located at %USERPROFILE%\Documents\WindowsPowerShell.

查看更多
余生请多指教
4楼-- · 2018-12-31 19:01

If, some time during a PowerShell session, you need to append to the PATH environment variable temporarily, you can do it this way:

$env:Path += ";C:\Program Files\GnuWin32\bin"
查看更多
皆成旧梦
5楼-- · 2018-12-31 19:05

Changing the actual environment variables can be done by using the env: namespace / drive information. For example, this code will update the path environment variable:

$env:Path = "SomeRandomPath";

There are ways to make environment settings permanent, but if you are only using them from PowerShell, it's probably a lot better to use your profile to initiate the settings. On startup, PowerShell will run any .ps1 files it finds in the WindowsPowerShell directory under My Documents folder. Typically you have a profile.ps1 file already there. The path on my computer is

c:\Users\JaredPar\Documents\WindowsPowerShell\profile.ps1
查看更多
余生请多指教
6楼-- · 2018-12-31 19:06

From the PowerShell prompt:

setx PATH "$env:path;\the\directory\to\add" -m

You should then see the text:

SUCCESS: Specified value was saved.

Restart your session, and the variable will be available. setx can also be used to set arbitrary variables. Type setx /? at the prompt for documentation.

Before messing with your path in this way, make sure that you save a copy of your existing path by doing $env:path >> a.out in a PowerShell prompt.

查看更多
倾城一夜雪
7楼-- · 2018-12-31 19:06

As Jonathan Leaders mentioned here, it is important to run the command/script elevated to be able to change environment variables for 'machine', but running some commands elevated doesn't have to be done with the Community Extensions, so I'd like to modify and extend JeanT's answer in a way, that changing machine variables also can be performed even if the script itself isn't run elevated:

function Set-Path ([string]$newPath, [bool]$permanent=$false, [bool]$forMachine=$false )
{
    $Env:Path += ";$newPath"

    $scope = if ($forMachine) { 'Machine' } else { 'User' }

    if ($permanent)
    {
        $command = "[Environment]::SetEnvironmentVariable('PATH', $env:Path, $scope)"
        Start-Process -FilePath powershell.exe -ArgumentList "-noprofile -command $Command" -Verb runas
    }

}
查看更多
登录 后发表回答