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.