6

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?

3 Answers 3

11

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.

2
  • 2
    Some 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. Commented Jan 10, 2013 at 21:03
  • Are 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?
    – Papooch
    Commented Feb 25, 2021 at 13:49
5

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:

PATH Properties

GPO summary

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.

Not sure why I used find, but there it is

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.

Please use groups instead of users or computers for filtering

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.

2
  • 1
    While this method is completely valid, OP wanted to know how to do this from the command line for scripting purposes.
    – John Homer
    Commented Jan 10, 2013 at 20:34
  • @JohnHomer I've only posted this as an alternative to the OP's self-answer. Personally, I think the OP's answer should be the accepted one. Still, GPP has much to offer, including many options previously only available via scripting. I must frequently remind myself to check GPP before I break out the logon scripts. :)
    – jscott
    Commented Jan 10, 2013 at 20:40
1

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%

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .