Adding a directory to the PATH environment variable in Windows

前端 未结 18 1334
我在风中等你
我在风中等你 2020-11-21 05:13

I am trying to add C:\\xampp\\php to my system PATH environment variable in Windows.

I have already added it using the Environment Varia

相关标签:
18条回答
  • 2020-11-21 05:58

    WARNING: This solution may be destructive to your PATH, and the stability of your system. As a side effect, it will merge your user and system PATH, and truncate PATH to 1024 characters. The effect of this command is irreversible. Make a backup of PATH first. See the comments for more information.

    Don't blindly copy-and-paste this. Use with caution.

    You can permanently add a path to PATH with the setx command:

    setx /M path "%path%;C:\your\path\here\"
    

    Remove the /M flag if you want to set the user PATH instead of the system PATH.

    Notes:

    • The setx command is only available in Windows 7 and later.
    • You should run this command from an elevated command prompt.

    • If you only want to change it for the current session, use set.

    0 讨论(0)
  • 2020-11-21 05:59

    I would use PowerShell instead!

    To add a directory to PATH using PowerShell, do the following:

    $PATH = [Environment]::GetEnvironmentVariable("PATH")
    $xampp_path = "C:\xampp\php"
    [Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path")
    

    To set the variable for all users, machine-wide, the last line should be like:

    [Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
    

    In a PowerShell script, you might want to check for the presence of your C:\xampp\php before adding to PATH (in case it has been previously added). You can wrap it in an if conditional.

    So putting it all together:

    $PATH = [Environment]::GetEnvironmentVariable("PATH")
    $xampp_path = "C:\xampp\php"
    if( $PATH -notlike "*"+$xampp_path+"*" ){
        [Environment]::SetEnvironmentVariable("PATH", "$PATH;$xampp_path", "Machine")
    }
    

    Better still, one could create a generic function. Just supply the directory you wish to add:

    function AddTo-Path{
    param(
        [string]$Dir
    )
    
        if( !(Test-Path $Dir) ){
            Write-warning "Supplied directory was not found!"
            return
        }
        $PATH = [Environment]::GetEnvironmentVariable("PATH")
        if( $PATH -notlike "*"+$Dir+"*" ){
            [Environment]::SetEnvironmentVariable("PATH", "$PATH;$Dir", "Machine")
        }
    }
    

    You could make things better by doing some polishing. For example, using Test-Path to confirm that your directory actually exists.

    0 讨论(0)
  • 2020-11-21 06:00

    This only modifies the registry. An existing process won't use these values. A new process will do so if it is started after this change and doesn't inherit the old environment from its parent.

    You didn't specify how you started the console session. The best way to ensure this is to exit the command shell and run it again. It should then inherit the updated PATH environment variable.

    0 讨论(0)
  • 2020-11-21 06:05

    Option 1

    After you change PATH with the GUI, close and re-open the console window.

    This works because only programs started after the change will see the new PATH.

    Option 2

    Execute this command in the command window you have open:

    set PATH=%PATH%;C:\your\path\here\
    

    This command appends C:\your\path\here\ to the current PATH.

    Breaking it down:

    • set – A command that changes cmd's environment variables only for the current cmd session; other programs and the system are unaffected.
    • PATH= – Signifies that PATH is the environment variable to be temporarily changed.
    • %PATH%;C:\your\path\here\ – The %PATH% part expands to the current value of PATH, and ;C:\your\path\here\ is then concatenated to it. This becomes the new PATH.
    0 讨论(0)
  • 2020-11-21 06:05

    Handy if you are already in the directory you want to add to PATH:

    set PATH=%PATH%;%CD%
    

    It works with the standard Windows cmd, but not in PowerShell.

    For PowerShell, the %CD% equivalent is [System.Environment]::CurrentDirectory.

    0 讨论(0)
  • 2020-11-21 06:06

    Use pathed from gtools.

    It does things in an intuitive way. For example:

    pathed /REMOVE "c:\my\folder"
    pathed /APPEND "c:\my\folder"
    

    It shows results without the need to spawn a new cmd!

    0 讨论(0)
提交回复
热议问题