Search code examples
.netwindowsbatch-filewindows-services

Set Environment Variable for Windows Service


I have a batch job that kicks off a windows service

sc serverName start serviceName

I'd like to set an environment variable for that service when I kick it off, similar to how you would do it for a subprocess. How can I do that with a windows service?

To clarify, I don't want to set any global environment variables, just one for that particular process. And I don't have access to the actual Service code.


Solution

  • Adapted from this answer on Server Fault:

    Add a multi-string "Environment" value to the service's registry entry to the key at HKLM\SYSTEM\CurrentControlSet\Services\SERVICE_NAME. Each string value should be a name=value pair, where the name is the environment variable name you want the service to have, and the value is, well, the environment variable's value.

    For example, if you want your service to use a different TEMP and TMP environment variables, you would enter TEMP=VALUE as one string and TMP=VALUE as another string.

    Restart the service for it to get the new environment variables.