Create a new windows service by using the sc command or windows powershell

To create a service from an executable we can use the command prompt or windows powershell. This can be done on both desktop and server editions of Microsoft Windows. I encountered this problem when I had to add an application developed to work over IIS. The thing was that this application had to start automatic and to work as a service.
If you are using command prompt to begin type the following sc ?:

sc command command prompt

If you read the description of the command you will see that sc is used to manipulate services that work under Windows. To add an application as a service first create a local folder on you partition that includes that desired executable:
windows service
I have created a folder called Service1 in the D: partition and inside it i have added the Service1.exe executable.
After the folder with the executable was created enter in command prompt the following sc create ?:
sc command
You will see that this command creates a service in registry(HKLM\SYSTEM\Control Sets) and also in Service Database. OK, now type the following:
sc create Service1 binPath= “D:\Service1\Service1.exe” DisplayName= “Service1” start= auto
The parameters used in this command:
binPath – is the path where the executable is located
DisplayName – how the service is seen in services manager (services.msc)
start – startup type of the service
Now use the sc query or sc querex (extended) to verify that your service was created.
If your service is not started type the following sc start [servicename]:
sc query command
example: sc start Service1
Now let’s try the same with windows powershell. First type the following get-help get-service to read something about this command.
get-service cmdlet
As you can see it is used to obtain information about a local or remote service. Now type simple get-help get-service to see all the services on your machine:
get-service Powershell
OK, now let’s create the service. Type get-help new-service. The new-service cmdlet is used to create a new service. You can also read the online version from microsoft website:
Now type the following:
C:\PS>new-service -name Service1 -path “D:\Service1\Service1.exe” -display
Name “Service1” -StartupType Automatic -Description “Service1”
-name name of service
-path – the path to the executable

-display Name – how will the service be displayed
-StartupType – how will the service behave upon startup
-Description – a show description of what is the service used for

Use Start-service cmdlet to start your service.
OK, that’s all about creating a service from an application using cmd or powershell.
Best regards

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s