When creating a service with sc.exe how to pass in context parameters?

how can arguments be passed to the Installer class's Context.Parameters collection? My reading of the sc.exe documentation is that such arguments could only be passed on the end of binPath , but I have not found an example or been able to successfully do this.

2,173 2 2 gold badges 18 18 silver badges 30 30 bronze badges asked Sep 7, 2010 at 22:54 sympatric greg sympatric greg 3,089 2 2 gold badges 25 25 silver badges 30 30 bronze badges

A glance at the Services key in the Registry suggests that any needed parameters are included with the ImagePath value, so your binPath= "c:\abc\def.exe /Param1=ghi" seems like the right idea. Do the backslashes need to be escaped (i.e. "c:\\abc\\. ")? Worst thing, you could directly edit the Registry value afterwards if SC.EXE can't do it.

Commented Sep 8, 2010 at 14:37

I gave up on sc.exe and am using installutil.exe like so: Installutil.exe /ServiceName=”TheName” /targetdir=”C:\TheInstallDirectory\” /PackageRoot=”PackageRootPath”

Commented Sep 8, 2010 at 16:45 I used Installutil.exe and for older technology I use Instsrv.exe from Windows XP/2003 Resource Ket. Commented Feb 25, 2011 at 18:58

14 Answers 14

sc create binpath= "" [option1] [option2] [optionN] 

The trick is to leave a space after the = in your create statement, and also to use " " for anything containing special characters or spaces.

It is advisable to specify a Display Name for the service as well as setting the start setting to auto so that it starts automatically. You can do this by specifying DisplayName= yourdisplayname and start= auto in your create statement.

Here is an example:

C:\Documents and Settings\Administrator> sc create asperacentral binPath= "C:\Program Files\Aspera\Enterprise Server\bin\Debug\asperacentral.exe" DisplayName= "Aspera Central" start= auto 

If this worked you should see:

[SC] CreateService SUCCESS 

UPDATE 1

688 2 2 gold badges 7 7 silver badges 20 20 bronze badges answered May 6, 2012 at 16:15 5,097 3 3 gold badges 23 23 silver badges 29 29 bronze badges

Keep in mind that the space after binPath= ( binPath= "C:\. " ) needs to be present, or else this won't work.

Commented Apr 22, 2013 at 14:48

start= auto is an important one, so after reboot the service will be automatically started. Very good in case the end user is not an expert

Commented Jan 29, 2014 at 12:54

Also if you need to pass extra parameters in the binPath that require quotes they have to be escaped ( \" ) example: if the path was c:\some long path\some.exe "first argument" it would need to be binPath= "\"c:\some long path\some.exe\" \"first argument\""

Commented Jul 16, 2015 at 13:38

If you don't have a space after the "=" in your args (like binPath= . and DisplayName= . ; in my case I forgot the "=" after DisplayName), then the console will print the usage instructions for the create command; like: DESCRIPTION: Creates a service entry. USAGE: sc create. etc

Commented Feb 28, 2016 at 18:02 The spaces after the "=" is very important Commented Apr 6, 2018 at 16:19

Parameters for created services have some peculiar formating issues, in particular if the command includes spaces or quotes:

If you want to enter command line parameters for the service, you have to enclose the whole command line in quotes. (And always leave a space after binPath= and before the first quote, as mrswadge pointed out)

So, to create a service for the command PATH\COMMAND.EXE --param1=xyz you would use the following binPath parameter:

binPath= "PATH\COMMAND.EXE --param1=xyz" ^^ ^ || | space quote quote 

If the path to the executable contains spaces, you have to enclose the path in quotes.

So for a command that has both parameters and a path with spaces, you need nested quotes. You have to escape the inner quotes with backslashes \" . The same holds if the parameters themselves contain quotes, you will need to escape those too.

Despite using backslashes as escape characters, you do not have to escape the regular backslashes contained in the path. This is contrary to how you normally use backslashes as escape characters.

So for a command like
"PATH WITH SPACES \COMMAND.EXE" --param-with-quotes="a b c" --param2 :

binPath= "\"PATH WITH SPACES \COMMAND.EXE\" --param-with-quotes=\"a b c\" --param2" ^ ^ ^ ^ ^ ^ ^ | | | | | | | opening escaped regular escaped escaped closing quote quote backslash closing quotes quote for for in quote for for whole path path for path parameter whole command command 

Here is a concrete example from the SVNserve documentation, which shows all special cases:

sc create svnserve binpath= "\"C:\Program Files\CollabNet Subversion Server\svnserve.exe\" --service -r \"C:\my repositories\" " displayname= "Subversion Server" depend= Tcpip start= auto 

(linebreaks are added for readability, do not include them)

This would add a new service with the command line "C:\Program Files\CollabNet Subversion Server\svnserve.exe" --service -r "C:\my repositories" .

So in summary