How to add a folder to `Path` environment variable in Windows 10 (with screenshots)
Asked Answered
C

2

69

On StackOverflow and on the net in general, there are outdated and few guides on how to add a specific folder to the Windows 10 Path environment variable of the user.

I think a complete guide for new developers with step by step instructions and screenshots could be really usefull to help them executing utilities from a Command Prompt without the need of the full path, simplifying the things.

Corinacorine answered 30/5, 2017 at 21:40 Comment(0)
C
126

For the guide below we want to add an example utility called mytool.exe which is located in C:\Users\NewFolderInPath\mytool.exe, so that everytime i want to execute the mytool utility i don't have to specify the full path.

I used this as an example, you can replace the folder with something more realistic like the JDK bin directory located here C:\Program Files\Java\{JDK_VERSION}\bin to execute javac, keytool or everything you want.

Step 1 - Click on the Windows icon

enter image description here

Step 2 - Click on the Settings icon

enter image description here

Step 3 - Click on System

enter image description here

Step 4 - Click on About

enter image description here

Step 5 - Click on System info

enter image description here

Step 6 - Click on Advanced system settings

enter image description here

Step 7 - Click on Environment variables...

enter image description here

Step 8 - Select Path row and then click Edit

enter image description here

Step 9 - Click New and then click Browse, then in the next panel which will open you need to select the folder you want in the Path. For the initial premise of this guide i will add the folder C:\Users\NewFolderInPath

enter image description here

Step 10 - Click OK and click every OK button you will encounter to close every previous windows.

enter image description here

Step 11 - Open a command prompt (cmd) and now you can execute your utility without specifying the full path.

enter image description here

Corinacorine answered 30/5, 2017 at 21:40 Comment(3)
In addition to this walkthrough this video was helpful - youtube.com/watch?v=extCL1UU5wk and so was this one youtube.com/watch?v=gBHOeI5QB8M in understanding what test command to try and that it wont work until you close out of command prompt and reopen it (looks like I have keytool running now :)Pork
tip - just type 'env' in the start menu - select 'edit environment variables' top item to skip half these stepsRegiment
If you type 'environment' into the start menu it also gives you the option for whether to edit variables for the current user of the system. The instructions above require admin privileges so for me end up editing the admin account's path instead of my regular user's account (my company's IT dept are sadists so I have multiple accounts).Cooker
C
16

DOS - A historical answer

For older systems, this is the way it was done and if you're working in in some flavor of Dos this is the easiest answer:

To print each entry of Windows PATH variable on a new line, execute:

C:\> echo %PATH:;=&echo.%

Set Windows PATH variable for the current session:

C:\> set PATH=%PATH%;C:\path\to\directory\

Set Windows PATH Permanently

Run as Administrator: The setx command is only available starting from Windows 7 and requires elevated command prompt.

Permanently add a directory to the user PATH variable:

C:\> setx path "%PATH%;C:\path\to\directory\"

Permanently add a directory to the system PATH variable (for all users):

C:\> setx /M path "%PATH%;C:\path\to\directory\"

PowerShell - To avoid problems on modern systems

On modern Windows there are more modern tools you can use to avoid some pitfalls that come with modern implementations of how the %PATH% variable is used.

What used to be user settings vs. system settings is now referred to as user vs machine settings. They are the same. User settings can be modified by anyone and only affects their own environment. Modification of Machine level settings require administrator privileges because they apply to all users.

To print each entry of Windows PATH variable on a new line, execute:

[Environment]::GetEnvironmentVariable("PATH", "User").Split(";")

[Environment]::GetEnvironmentVariable("PATH", "Machine").Split(";")

Set Windows PATH avoiding the pitfalls of setx method

The two main things we want to avoid are the character limit of 1024 and duplicate entries. Using the modern SetEnvironmentVariable will avoid the 1024 character limit so we could do a simple thing:

function Add-Path-User($newPath) {
    $Path = [Environment]::GetEnvironmentVariable("PATH", "User") + [IO.Path]::PathSeparator + $newPath
    [Environment]::SetEnvironmentVariable( "Path", $Path, "User" )
}

function Add-Path-Machine($newPath) {
    $Path = [Environment]::GetEnvironmentVariable("PATH", "Machine") + [IO.Path]::PathSeparator + $newPath
    [Environment]::SetEnvironmentVariable( "Path", $Path, "Machine" )
}

But if you want to avoid duplicates you would need something more involved:

Function Set-PathVariable {
    param (
        [string]$AddPath,
        [ValidateSet('Process', 'User', 'Machine')]
        [string]$Scope = 'Process'
    )

    $regexPaths = @()
    if ($PSBoundParameters.Keys -contains 'AddPath') {
        $regexPaths += [regex]::Escape($AddPath)
    }
    
    $arrPath = [System.Environment]::GetEnvironmentVariable('PATH', $Scope).Split([IO.Path]::PathSeparator)
    foreach ($path in $regexPaths) {
        $arrPath = $arrPath | Where-Object { $_ -notMatch "^$path\\?$" }
    }

    $value = ($arrPath + $addPath).Join([IO.Path]::PathSeparator)
    [System.Environment]::SetEnvironmentVariable('PATH', $value, $Scope)
}
Castello answered 29/9, 2021 at 22:2 Comment(5)
The echo trick is neat, but note that setx.exe is best avoided for persistent updates to the Path environment variables: while it may have no (immediate) ill effects, it can: setx.exe has a hard 1024-character limit, and replaces the original REG_EXPAND_SZ value with a REG_SZ value if the new value happens not to contain unexpanded environment-variable references. Additionally, you're duplicating entries by basing your new value on the process-level value of %PATH%, which is a composite of the system-level and user-level definition and only contains expanded values.Prurigo
It is an absolute NO GO - NEVER EVER using the command setx with local PATH value referenced with %PATH% to update persistent stored user or system environment variable PATH. That corrupts the user/system PATH value by creating duplicate folder paths, expanding environment variable references which could result in a program not working anymore, and truncation of PATH value to 1024 characters which means with loosing folder paths. Nobody should ever use the last two command lines.Jasun
Why are other folder paths also added to system PATH with SetX and not only the specified folder path? and What is the best practice for adding a directory to the PATH environment variable on Windows? These two answers offer more information why a modification of persistent stored user or system environment variable PATH should be best done never from the command line but by using the Environment Variables system configuration dialog window of Windows.Jasun
While I appreciate the nuance you have brought to the discussion I do feel your solution is over-engineered. It should be noted that the PATH=%PATH% pattern has been the industry standard since the 1980's. I have personally used this pattern that long, starting with DOS 3. I have never run into the issues you raise. They do appear to be modern problems requiring modern solutions. So while your solution is cool for the edge case it protects against, I cannot agree with NEVER EVER.Castello
@Castello It is absolutely no problem and indeed industry standard to modify local environment variable PATH of a running process. It is 100% okay using set "PATH=%PATH%;C:\OneMoreFolderPath" or set "PATH=C:\One more folder path;%PATH%" or the command PATH for doing the same in a command prompt window or a batch file for local PATH. But the modification of persistent stored user or system environment variable PATH with setx and usage of local PATH value referenced with %PATH% is a NO GO - NEVER EVER although posted unfortunately on many web pages.Jasun

© 2022 - 2024 — McMap. All rights reserved.