How to change path to executable for a Windows Service?

59

12

I want to change the path to the executable for a service from the command line.

How can I do that?

I want to run another .EXE from that service's path to executable.

sam

Posted 2010-12-15T06:24:52.377

Reputation: 809

If Dave's answer is not what you need, then please use the "edit" link to explain a bit more? – Arjan – 2010-12-15T23:13:11.663

Answers

75

You can use the sc config command to change the path a service points to:

SC CONFIG YourServiceName binPath= "C:\SomeDirectory\YourFile.EXE"

This will update the service called YourServiceName and change the "Path to Executable" entry to C:\SomeDirectory\YourFile.EXE. You will want to restart your service afterwards, which you can do with:

NET STOP YourServiceName & NET START YourServiceName

LittleBobbyTables - Au Revoir

Posted 2010-12-15T06:24:52.377

Reputation: 857

If you are moving MySQL config (.ini) to another drive. You can leave out the inner quotes. Just wrap the entire modified string in a single pair of double quotes. Moved my .ini and data folder to D: drive on Win7. – Brian Boatright – 2011-11-18T02:10:20.020

1What about when the Path to executable also contains quotes and other parameters? For example: "C:\Program Files\CollabNet\Subversion Server\svnserve.exe" --service -r "E:\Repositories" --listen-port "3690" I sadly had to go the regedit route for this one. – James Skemp – 2011-12-06T22:14:30.953

@James - I'm pretty sure you can escape those characters, but I'll have to play around with it and get back to you. – LittleBobbyTables - Au Revoir – 2011-12-08T03:41:49.950

@LittleBobbyTables Groovy. I was hoping simply swapping to single quotes would work (didn't think it would), but that wasn't the case, and I had to get it up and running asap. – James Skemp – 2011-12-08T13:06:10.707

6Would like to note there is a required space between binpath= and the command. Dumb, I know. – Chloe – 2012-09-07T16:26:20.317

2Quotes in binPath can be escaped with backslash: " – gwyn – 2013-03-21T14:14:46.543

17

You will need to do that in the registry:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services

Navigate to the service, then modify the image name to reflect the new path

As always, make a backup of the system PRIOR.

dave

Posted 2010-12-15T06:24:52.377

Reputation: 179

This worked better for me. The service I was changing had some complex arguments. – John Allers – 2015-07-10T17:57:47.400

2

You could also do it with PowerShell:

Get-WmiObject win32_service -filter "Name='My Service'" `
    | Invoke-WmiMethod -Name Change `
    -ArgumentList @($null,$null,$null,$null,$null, `
    "C:\Program Files (x86)\My Service\NewName.EXE")

Or:

Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\My Service" `
    -Name ImagePath -Value "C:\Program Files (x86)\My Service\NewName.EXE"

Greg Sansom

Posted 2010-12-15T06:24:52.377

Reputation: 827

1

The answer provided above works great, I can't reply to it, but to add up, in case you need to have quotes or other arguments in the path, say to fix an unquoted path vulnerability in the registry, like an imagepath, you can do the following from CMD as admin:

(e.g. for C:\Program Files (x86)\YourService\YourProcess.exe)

SC CONFIG YourService binPath= "\"C:\Program Files (x86)\YourService\YourProcess.exe\"

you can do the following from powershell as admin:

 SC.exe CONFIG YourService binPath= --% "\"C:\Program Files (x86)\YourService\YourProcess.exe\"

These will give you the following result:

"C:\Program Files (x86)\YourService\YourProcess.exe"

...bloody arguments and escaping parameters are a nightmare! Hope this helps someone in the future.

Maverick Sevmont

Posted 2010-12-15T06:24:52.377

Reputation: 11

0

You can't directly edit your path to execute of a service. For that you can use sc command,

Open your command prompt as administrator then type the following command,

SC CONFIG ServiceName binPath= "Path of your file"

Eg:

sc config MongoDB binPath="I:\Programming\MongoDB\MongoDB\bin\mongod.exe --config I:\Programming\MongoDB\MongoDB\bin\mongod.cfg --service"

Codemaker

Posted 2010-12-15T06:24:52.377

Reputation: 101

:- your looks the same as LittleBobbyTables, if you can detail difference please do. A read of [answer]and [tour] are always good every now and then. – mic84 – 2018-08-14T09:41:19.883