Toggle All Running Services

From espinola.net

Jump to: navigation, search
Script

This script will attempt to stop all current system services in alphabetical order. Running the script a second time will restart all previously stopped services. Services that were unable to be stopped will be reported at the end of the scripts initial 'stop' pass.

This script is useful when applying updates to systems where open files need to be replaced. Using this script can help circumvent a reboot requirement. With Windows NT4 (for which this script was first created in 1998), this script can help ensure that all Service Pack files are updated successfully.

This script is also very useful to reduce system resources and increase available memory for playing resource intensive games.

On This Page


Utilities used with this script

  • FINDSTR (native)
  • IF (native)
  • NET (native)
  • START (native)

Requirements of this script

  • Administrative access is required to stop and start services.

Caveats of this script

  • This script is autonomous and does not use command-line parameters.
  • This script leaves a 'marker' file in the %TEMP% directory to determine if services are pending to be restarted.
  • This script uses an internal %TARSLIST% variable to skip stopping services of your choice. This variable is located at the beginning of the script.
  • Rerunning this script a second time will locate the 'marker' file, and restart all the services that were previously stopped - after which the marker is deleted.
  • This script should only very carefully be run from a network drive. The inherent nature of this script can cause network connections to fail before the script completes. If restoring network connectivity is required after the scripts initial 'stop' pass is complete, simply manually restart the appropriate network services (typically 'Workstation' and 'DHCP Client').
  • This script will most likely disable anti-virus software running as a service. Exclude the anti-virus from being stopped, or be careful of what you do while your anti-virus is disabled.


Example Usage

C:\Scripts>TARS

Code

TARS.CMD

@ECHO OFF
ECHO.
ECHO [TARS] Toggle All Running Services 6.2
ECHO.===============================================================================
ECHO.

:SET_ENVIRONMENT
SETLOCAL
REM TARSLIST is a search string variable to match services to NOT be stopped.
SET TARSLIST=DameWare DHCP DNS Event Logon Workstation Server 

:STOP
IF EXIST %TEMP%\TARS1.txt GOTO RESTART
NET START > %TEMP%\TARS1.txt
IF EXIST %TEMP%\TARS1.txt FINDSTR /V /C:"The command completed successfully." %TEMP%\TARS1.txt > %TEMP%\TARS2.txt
IF EXIST %TEMP%\TARS2.txt FINDSTR /V "%TARSLIST%" %TEMP%\TARS2.txt > %TEMP%\TARS3.txt
IF EXIST %TEMP%\TARS3.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS3.txt) DO ECHO * Stopping the %%a service.. & START /MIN /WAIT NET STOP "%%a" /Y
NET START > %TEMP%\TARS3.txt
IF EXIST %TEMP%\TARS3.txt FINDSTR /V /C:"The command completed successfully." %TEMP%\TARS3.txt > %TEMP%\TARS4.txt

ECHO.
IF EXIST %TEMP%\TARS4.txt ECHO The following services could not be stopped:
IF EXIST %TEMP%\TARS4.txt ECHO.
IF EXIST %TEMP%\TARS4.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS4.txt) DO ECHO %%a
GOTO END

:RESTART
IF EXIST %TEMP%\TARS2.txt FOR /F "skip=2 tokens=*" %%a IN (%TEMP%\TARS2.txt) DO ECHO * Restarting the %%a service.. & START /MIN /WAIT NET START "%%a" /Y
IF EXIST %TEMP%\TARS1.txt DEL %TEMP%\TARS1.txt > NUL:
IF EXIST %TEMP%\TARS2.txt DEL %TEMP%\TARS2.txt > NUL:
IF EXIST %TEMP%\TARS3.txt DEL %TEMP%\TARS3.txt > NUL:
IF EXIST %TEMP%\TARS4.txt DEL %TEMP%\TARS4.txt > NUL:
GOTO END

:END
ECHO.
REM [TARS] Toggle All Running Services 6.1
REM Copyright 1998-*, Micheal Espinola Jr (michealespinola@gmail.com)
REM
REM This script is free to USE & MODIFY or REDISTRIBUTE in its' ORIGINAL FORM,
REM as long as this REM'd passage is always included.
ECHO.===============================================================================
ECHO.

:THEEND
PAUSE
ENDLOCAL