Operating systems run services to server operating system or user level features. These windows services generally starts in the startup and stopped at the shutdown. But some time we need to start service after the start or a configuration change.
Windows provides different tools to manage services. We have all ready looked the native Powershell service management tools following tutorial.
Service Management With Windows Powershell From Command Line
Detailed help about
sc command can be get with
/? option like below.
$ sc /?
sc command syntax is similar to the most of the windows command. First we specify server names if we want to run service command remote systems and then provide real command to operate on service. After those we specify service name and other options.
sc <server> [command] [service name] <option1> <option2>...
There are two identifier to name services.
SERVICE_NAME is real name where we use for related service operations and will be used for identifier.
DISPLAY_NAME is like a tag and used to provide more readable and understandable service names. In the following example we should use
Appinfo for service operations.
SERVICE_NAME: Appinfo DISPLAY_NAME: Application Information
Services should be started in order serve. There are a lot of services in a windows operating systems. We will use
start command in order to start windows service. Some services can be depended other services. In this situations we should start first dependency services.
In this example we will start the service named
$ sc start ProtectedStorage
If the service starts without a problem it will print the status of the service. The
STATE line shows the service is pending for start.
Stopping a windows service is very similar to starting it. We will just change the start command with
stop command. In this example we will stop the service named
ProtectedStorage where we have started it in previous step.
$ sc stop ProtectedStorage
Display Detailed Service Status
Detailed information about a service can be displayed with
query command by providing the service name. In this example we display information about
$ sc query ProtectedStorage
This command output will provide following information about the service.
TYPEline shows service type
STATEline shows current status of service like
WIN32_EXIT_CODEline shows last stop event exit code
List All Services
There is a lot of service in a default Windows installation. After the installation 3 party applications may add new services too. All of these services can be listed with
query command and
state=all options like below.
$ sc query state= all
List Only Running Services
Previous example lists all services those running or stopped. We may interested with only running services. If we provide no option to the
query command it will only print running services by default.
$ sc query
Services have configurations and these configurations can be changed during they are running. Or some services may start work incorrectly. In this situations restart services is the best way to make it work and apply new service configuration.
$ sc stop ProtectedStorage $ sc start ProtectedStorage
A service can be paused without loosing its session related information and data. And then this service can be resumed too. In order to pause a service the service should support pausing. In this example we will pause
$ sc pause ProtectedStorage
Resume or Continue Service
We will resume and continue an all ready paused service with
continue command like below.
$ sc continue ProtectedStorage