Script - Restart Service on ALL Hosts

After pushing some registry tweaks via GPO, it turned out I needed to restart the service on every host.  Rebooting and/or logging into each host was not a convenient option.  I figured there must be a way to do it.  This is the solution I came up...


  • Download the PSTools from SysInternals to get the "psservice.exe" tool.
  • You are running an Windows AD domain, logged in as an Administrator when executing.

What It Does...

Uses the "net view" command to get a list of available servers.  Then the "psservice" command is used to connect to each system and execute the action for the service specified.  It also creates a log file in C:\TEMP

Usage:  <filename.bat> <servicename> <action>

<servicename> = The name of the serivce as it appears in the properties of the Services Control Panel.

<action> = any action that is supported by psservice (see "psserivce /?")

My Batch Script

@echo off  
REM Created by - 20090402  
REM -- initialize global variables -- 
set Tnow=0 
set Today=0 
set DEBUG=1 
set Service=%1 
set Action=%2 
set Server=0  
echo Are you sure you %Action% %Sevice% on ALL servers? CTRL-C now to bail.  
REM *** Pre-Execution Stuff ***
for /f "tokens=1,2,3 delims=,-/" %%a in ("%date:~4%") do set Today=%%c%%a%%b
set Tnow=%time::=%
set logfile=C:\temp\service_restart_script_%Today%-%Tnow%.log
echo Logfile for this script is... %logfile%

REM *** Cleanup Old List File ***
if exist "C:\TEMP\SERVER.LIST" (
    ECHO *** DEBUG *** Deleting Old SERVER.LIST file >> %logfile%
    del /F "C:\TEMP\SERVER.LIST"

REM *** Create the Server List ***
IF %DEBUG% EQU 1 net view |find "\\" >> C:\TEMP\SERVER.LIST.%Today%-%Tnow%
ECHO - Getting Server List >>%logfile%
net view |find "\\" >> C:\TEMP\SERVER.LIST

REM *** For Each Server Listed, Do Something ***
ECHO - Starting process at %Today%-%Tnow% >> %logfile%
for /f "tokens=1 delims= " %%i in (SERVER.LIST) do (
    IF %DEBUG% EQU 1 ECHO *** DEBUG *** Processing %%i
    ECHO - Processing "%%i" >>%logfile%
    IF %DEBUG% EQU 1 ECHO *** DEBUG *** Executing - psservice %%i %Action% %Service% >>%logfile%
    psservice %%i %Action% %Service%
ECHO - Ending process at %Today%-%Tnow% >> %logfile% 


An idea for tweaking this script would be to add more command line options to pass server names or patterns to be found in names.  For example if you have a server name convention for SQL servers that use "sql" in the servername, you could restart a service on only SQL servers.

Also one could right a more complex script to issue commands and track the status and report if any server fails to restart correctly.  Feel free to comment and submit your ideas and tweaks.