How to Start a Service on a Remote Computer in Batch Script
Managing Windows services on remote machines is a common administrative task. You might need to restart the Print Spooler on a print server, start the Windows Update service on workstations, or manage IIS on web servers.
In this guide, we will explore the standard ways to start, stop, and query services on remote computers using Batch Script. We will primarily use the built-in sc (Service Control) command.
Method 1: Using the sc Command
The sc.exe command is the native tool for communicating with the Service Control Manager (SCM). It works over the network using RPC (Remote Procedure Call).
Syntax
sc \\ComputerName Command ServiceName
\\ComputerName: The name or IP address of the remote machine.Command: Usuallystart,stop,query, orconfig.ServiceName: The short name of the service (e.g.,spooler), not the Display Name (e.g., "Print Spooler").
Finding the Service Short Name
Before running commands, verify the service name. You can query the remote machine to find it.
sc \\Server01 query | findstr "SERVICE_NAME DISPLAY_NAME"
Or specifically for the spooler:
sc \\Server01 query spooler
Starting a Service
To start the 'Print Spooler' service on 'Server01':
sc \\Server01 start spooler
If successful, you will see output indicating the service is now in the START_PENDING state.
Stopping a Service
To stop it:
sc \\Server01 stop spooler
Checking the Status
It is good practice to check if the service actually started.
@echo off
set "Server=Server01"
set "Service=wuauserv"
echo Checking %Service% on %Server%...
sc \\%Server% query %Service% | find "STATE" >nul
if %errorlevel% equ 0 (
sc \\%Server% query %Service% | find "STATE"
) else (
echo [ERROR] Could not query %Service% on %Server%. Check the name, connectivity, or permissions.
)
pause
Method 2: Using PsService (Sysinternals)
If sc is too basic or you need more reliable output parsing, PsService from the Sysinternals Suite is excellent.
Syntax:
psservice \\Server01 start spooler
Method 3: Using Netsvc (Older Resource Kit)
netsvc is an older tool from the Windows Resource Kit. It is less common now but might be present in legacy scripts.
Syntax: netsvc spooler \\Server01 /start
Troubleshooting "Access Denied"
The most common error is:
[SC] OpenSCManager FAILED 5: Access is denied.
This happens because the user running the script does not have administrative privileges on the target machine.
Solutions:
- Run as Domain Admin: Execute your script from a command prompt running as a user with appropriate rights.
- Use
net use: Authenticate to the IPC$ share of the remote machine first.net use \\Server01\IPC$ /user:Domain\AdminUser Password >nul 2>nulif %errorlevel% neq 0 (echo Failed to authenticate to Server01.) else (sc \\Server01 start spoolernet use \\Server01\IPC$ /delete >nul 2>nul)
Putting passwords in scripts is insecure!
Example: Restarting a Service on Multiple Computers
Here is a script that reads a list of computers and restarts the "Windows Update" service on each one.
@echo off
setlocal EnableDelayedExpansion
set "Service=wuauserv"
set "ListFile=servers.txt"
if not exist "%ListFile%" (
echo File %ListFile% not found.
pause
exit /b
)
for /f "usebackq tokens=*" %%A in ("%ListFile%") do (
echo ==========================================
echo Managing %%A
REM Stop the service (may already be stopped; ignore errors)
echo Stopping %Service%...
sc \\%%A stop %Service% >nul 2>nul
REM Wait for the service to fully stop
timeout /t 3 >nul
REM Start the service
echo Starting %Service%...
sc \\%%A start %Service% >nul 2>nul
if !errorlevel! equ 0 (
echo [OK] Service start command sent.
) else (
echo [FAIL] Could not start service. Check permissions/RPC.
)
)
echo ==========================================
echo Done.
pause
Summary
Use sc \\ComputerName start ServiceName for native, dependency-free service management. Always ensure you have administrative credentials and the correct service short name before executing.