329

I'd like to modify the path to my application, but doing so breaks it because the service still points to the old location.

By going to Administrative Tools > Services you can open a properties dialog and view the Path to executable, but there is no way to change it.

Is there any way a user can modify the service path without having to reinstall the application ?

GenericJon
  • 8,746
  • 4
  • 39
  • 50

12 Answers12

355

There is also this approach seen on SuperUser which uses the sc command line instead of modifying the registry:

sc config <service name> binPath= <binary path>

Note: the space after binPath= is important. You can also query the current configuration using:

sc qc <service name>

This displays output similar to:

[SC] QueryServiceConfig SUCCESS

SERVICE_NAME: ServiceName

    TYPE               : 10  WIN32_OWN_PROCESS
    START_TYPE         : 2   AUTO_START
    ERROR_CONTROL      : 1   NORMAL
    BINARY_PATH_NAME   : C:\Services\ServiceName
    LOAD_ORDER_GROUP   :
    TAG                : 0
    DISPLAY_NAME       : <Display name>
    DEPENDENCIES       :
    SERVICE_START_NAME : user-name@domain-name
Community
  • 1
  • 1
Niall Connaughton
  • 15,518
  • 10
  • 52
  • 48
  • 30
    I would recommend this approach over direct registry changes. Many paths require quoted strings, which you can enter as follows, for example with MS SQL Server: `sc config mssqlserver binPath= "\"F:\SQL DATA\MSSQL10.MSSQLSERVER\MSSQL\Binn\sqlservr.exe\" -sMSSQLSERVER"` – Marc Durdin Nov 19 '14 at 23:30
  • 2
    It's also probably a bit nicer in a deployment script than the direct registry modification approach. – Niall Connaughton Nov 20 '14 at 00:35
  • Sweet. Cleaner than mucking around in reg. ** Note: You will need to close and open services.msc to see the change. – yonsk Dec 23 '15 at 10:36
  • 1
    If you get a `[SC] QueryServiceConfig FAILED 122:` after `sc qc X` use `sc qc X 1000` see [this](http://stackoverflow.com/a/19266092/4047679) – raphael Aug 02 '16 at 15:27
  • 40
    If using this from PowerShell, ensure you use `sc.exe`, not a naked `sc` – fiat Oct 23 '17 at 05:23
  • The `"\"something\""`syntax doesn't work before Windows Vista. – Sandburg Jun 05 '19 at 07:16
  • @fiat thank you! was wondering why it wasn't working – imsan Feb 09 '23 at 09:39
335

It involves editing the registry, but service information can be found in HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services. Find the service you want to redirect, locate the ImagePath subkey and change that value.

Matt McHugh
  • 3,995
  • 1
  • 15
  • 8
  • 4
    Although finally this leads to registry data, I should mention that there exist special Windows API functions to deal with the service. Direct registry modification should be avoided (because you can't be sure what else Windows is changing when it modifies the path to exe, f.e.) unless you are **absolutely** sure what you do. – lospejos Jul 22 '16 at 20:19
  • 4
    @lospejos "I should mention that there exist special Windows API functions to deal with the service" ... which would be? – Nick M Nov 25 '16 at 00:59
  • 2
    If someone don't see the service path updated in Services.msc, try killing mmc.exe "taskkill /F /IM mmc.exe" in cmd – Drag0nKn1ght May 18 '18 at 08:44
  • Windows services never requires you to edit anything in the registry. Using `sc` or `sc.exe` is way better and less risky – Arrcival Jan 18 '22 at 14:07
15

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
  • 20,442
  • 6
  • 58
  • 76
10

Open Run(win+R) , type "Regedit.exe" , to open "Registry Editor", go to

HKEY_LOCAL_MACHINE\System\CurrentControlSet\Services

find "Apache2.4" open the folder find the "ImagePath" in the right side, open "ImagePath" under "value Data" put the following path:

"C:\xampp\apache\bin\httpd.exe" -k runservice foe XAMPP for others point to the location where Apache is installed and inside locate the bin folder "C:(Apache installed location)\bin\httpd.exe" -k runservice

Antony Joseph
  • 101
  • 1
  • 2
5

Slight modification to this @CodeMaker 's answer, for anyone like me who is trying to modify a MongoDB service to use authentication.

When I looked at the "Path to executable" in "Services" the executed line already contained speech marks. So I had to make minor modification to his example.

To be specific.

  1. Type Services in Windows
  2. Find MongoDB (or the service you want to change) and open the service, making sure to stop it.
  3. Make a note of the Service Name (not the display name)
  4. Look up and copy the "Path to executable" and copy it.

For me the path was (note the speech marks)

"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg" --service

In a command line type

sc config MongoDB binPath= "<Modified string with \" to replace ">"

In my case this was

sc config MongoDB binPath= "\"C:\Program Files\MongoDB\Server\4.2\bin\mongod.exe\" --config \"C:\Program Files\MongoDB\Server\4.2\bin\mongod.cfg\" --service -- auth"
John Duskin
  • 355
  • 5
  • 12
  • The service I needed to change wasn't MongoDB; it was Redis. But your answer helped me, since I also needed to set some extra properties (`service-run` instead of `config`). – Ernani Dec 06 '21 at 13:25
2

You can't directly edit your path to execute of a service. For that you can use sc 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"
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
2

i just felt like adding for Git Bash users you should put the path in single quotes ' ' as in

sc config <service name> binPath='<binary path>'

in e.g. sc config MongoDB binPath='"C:\Program Files\MongoDB\Server\5.03\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\5.03\bin\mongod.cfg" --service --auth'

this worked for me to update the path of the service with Git Bash on Windows 10

Bender
  • 72
  • 6
  • Nice one. Worked great. But you have some extra '\' and '"' in your command. So maybe that's why the downvotes. The working syntax would be: sc config MongoDB binPath='"C:\Program Files\MongoDB\Server\5.03\bin\mongod.exe" --config "C:\Program Files\MongoDB\Server\5.03\bin\mongod.cfg\" --service --auth' – igs013 Feb 11 '22 at 09:03
  • i edited the command accordingly (although im pretty sure this worked for me the edited command works too) thank you ! – Bender Feb 12 '22 at 06:43
1

If you have Process Hacker installed, you can use it.

enter image description here

sean
  • 468
  • 5
  • 10
0

An alternative to using Invoke-WmiMethod is to use the newer CIM cmdlets. This also avoids the need for the @($null,$null...) object, as seen in a previous answer.

Get-CimInstance win32_service -Filter "Name='My Service'" | Invoke-CimMethod -MethodName Change -Arguments @{PathName="C:\Program Files\My Service\NewName.exe"}

Gary Pendlebury
  • 336
  • 5
  • 8
-1

A little bit deeper with 'SC' command, we are able to extract all 'Services Name' and got all 'QueryServiceConfig' :)

>SC QUERY > "%computername%-services.txt" [enter]

>FIND "SERVICE_NAME: " "%computername%-services.txt" /i > "%computername%-services-name.txt" [enter]

>NOTEPAD2 "%computername%-services-name.txt" [enter]

Do 'small' NOTEPAD2 editing.. Select 'SERVICE_NAME: ', CTRL+H, click 'Replace All' Imagine that we can do 'Replace All' within 'CMD'

Then, continue with 'CMD'..

>FOR /F "DELIMS= SKIP=2" %S IN ('TYPE "%computername%-services-name.txt"') DO @SC QC "%S" >> "%computername%-services-list-config.txt" [enter]

>NOTEPAD2 "%computername%-services-list-config.txt" [enter]

it is 'SERVICES on Our Machine' Raw data is ready for feeding 'future batch file' so the result is look like this below!!!

+ -------------+-------------------------+---------------------------+---------------+--------------------------------------------------+------------------+-----+----------------+--------------+--------------------+
| SERVICE_NAME | TYPE                    | START_TYPE                | ERROR_CONTROL | BINARY_PATH_NAME                                 | LOAD_ORDER_GROUP | TAG | DISPLAY_NAME   | DEPENDENCIES | SERVICE_START_NAME |
+ -------------+-------------------------+---------------------------+---------------+--------------------------------------------------+------------------+-----+----------------+--------------+--------------------+
+ WSearch      | 10  WIN32_OWN_PROCESS   | 2   AUTO_START  (DELAYED) | 1   NORMAL    | C:\Windows\system32\SearchIndexer.exe /Embedding | none             | 0   | Windows Search | RPCSS        | LocalSystem        |
+ wuauserv     | 20  WIN32_SHARE_PROCESS | 2   AUTO_START  (DELAYED) | 1   NORMAL    | C:\Windows\system32\svchost.exe -k netsvcs       | none             | 0   | Windows Update | rpcss        | LocalSystem        |

But, HTML will be pretty easier :D

Any bright ideas for improvement are welcome V^_^

Rhak Kahr
  • 740
  • 7
  • 10
-3

The best way for this scenario is to uninstall the application and reinstall the application. That is the right legal way.

last-Programmer
  • 985
  • 3
  • 9
  • 19
-4

You can delete the service:

sc delete ServiceName

Then recreate the service.

shA.t
  • 16,580
  • 5
  • 54
  • 111
Kim Wilson
  • 111
  • 1
  • 11