Windows: How do I add a program to the system's path environmental variable from the command line?
Solution 1
By far, the easiest way to go about this is through the use of the setx
command, which is included in Windows 7/Server 2008 and up, or as part of the Windows Server 2003 Resource Kit for XP and Server 2003 systems.
You can use the setx
command to either specify an entirely new set of directories in the %PATH%
variable, or append a value, using a little extra logic. Say I wanted to add the directory at C:\stuff
to the %PATH%
. I would do so as below:
setx PATH "%PATH%,C:\stuff" /M
This appends ,C:\stuff
to the current path by overwriting the existing path with its current value, followed by ,C:\stuff
. The path environmental variable is comma delimited. The /M
switch makes the change in the HKLM (system-wide) registry hive, rather than the HKCU (the current user) registry hive.
You could throw this into a logon/startup script, or use the /s
switch to specify a remote server as the target, and make the changes from your workstation. For example, the below would add the stuff directory to the path on myserver.mydomain.com, with the credentials for the mydomainadmin
user.
setx /s myserver.mydomain.com /u mydomain\mydomainadmin /p mypassword PATH "%PATH%,C:\stuff" /M
The usual qualifications apply, mainly that changing a global environmental variable will only affect user sessions on their next logon, and will only apply to applications when they next check, which is usually at startup, so the easiest way to get this to apply to everything is to reboot the server, though if you know specifically what users or services need the change, you can take less disruptive measures to get the change applied.
Solution 2
By far, the easiest way to go about this is through the use of Group Policy Preferences Client Side Extensions, (GPP) which is included in Windows 7/Server 2008 and up, or as part of an update for XP and Server 2003 systems.
You can use the GPP settings to either specify an entirely new set of directories in the %PATH%
variable, a "Replace", or append a value using the "Create" Action preference item. Say I wanted to add the directory at C:\scripts
to the %PATH%
. I would do so as below:
This appends C:\scripts
to the current %PATH%
. The path environmental variable is semicolon delimited. Using the Computer Configuration section of the GPO makes the change in the HKLM (system-wide) registry hive, rather than the HKCU (the current user) registry hive.
You could throw this into an existing GPO, or use a seperate GPO to target specific workstations/users. For example, the below would add the scripts directory to the path only on the computer ISC-JSCOTT.
The usual qualifications apply. Changing a system environmental variable will only take effect at startup, so the easiest way to get this to apply to everything is to reboot.
Solution 3
The help for path gives the answer. Y:>path /? Displays or sets a search path for executable files.
PATH [[drive:]path[;...][;%PATH%] PATH ;
Type PATH ; to clear all search-path settings and direct cmd.exe to search only in the current directory. Type PATH without parameters to display the current path. Including %PATH% in the new path setting causes the old path to be appended to the new setting.
To add c:\stuff to path variable you run:
path c:\stuff;%path%
Related videos on Youtube
HopelessN00b
Updated on September 18, 2022Comments
-
HopelessN00b over 1 year
I'm looking to permanently add a directory to the
%PATH%
environmental available on a large number of Windows machines. Is there any way to do this from a command line (cmd
) so I can script up a solution, rather than having to use GUI on dozens of servers.How can I go about accomplishing this?
-
John Homer over 11 yearsWhile this method is completely valid, OP wanted to know how to do this from the command line for scripting purposes.
-
Harry Johnston over 11 yearsSome caution needs to be taken with this approach, because the path is stored in the registry as a REG_EXPAND_SZ type, meaning it can contain references to other environment variables. By default it only uses SystemRoot which is fixed, so losing the reference doesn't matter, but if the path might have already been modified you should check that there are no environment variable references in it before using this approach. There is an older tool named pathman.exe that may be an alternative.
-
Papooch about 3 yearsAre you sure the PATH string is comma delimited? It was semicolon delimited at least on all windows machines I've been working with. Maybe it depends on the system locale?