How to start a service with certain start parameters on Windows

How do I start a service with certain parameters? In another question I have found

net start servicename blah

but if I try this, net throws a syntax error at me.

What am I doing wrong?

Edit: To clarify net start servicename works just fine, but I need to pass parameters to the service. I can do this manually in services.msc by filling in a start parameter before starting the service. But how can I do this from a script?

Another edit: Sorry, but my question was misleading. In my tests, I had many more parameters and it's not the /blah net start complains about. In fact, anything starting with a slash is fine. So net start servicename /blah works, net start servicename blah doesn't work. Since I need net start servicename /foo bar, it's the bar that is the problem.


Solution 1:

sc start fooservice arg1 arg2 ...

Solution 2:

NET START won't allow you to pass arbitrary parameters as far as I know (it appears they have to be prefixed with a /), I believe the correct way is to modify the appropriate registry key and stop/start the service. Assuming your custom service is called MyService:

[HKLM\SYSTEM\CurrentControlSet\Services\MyService]
"ImagePath":C:\Projects\MyService\bin\MyService.exe Parameter1 Parameter2

Really though, a better way is to store your configuration somewhere the service knows about, like perhaps the registry or file system, and read whatever values you need. As you can see customizing the command line is a bit more painful than it should be.

If this is by chance a .NET developed service, you could create n copies of the executable on your file system, and have each automatically get the parameters from the appropriate app.config file.

It sounds like you may have stepped too far outside the Win32 box. Since this sounds like custom development any of the other offered solutions, while perhaps not your ideal, will work correctly and are a safer way to go.

Solution 3:

You need a slash before each parameter when using net start. Also make sure to quote parameters that have spaces.

net start servicename /param0 /"param1 with spaces" /"/param2_has_slash"