Windows

Windows / Windowskommandon

SC command info

What is the SC Command?

SC.exe - Service Control: Create, Start, Stop, Query or Delete any Windows SERVICE. The command options for SC are case sensitive.

Syntax:

SC [\\server] [command] [service_name] [Options]

server - The machine where the service is running

commands:

query [qryOpt]Show status
queryEx [qryOpt]Show extended info - pid, flags
GetDisplayNameShow the DisplayName
GetKeyNameShow the ServiceKeyName for a service
EnumDependShow Dependencies
qcShow config - dependencies, full path etc. For detailed information, see the reference section: SC QC
QueryQueries the status for a service, or enumerates the status for types of services. For detailed information, see the reference section: SC QUERY
startSTART a service
stopSTOP a service
pausePAUSE a service
continueCONTINUE a service
createCreate a service. (add it to the registry)
configpermanently change the service configuration
controlSends a control to a service
deleteDelete a service (from the registry)
controlSend a control to a service
interrogateSend an INTERROGATE control request to a service
QdescriptionQuery the description of a service
descriptionChange the description of a service
QfailureQuery the actions taken by a service upon failure
failureChange the actions taken by a service upon failure
sdShowDisplay a service's security descriptor using SDDL
SdSetSets a service's security descriptor using SDDL

Misc commands that don’t require a service name:

SC QueryLockQuery the LockStatus for the ServiceManager Database. This will show if a service request is running
SC LockLock the Service Database
SC BOOTValues are {ok | bad} Indicates whether to save the last restart configuration as the "last-known-good" restart configuration

service_name - The KeyName of the service, this is often but not always the same as the DisplayName shown in Control Panel, Services.
You can get the KeyName by running: SC GetKeyName DisplayName

Options: - The CREATE and CONFIG commands allow additional options to be set. See the build-in help: SC create and SC config.

qryOpt:

ype= driver|service|allQuery specific types of service
state= active|inactive|allQuery services in a particular state only
bufsize= bytes
ri= resume_index_number (default=0)
group= groupnameQuery services in a particular group

Note the qryOpt options above are case sensitive - they must be entered in lower case, also the position of spaces and = must be exactly as shown.

The SC command duplicates some aspects of the NET command but adds the ability to create a service. SC query will display if a service is running, giving output like this:

SERVICE_NAME       : messenger
TYPE               : 20  WIN32_SHARE_PROCESS
STATE              : 4  RUNNING (STOPPABLE,NOT_PAUSABLE,ACCEPTS_SHUTDOWN)
WIN32_EXIT_CODE    : 0  (0x0)
SERVICE_EXIT_CODE  : 0  (0x0)
CHECKPOINT         : 0x0
WAIT_HINT          : 0x0

To retrieve specific information from SC's output, pipe into FIND or FindStr, e.g:

C:\> SC query messenger | FIND "STATE" | FIND "STOPPED"

C:\> SC query messenger | FIND "STATE" | FIND "RUNNING"

The statements above will return an %ERRORLEVEL% = 1 if the text is not found.

IF errorlevel 1 GOTO :my_subroutine

The NET START command can be used in a similar way to check if a service is running:

NET START | FIND "Service name" > nul
IF errorlevel 1 ECHO The service is not running

The service control manager will normally wait up to 30 seconds to allow a service to start - you can modify this time (30,000 milliseconds) in the registry.

HKLM\SYSTEM\CurrentControlSet\Control
ServicesPipeTimeout (REG_DWORD)

Some options only take effect at the point when the service is started e.g. the SC config command allows the executable of a service to be changed. When the service next starts up it will run the new executable. Config changes requires the current user to have "permission to configure the service".

Examples:

SC GetKeyName "task scheduler"
SC GetDisplayName schedule 
SC start schedule
SC QUERY schedule
SC QUERY type= driver
SC QUERY state= all |findstr "DISPLAY_NAME STATE" >svc_installed.txt 
SC \\myServer CONFIG myService obj= LocalSystem password= mypassword
SC CONFIG MyService binPath=c:\myprogram.exe obj=".\LocalSystem" password=""

Watch out for extra spaces:

SC QUERY state= all - Works
SC QUERY state =all - Fails!

Syntax:

sc [ServerName] boot [{bad|OK}]

