Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

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.

share|improve this answer
1  
While this method is completely valid, OP wanted to know how to do this from the command line for scripting purposes. –  John Homer Jan 10 '13 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 Jan 10 '13 at 20:40
add comment
up vote 9 down vote accepted

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.

share|improve this answer
1  
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. –  Harry Johnston Jan 10 '13 at 21:03
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.