Windows: Modifying the Command Line Path

For command line environments the default locations where the terminal will look for executable programs is called the Path. The path may have several directories noted in it, meaning that all of the noted locations will be searched for executable files to match the command that you’re issuing. The purpose of the path is to save system users the time it would take to type a full file path to each executable file they want to run. Path is the reason why, when you type ‘cmd’ or ‘ipconfig’ or any other command, your command runs even though you did not specify C:\Windows\System32 in front of it (which is the directory where many commands live).

When you create your own programs or commands certainly you would like to run them without needing to specify the file path to them, and you can do so by modifying the path variable to include the directory where you store your own programs. Certainly you could also just place your programs in the C:\Windows\System32 folder, however that is not recommended for several reasons:

  • Modifying the contents of this folder (including adding or removing files) requires system administrator permission. Modifying the path variable allows normal system users to enjoy the benefits of path too (though non-admins should modify the non-system version of the path variable instead).
  • The C:\Windows\System32 folder is meant for Windows system files - files that are provided with an installation of Windows. Including your own programs breaks that file organization. Windows updates will also assume that the Windows folder does not contain user files, so future updates or restores could overwrite your files stored in this location.
  • Other file system locations are more suitable, such as: C:\Program Files or C:\Users\<my user>\Programs

As an example, let’s add the C:\Program Files\Scripts directory to the system path variable, by using Windows Powershell. C:\Program Files\Scripts is not located in a user accessible area - you must be a system administrator to modify the contents of the C:\Program Files directory. However, I am the system administrator and I also want to ensure that the script that I am including in this directory is available to all users. If the script were placed in C:\Users\<my user>\Programs (or similar) then only I would be able to access it. However I would use C:\Users\<my user>\Programs or similar if I were working on a system that I did not have administrator permissions to.

Note

The command below comes in two parts: setting the value of the path variable in my local Powershell session, then committing that value to the system path variable permanently

$env:path += ";C:\Program Files\Scripts\"
[Environment]::SetEnvironmentVariable('Path',$env:path,[System.EnvironmentVariableTarget]::Machine)

That’s it! Note the use of the += operator which tells Powershell to add the string that I specified to the existing value of $env:path, rather than removing the previous value of $env:path and replacing it. This preserves what was in the path before (things like C:\Windows\System32) so that the other programs in the system don’t break. That is also the point of the ; at the start of the string - each file path stored in the path variable is separated by a semicolon, but typically there isn’t one already on the end of the existing path, so we add that first.

System Properties GUI

Also keep in mind that you can edit the system path variable through the System Properties window, on the Advanced tab, in the Environment Variables window. You can get to these windows through the File Explorer, by right-clicking on ‘This PC’ and then select Properties, then find ‘Advanced system settings’ at the bottom of the page. Alternatively you can open a run box and run the command systempropertiesadvanced to jump right there.