sc [ServerName] config [ServiceName] 
[type= {own|share|kernel|filesys|rec|adapt|interact type= {own|share}}] 
[start= {boot|system|auto|demand|disabled}] 
[error= {normal|severe|critical|ignore}] 
[binpath= BinaryPathName] [group= LoadOrderGroup] [tag= {yes|no}] [depend= dependencies] 
[obj= {AccountName|ObjectName}] [displayname= DisplayName] [password= Password]

Parameters:

ServerName
Specifies the name of the remote server on which the service is located. The name must use the Universal Naming Convention (UNC) format ("\\myserver"). To run SC.exe locally, ignore this parameter.

[{ bad | OK }]
Specifies whether the last boot was bad or whether it should be saved as the last-known-good boot configuration.

/?
Displays help at the command prompt.

ServiceName
Specifies the service name returned by the getkeyname operation.

type= { own | share | kernel | filesys | rec | adapt | interact   type= { own | share }}
Specifies the service type.

- own
The service runs in its own process. It does not share an executable file with other services. This is the default.

- share
The service runs as a shared process. It shares an executable file with other services.

- kernel
Driver.

- filesys
File system driver.

- rec
File system-recognized driver (identifies file systems used on the computer).

- adapt
Adapter driver (identifies hardware items such as keyboard, mouse, and disk drive).

- interact
The service can interact with the desktop, receiving input from users. Interactive services must be run under the LocalSystem account. This type must be used in conjunction with type= own or type= shared (for example, type= interact type= own). Using type= interact by itself will generate an invalid parameter error.

start= { boot | system | auto | demand | disabled }
Specifies the start type for the service.

- boot
A device driver that is loaded by the boot loader.

- system
A device driver that is started during kernel initialization.

- auto
A service that automatically starts each time the computer is restarted and runs even if no one logs on to the computer.

- demand
A service that must be manually started. This is the default value if start= is not specified.

- disabled
A service that cannot be started. To start a disabled service, change the start type to some other value.

error= { normal | severe | critical | ignore }
Specifies the severity of the error if the service fails to start during boot.

- normal
The error is logged and a message box is displayed informing the user that a service has failed to start. Startup will continue. This is the default setting.

- severe
The error is logged (if possible). The computer attempts to restart with the last-known-good configuration. This could result in the computer being able to restart, but the service may still be unable to run.

- critical
The error is logged (if possible). The computer attempts to restart with the last-known-good configuration. If the last-known-good configuration fails, startup also fails, and the boot process halts with a Stop error.

- ignore
The error is logged and startup continues. No notification is given to the user beyond recording the error in the Event Log.

binpath= BinaryPathName
Specifies a path to the service binary file.

group= LoadOrderGroup
Specifies the name of the group of which this service is a member. The list of groups is stored in the registry in the HKLM\System\CurrentControlSet\Control\ServiceGroupOrder subkey. The default is null.

tag= { yes | no }
Specifies whether or not to obtain a TagID from the CreateService call. Tags are only used for boot-start and system-start drivers.

depend= dependencies
Specifies the names of services or groups which must start before this service. The names are separated by forward slashes (/).

obj= {AccountName | ObjectName}
Specifies a name of an account in which a service will run, or specifies a name of the Windows driver object in which the driver will run. The default is LocalSystem.

displayname= DisplayName
Specifies a friendly, meaningful name that can be used in user-interface programs to identify the service to users. For example, the subkey name of one service is wuauserv, which is not be helpful to the user, and the display name is Automatic Updates.

password= Password
Specifies a password. This is required if an account other than the LocalSystem account is used.

Examples:

The following examples show how you can use the sc boot command:

sc boot ok 
sc boot bad

Modify the value of a service's entries in the registry and in the Service Control Manager's database:

sc config

Related

DELSRVDelete a service
INSTSRVInstall a service (run under a specific account)
NETManage network resources
NETSVCCommand-line Service Controller (Win 2K ResKit)
PsServiceView and control services
SCLISTDisplay Services
SvcmonMonitor services and raise an alert if they stop. (Win 2K ResKit)
SvcaclsService ACL Editor (Win 2K ResKit)
SUBINACLSet service permissions
WMIC SERVICEWMI access to services

Info about Windows Services:
http://support.microsoft.com/kb/251192 Q251192 - Create a Windows Service using SC
http://support.microsoft.com/kb/166819 Q166819 - Control Services Remotely
http://support.microsoft.com/kb/170738 Q170738 - Debug a Windows Service
Powershell command: Get-Service - Get a list of services

Sources:
SC.exe (SS64.com)
SC (Microsoft Technet)