What's the easiest programmatic way to restart a service on a remote Windows system? Language or method doesn't matter as long as it doesn't require human interaction.
11 Answers
As of Windows XP, you can use sc.exe
to interact with local and remote services. Schedule a task to run a batch file similar to this:
sc \\server stop service sc \\server start service
Make sure the task runs under a user account privileged on the target server.
psservice.exe
from the Sysinternals PSTools would also be doing the job:
psservice \\server restart service

- 332,285
- 67
- 532
- 628
-
4Be aware of the fact that the "service name" for a service may be different from its "display name". E.g. The "Windows Time" service has the service name "W32Time". You can check the service name in the properties dialog of a service. – Brian Hinchey Dec 27 '12 at 03:15
-
2psservice was the only thing I could use to script my task successfully. sc and net commands are very good for local systems or over the network actions in which you would want to use current user's credentials. But if you need to provide username and password to start/stop services remotely and dont want to use powershell then psservice is the way to go – Aidin Oct 03 '13 at 20:27
DESCRIPTION: SC is a command line program used for communicating with the NT Service Controller and services. USAGE: sc [command] [service name] ...
The option <server> has the form "\\ServerName"
Further help on commands can be obtained by typing: "sc [command]"
Commands:
query-----------Queries the status for a service, or
enumerates the status for types of services.
queryex---------Queries the extended status for a service, or
enumerates the status for types of services.
start-----------Starts a service.
pause-----------Sends a PAUSE control request to a service.
interrogate-----Sends an INTERROGATE control request to a service.
continue--------Sends a CONTINUE control request to a service.
stop------------Sends a STOP request to a service.
config----------Changes the configuration of a service (persistant).
description-----Changes the description of a service.
failure---------Changes the actions taken by a service upon failure.
qc--------------Queries the configuration information for a service.
qdescription----Queries the description for a service.
qfailure--------Queries the actions taken by a service upon failure.
delete----------Deletes a service (from the registry).
create----------Creates a service. (adds it to the registry).
control---------Sends a control to a service.
sdshow----------Displays a service's security descriptor.
sdset-----------Sets a service's security descriptor.
GetDisplayName--Gets the DisplayName for a service.
GetKeyName------Gets the ServiceKeyName for a service.
EnumDepend------Enumerates Service Dependencies.
The following commands don't require a service name:
sc <server> <command> <option>
boot------------(ok | bad) Indicates whether the last boot should
be saved as the last-known-good boot configuration
Lock------------Locks the Service Database
QueryLock-------Queries the LockStatus for the SCManager Database
EXAMPLE: sc start MyService

- 37,399
- 13
- 80
- 138
-
4How do you pass the username and password required to restart the service on the remote system? Does this suggest "no password required" ? – djangofan Jun 03 '13 at 21:26
-
1@djangofan bit late but for those who are wondering you use `net use \\server_name\admin$ password /user:domain\username` prior to executing this command. https://stackoverflow.com/a/30787763/3447365 – Songy Jun 01 '17 at 09:59
If it doesn't require human interaction which means there will be no UI that invokes this operation and I assume it would restart at some set interval? If you have access to machine, you could just set a scheduled task to execute a batch file using good old NET STOP and NET START
net stop "DNS Client"
net start "DNS client"
or if you want to get a little more sophisticated, you could try Powershell

- 31,040
- 13
- 70
- 99
-
1As most other answers, this restarts the "DNS Client" service from the **local** machine... – Álvaro González Nov 11 '15 at 13:19
There will be so many instances where the service will go in to "stop pending".The Operating system will complain that it was "Not able to stop the service xyz." In case you want to make absolutely sure the service is restarted you should kill the process instead. You can do that by doing the following in a bat file
taskkill /F /IM processname.exe
timeout 20
sc start servicename
To find out which process is associated with your service, go to task manager--> Services tab-->Right Click on your Service--> Go to process.
Note that this should be a work around until you figure out why your service had to be restarted in the first place. You should look for memory leaks, infinite loops and other such conditions for your service to have become unresponsive.

- 924
- 1
- 12
- 27
-
1If you have written a service in python it will usually be the following:- "taskkill /F /IM pythonservice.exe" – ambassallo Jun 15 '15 at 15:38
As of Powershell v3, PSsessions allow for any native cmdlet to be run on a remote machine
$session = New-PSsession -Computername "YourServerName"
Invoke-Command -Session $Session -ScriptBlock {Restart-Service "YourServiceName"}
Remove-PSSession $Session
See here for more information

