Adding a directory to the PATH environment variable in Windows

前端 未结 18 1229
我在风中等你
我在风中等你 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 06:07

    Safer SETX

    Nod to all the comments on the @Nafscript's initial SETX answer.

    • SETX by default will update your user path.
    • SETX ... /M will update your system path.
    • %PATH% contains the system path with the user path appended

    Warnings

    1. Backup your PATH - SETX will truncate your junk longer than 1024 characters
    2. Don't call SETX %PATH%;xxx - adds the system path into the user path
    3. Don't call SETX %PATH%;xxx /M - adds the user path into the system path
    4. Excessive batch file use can cause blindness1

    The ss64 SETX page has some very good examples. Importantly it points to where the registry keys are for SETX vs SETX /M

    User Variables:

    HKCU\Environment

    System Variables:

    HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

    Usage instructions

    Append to User PATH

    append_user_path.cmd

    @ECHO OFF
    REM usage: append_user_path "path"
    SET Key="HKCU\Environment"
    FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
    ECHO %CurrPath% > user_path_bak.txt
    SETX PATH "%CurrPath%";%1
    

    Append to System PATH

    append_system_path.cmd. Must be run as administrator.

    (It's basically the same except with a different Key and the SETX /M modifier.)

    @ECHO OFF
    REM usage: append_system_path "path"
    SET Key="HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
    FOR /F "usebackq tokens=2*" %%A IN (`REG QUERY %Key% /v PATH`) DO Set CurrPath=%%B
    ECHO %CurrPath% > system_path_bak.txt
    SETX PATH "%CurrPath%";%1 /M
    

    Alternatives

    Finally there's potentially an improved version called SETENV recommended by the ss64 SETX page that splits out setting the user or system environment variables.


    1. Not strictly true

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

    You don't need any set or setx command. Simply open the terminal and type:

    PATH
    

    This shows the current value of PATH variable. Now you want to add directory to it? Simply type:

    PATH %PATH%;C:\xampp\php
    

    If for any reason you want to clear the PATH variable (no paths at all or delete all paths in it), type:

    PATH ;
    

    Update

    Like Danial Wilson noted in comment below, it sets the path only in the current session. To set the path permanently, use setx but be aware, although that sets the path permanently, but not in the current session, so you have to start a new command line to see the changes. More information is here.

    To check if an environmental variable exist or see its value, use the ECHO command:

    echo %YOUR_ENV_VARIABLE%
    
    0 讨论(0)
  • 2020-11-21 06:12

    Aside from all the answers, if you want a nice GUI tool to edit your Windows environment variables you can use Rapid Environment Editor.

    Try it! It's safe to use and is awesome!

    0 讨论(0)
  • 2020-11-21 06:14
    1. I have installed PHP that time. Extracted php-7***.zip into C:\php\
    2. Backup my current PATH environment variable: run cmd, and execute command: path >C:\path-backup.txt

    3. Get my current path value into C:\path.txt file (same way)

    4. Modify path.txt (sure, my path length is more than 1024 chars, windows is running few years)
      • I have removed duplicates paths in there, like 'C:\Windows; or C:\Windows\System32; or C:\Windows\System32\Wbem; - I've got twice.
      • Remove uninstalled programs paths as well. Example: C:\Program Files\NonExistSoftware;
      • This way, my path string length < 1024 :)))
      • at the end of path string add ;C:\php\
      • Copy path value only into buffer with framed double quotes! Example: "C:\Windows;****;C:\php\" No PATH= should be there!!!
    5. Open Windows PowerShell as Administrator.
    6. Run command:

    setx path "Here you should insert string from buffer (new path value)"

    1. Re-run your terminal (I use "Far manager") and check: php -v
    0 讨论(0)
  • 2020-11-21 06:18
    • Command line changes will not be permanent and will be lost when the console closes.
    • The path works like first comes first served.
    • You may want to override other already included executables. For instance, if you already have another version on your path and you want to add different version without making a permanent change on path, you should put the directory at the beginning of the command.

    To override already included executables;

    set PATH=C:\xampp\php;%PATH%;

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

    A better alternative to Control Panel is to use this freeware program from SourceForge called Pathenator.

    However, it only works for a system that has .NET 4.0 or greater such as Windows 7, Windows 8, or Windows 10.

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