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.
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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
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")
Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Services\My Service" ` -Name ImagePath -Value "C:\Program Files (x86)\My Service\NewName.EXE"
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.
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"
sc config MongoDB binPath="I:\Programming\MongoDB\MongoDB\bin\mongod.exe --config I:\Programming\MongoDB\MongoDB\bin\mongod.cfg --service"