- 2,237
- 2
- 32
- 53
-
great answer, but keep in mind if the remote server is on a different domain you will [need to configure WinRM.](https://serverfault.com/questions/657918/remote-ps-session-fails-on-non-domain-server) – David Rogers Oct 08 '20 at 19:34
look at sysinternals for a variety of tools to help you achieve that goal. psService for example would restart a service on a remote machine.

- 1,932
- 2
- 18
- 23
I recommend the method given by doofledorfer.
If you really want to do it via a direct API call, then look at the OpenSCManager function. Below are sample functions to take a machine name and service, and stop or start them.
function ServiceStart(sMachine, sService : string) : boolean; //start service, return TRUE if successful
var schm, schs : SC_Handle;
ss : TServiceStatus;
psTemp : PChar;
dwChkP : DWord;
begin
ss.dwCurrentState := 0;
schm := OpenSCManager(PChar(sMachine),Nil,SC_MANAGER_CONNECT); //connect to the service control manager
if(schm > 0)then begin // if successful...
schs := OpenService( schm,PChar(sService),SERVICE_START or SERVICE_QUERY_STATUS); // open service handle, start and query status
if(schs > 0)then begin // if successful...
psTemp := nil;
if (StartService(schs,0,psTemp)) and (QueryServiceStatus(schs,ss)) then
while(SERVICE_RUNNING <> ss.dwCurrentState)do begin
dwChkP := ss.dwCheckPoint; //dwCheckPoint contains a value incremented periodically to report progress of a long operation. Store it.
Sleep(ss.dwWaitHint); //Sleep for recommended time before checking status again
if(not QueryServiceStatus(schs,ss))then
break; //couldn't check status
if(ss.dwCheckPoint < dwChkP)then
Break; //if QueryServiceStatus didn't work for some reason, avoid infinite loop
end; //while not running
CloseServiceHandle(schs);
end; //if able to get service handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_RUNNING = ss.dwCurrentState; //if we were able to start it, return true
end;
function ServiceStop(sMachine, sService : string) : boolean; //stop service, return TRUE if successful
var schm, schs : SC_Handle;
ss : TServiceStatus;
dwChkP : DWord;
begin
schm := OpenSCManager(PChar(sMachine),nil,SC_MANAGER_CONNECT);
if(schm > 0)then begin
schs := OpenService(schm,PChar(sService),SERVICE_STOP or SERVICE_QUERY_STATUS);
if(schs > 0)then begin
if (ControlService(schs,SERVICE_CONTROL_STOP,ss)) and (QueryServiceStatus(schs,ss)) then
while(SERVICE_STOPPED <> ss.dwCurrentState) do begin
dwChkP := ss.dwCheckPoint;
Sleep(ss.dwWaitHint);
if(not QueryServiceStatus(schs,ss))then
Break;
if(ss.dwCheckPoint < dwChkP)then
Break;
end; //while
CloseServiceHandle(schs);
end; //if able to get svc handle
CloseServiceHandle(schm);
end; //if able to get svc mgr handle
Result := SERVICE_STOPPED = ss.dwCurrentState;
end;

- 57,317
- 63
- 160
- 234
- open service control manager database using openscmanager
- get dependent service using EnumDependService()
- Stop all dependent services using ChangeConfig() sending STOP signal to this function if they are started
- stop actual service
- Get all Services dependencies of a service
- Start all services dependencies using StartService() if they are stopped
- Start actual service
Thus your service is restarted taking care all dependencies.

- 236,525
- 50
- 385
- 514
I believe PowerShell now trumps the "sc" command in terms of simplicity:
Restart-Service "servicename"

- 305
- 1
- 3
- 9
-
Restart-Service doesn't have the option to restart a service on a remote computer though. – solipsicle Sep 21 '17 at 14:11
This is what I do when I have restart a service remotely with different account
Open a CMD with different login
runas /noprofile /user:DOMAIN\USERNAME cmd
Use SC to stop and start
sc \\SERVERNAME query Tomcat8
sc \\SERVERNAME stop Tomcat8
sc \\SERVERNAME start Tomcat8

- 12,675
- 27
- 97
- 154
If you attempting to do this on a server on different domain you will need to a little bit more than what has been suggested by most answers so far, this is what I use to accomplish this:
#Configuration
$servername = "ABC",
$serviceAccountUsername = "XYZ",
$serviceAccountPassword = "XXX"
#Establish connection
try {
if (-not ([System.IO.Directory]::Exists('\\' + $servername))) {
net use \\$servername /user:$serviceAccountUsername $serviceAccountPassword
}
}
catch {
#May already exists, if so just continue
Write-Output $_.Exception.Message
}
#Restart Service
sc.exe \\$servername stop "ServiceNameHere"
sc.exe \\$servername start "ServiceNameHere"

- 2,601
- 4
- 39
- 84