76

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.

I say Reinstate Monica
  • 25,487
  • 19
  • 95
  • 131
sam
  • 1,007
  • 2
  • 9
  • 6
  • 1
    If Dave's answer is not what you need, then please use the "edit" link to explain a bit more? – Arjan Dec 15 '10 at 23:13

5 Answers5

101

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
  • 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 Nov 18 '11 at 02:10
  • 1
    What 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 Dec 06 '11 at 22:14
  • @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 Dec 08 '11 at 03:41
  • @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 Dec 08 '11 at 13:06
  • 8
    Would like to note there is a required space between binpath= and the command. Dumb, I know. – Chloe Sep 07 '12 at 16:26
  • 6
    Quotes in binPath can be escaped with backslash: \" – gwyn Mar 21 '13 at 14:14
20

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.

Nakilon
  • 952
  • 2
  • 13
  • 22
dave
  • 209
  • 1
  • 2
6

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.

  • An alternative PowerShell command without the stop-parsing token is SC.exe CREATE YourService binPath= "\\`"$env:SERVICE_HOME\bin\srvc.exe\\`" -param \\`"SOME_PATH\\`"" – honzajscz Feb 11 '22 at 08:47
3

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
  • 907
  • 7
  • 13
  • This is one of those rare cases where I prefer the `cmd` version better than Powershell. All those `$null`s and hardcoded registry paths make me uneasy; it's too easy to miss one, or for that path to be munged somewhere down the line. The `cmd` method is much cleaner IMO. – Max Cascone May 26 '21 at 05:33
  • There's two different ways to do it in this answer. The second example doesn't have the $null parameters that worry you, and it's trivial to replace the hardcoded path with a variable. – Greg Sansom May 27 '21 at 04:20
1

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"
  • :- 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 Aug 14 '18 at 09:41