Kaseya Community

agent procedure to write/add to the 'path' environment variable

This question has suggested answer(s)

We have some new software, and one of the prerequisites is to write a value to the 'path' environment variable.  Obviously I do not want to overwrite what is there, just add to the list.  Has anyone come up with a good way of accomplishing this through an agent procedure?

Thanks for your assistance.

All Replies
  • Overall it's pretty straightforward to make a change to the path as it's stored in the registry.    I've not done it specifically with an agent procedure but I've done it with powershell before.

    The registry containing the "PATH" environment variable is HKLM\System\CurrentControlSet\Control\Session Manager\Environment\Path

    On newer systems that's a REG_EXPAND_SZ.  What I've done with powershell before is simply read the value into a variable, append what I need to it, and then write it back out.  It should be possible to do something similar with an agent procedure.

    The main caveat here is that changing the path via the registry like this doesn't actually take effect until the next system restart. (or if you kill explorer.exe and restart it.)

    Made it a suggested answered.
    [edited by: Jonathan at 7:30 AM (GMT -7) on Jul 12, 2018]
  • On any modern Windows platform:

    SETX PATH newpath_prefix;%PATH%;newpath_suffix /M

    /M sets the SYSTEM variable, otherwise it sets the CURRENT USER variable.

    Changing the registry will not update the active sessions. SetX will notify all apps of the change, making it take effect immediately. Command prompts will need a log-off/on because it doesn't respond to the env-update method. Much safer and faster than regedits.

    I run this from a script during onboarding to set the kworking\bin folder into the path, during offboarding to remove it from the path, and call it directly from executeShellCommand within procedures.