Managing Windows Service via Command Line Interface

Home / Managing Windows Service via Command Line Interface

Service is also known as background process, a program which run in background and similar concept to a Unix daemon. A Windows service must conform to the interface rules and protocols of the Service Control Manager, the component responsible for managing Windows services.

As title suggests, we will discuss about how to manage Windows service using Command Line Interface (CLI) not Graphical User Interface (GUI). Specifically we will use two CLI: command prompt and Windows PowerShell. Using a command prompt, we can invoke two different program to fulfill our need: sc.exe and net.exe.

We will use a fictional service serv.exe registered as serv48 as our example.

All examples are done using Administrator privilege, no user privilege involved.

Get Service Status

Get status from a registered service, such as state (running, paused, suspended, stopped), name, etc.

Using sc (command prompt)

sc query serv48

Sample response:

SERVICE_NAME: serv48
        TYPE               : 10  WIN32_OWN_PROCESS
        STATE              : 1  STOPPED
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Using PowerShell

Get-Service mysql56serv48

Sample response:

Status   Name               DisplayName
------   ----               -----------
Stopped  Serv48             serv48

Register New Service

Creates a service entry in the registry and service database. In other word, it register the service to windows service component.

Using sc (command prompt)

sc create serv48 binPath=C:\ImportantApp\serv48d.exe DisplayName=serv48

Using PowerShell

New-Service -name serv48 -binaryPathName C:\ImportantApp\serv48d.exe -displayName serv48

Restart Service

This section will restart a service. Actually, a restart means stopping and starting the same service.

Using sc (command prompt)

sc stop serv48
sc start serv48

Using net (command prompt)

net stop serv48
net start serv48

Using PowerShell

Stop-Service serv48
Start-Service serv48

There is also a single command in Powershell to restart a service:

Restart-Service serv48

Resume Service

Resuming a service after the service is suspended.

Using sc (command prompt)

sc continue serv48

Using net (command prompt)

net continue serv48

Using PowerShell

Resume-Service serv48

Set Service

Change or set state of services with some options.

Using sc (command prompt)

sc config serv48 [option=value]

with options available (and possible value) are:

  • type= own,share,interact,kernel,filesys,rec,adapt
  • start= boot,system,auto,demand,disabled,delayed-auto
  • error= normal,servere,critical,ignore
  • binPath= binary pathname to the .exe file
  • tag= yes,no
  • depend= service it’s depended on, separated by / (forward slash)
  • DisplayName= name used to display the service

Using PowerShell

Set-Service serv48 [-option value]

with options available (and possible value) are:

  • ComputerName – specifies one or more computers, the default is local computer.
  • Description – new description for service which will appear in Computer Management.
  • DisplayName – New display name for the service
  • Name – new service name (in our case: serv48)
  • StartupType – Automatic, Manual, Disabled
  • Status – Running, Stopped, Paused

Start Service

Start a service, change the state from stopped to running.

Using sc (command prompt)

sc start serv48

Using net (command prompt)

net start serv48

Using PowerShell

Start-Service serv48

Stop Service

Stopping a service, change state from running to stopped.

Using sc (command prompt)

sc stop serv48

Using net (command prompt)

net stop serv48

Using PowerShell

Stop-Service

Suspend Service

Also known as pause. Pause a service for a moment until it is resumed.

Using sc (command prompt)

sc pause serv48

Using net (command prompt)

net pause serv48

Using PowerShell

Suspend-Service

About Author

about author

xathrya

A man who is obsessed to low level technology.

Leave a Reply

Your email address will not be published. Required fields are marked *

Social media & sharing icons powered by UltimatelySocial