1

I'm trying to add a variable to the system path via command line but can't find the variable for "system path"

If I run the following code:

setx path "%path%;C:\Python27\;C:\Python27\Scripts\"

It adds it to the local path, not the system path so I tried things like:

setx syspath "%syspath%;C:\Python27\;C:\Python27\Scripts\"
setx systempath "%systempath%;C:\Python27\;C:\Python27\Scripts\"

I can't find anything located at http://technet.microsoft.com/en-us/library/cc755104.aspx other than the ability to edit the local path variable.

I'm not looking for a way to do it via the GUI, I'm looking ONLY for a way to do it by code (command line hopefully)

In Autoit for example I can edit it via registry:

$SystemPath = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path")
If StringRight($SystemPath, 1) = ";" Then
    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_SZ", $SystemPath & "C:\Python27\;C:\Python27\Scripts\")
Else
    RegWrite("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment", "Path", "REG_SZ", $SystemPath & ";C:\Python27\;C:\Python27\Scripts\")
EndIf

or via GUI

Run("SystemPropertiesAdvanced.exe")
WinWait("System Properties")
ControlClick("System Properties", "Enviro&nment Variables...", "[CLASS:Button; INSTANCE:7]")
WinWait("Environment Variables")
ControlListView("Environment Variables", "", "[CLASS:SysListView32; INSTANCE:2]", "Select", ControlListView("Environment Variables", "", "[CLASS:SysListView32; INSTANCE:2]", "FindItem", "Path"))
ControlClick("Environment Variables", "", "[CLASS:Button; INSTANCE:7]")
WinWait("Edit System Variable")
$SystemPath = ControlGetText("Edit System Variable", "", "[CLASS:Edit; INSTANCE:2]")

If StringRight($SystemPath, 1) = ";" Then
    ControlSetText("", "", "", $SystemPath & "C:\Python27\;C:\Python27\Scripts\")
Else
    ControlSetText("", "", "", $SystemPath & ";C:\Python27\;C:\Python27\Scripts\")
EndIf

ControlClick("Edit System Variable", "", "[CLASS:Button; INSTANCE:1]")
ControlClick("Environment Variables", "", "[CLASS:Button; INSTANCE:9]")
ControlClick("System Properties", "", "[CLASS:Button; INSTANCE:8]")

1 Answer 1

2

From the linked documentation on switchs for setx command

/m Specifies to set the variable in the system environment. The default setting is the local environment.

Sign up to request clarification or add additional context in comments.

1 Comment

pathman, if available, is more appropriate than setx for manipulating the path.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.