Skip to main content

How to Start All Services That Were Stopped by Dependency in Batch Script

One of the most annoying aspects of Windows service management is that dependencies only work in one direction. When you stop a "base" service, Windows forces you to stop all its dependents. However, when you start that same base service, Windows does not automatically restart the services that were dragged down with it. This leaves your system in a partially broken state unless you manually intervene.

This guide will explain how to build a Batch script that intelligently identifies and restarts an entire service stack, ensuring your machine returns to full functionality.

The Problem: The One-Way Street

Imagine you stop the BaseData service, which also stops DataAPI and WebFrontend.

  • Stop: net stop BaseData /y stops all three.
  • Start: net start BaseData starts only the base. Your API and Frontend remain offline.

To fix this, your Batch script must be "Aware" of the hierarchy.

Method 1: The Hardcoded Sequence (Best for Specific Apps)

If you know exactly which services are involved (e.g., an SQL Server stack), the most reliable method is to list them explicitly in the correct startup order.

@echo off
echo [ACTION] Starting Service Stack...
echo.

set "FailCount=0"

:: Step 1: Start the Base (must succeed before dependents)
echo [1/3] Starting Database Engine...
net start "MSSQLSERVER"
if %errorlevel% neq 0 (
echo [CRITICAL] Base service failed to start. Cannot start dependents.
pause
exit /b 1
)

:: Brief pause to allow the base service to initialize
timeout /t 5 /nobreak >nul

:: Step 2: Start the Middleware
echo [2/3] Starting SQL Agent...
net start "SQLSERVERAGENT"
if %errorlevel% neq 0 (
echo [WARNING] SQL Agent failed to start.
set /a FailCount+=1
)

:: Step 3: Start the UI/Reporting
echo [3/3] Starting Reporting Services...
net start "ReportServer"
if %errorlevel% neq 0 (
echo [WARNING] Report Server failed to start.
set /a FailCount+=1
)

echo.
if %FailCount% equ 0 (
echo [SUCCESS] Full service stack is operational.
) else (
echo [WARNING] Base service is running but %FailCount% dependent(s^) failed.
)

pause

Method 2: Dynamic Restarting Using SC ENUMDEPEND

If you are writing a generic script and don't know the dependents in advance, you can use sc enumdepend to dynamically discover them. This script captures the dependency list before stopping the service, performs maintenance, and then restores everything.

Script: Intelligent "Full Stack" Restarter

@echo off
setlocal enabledelayedexpansion

set "BaseSvc=Spooler"
set "TempFile=%TEMP%\svc_deps_%BaseSvc%.txt"

:: Verify the base service exists
sc query "%BaseSvc%" >nul 2>&1
if %errorlevel% neq 0 (
echo [ERROR] Service '%BaseSvc%' does not exist.
pause
exit /b 1
)

:: 1. Capture the list of dependent services BEFORE stopping
echo [PHASE 1] Mapping dependencies for '%BaseSvc%'...

if exist "%TempFile%" del "%TempFile%"

for /f "tokens=2" %%a in ('sc enumdepend "%BaseSvc%" ^| findstr /c:"SERVICE_NAME"') do (
:: Only record dependents that are currently running
sc query "%%a" | findstr /i "RUNNING" >nul
if !errorlevel! equ 0 (
echo %%a >> "%TempFile%"
echo [RUNNING] %%a
)
)

if not exist "%TempFile%" (
echo [INFO] No running dependents found.
)

:: 2. Stop the stack
echo.
echo [PHASE 2] Stopping '%BaseSvc%' and dependents...
net stop "%BaseSvc%" /y >nul 2>&1

:: Wait for everything to fully stop
timeout /t 5 /nobreak >nul

echo.
echo [PHASE 3] --- Maintenance Window ---
echo (Perform your maintenance tasks here^)
echo.

:: 3. Start the Base service first
echo [PHASE 4] Restarting service stack...
echo [START] %BaseSvc%
net start "%BaseSvc%"

if %errorlevel% neq 0 (
echo [CRITICAL] Base service '%BaseSvc%' failed to start.
echo Cannot restart dependents.
del "%TempFile%" >nul 2>&1
pause
exit /b 1
)

:: Brief pause for the base service to initialize
timeout /t 3 /nobreak >nul

:: 4. Restart only the dependents that were previously running
if exist "%TempFile%" (
echo.
echo Restarting previously running dependents...
for /f "tokens=*" %%s in ('type "%TempFile%"') do (
echo [START] %%s
net start "%%s" >nul 2>&1
if !errorlevel! neq 0 (
echo [WARNING] Failed to start %%s
)
)
del "%TempFile%" >nul 2>&1
)

echo.
echo [DONE] Service stack restoration complete.

pause
endlocal
info

By saving the running dependent services to a temporary file, the script "remembers" exactly which services were active before the maintenance began. Services that were already stopped before maintenance are not unnecessarily started.

Best Practices and Rules

1. Wait for Initialization

Some base services (like SQL or heavy databases) start in the background. Even if net start says "Successfully started," the service might not be ready to accept connections from its dependents yet.

Best Practice: Add a timeout /t 5 or a polling loop between starting the base and starting the dependents (as shown in both methods).

2. Administrative Privileges

Starting services is a restricted system action. Your Batch script must be run as an Administrator. Without elevation, the start commands will fail with "Access Denied."

3. Error Handling

Always check the %errorlevel% after starting the base service. If the base fails to start, trying to start the dependents is pointless and will just fill your screen with errors.

net start "BaseSvc"
if %errorlevel% neq 0 (
echo [CRITICAL] Base service failed. Aborting stack restart.
exit /b 1
)

How to Avoid Common Errors

Wrong Way: Expecting automatic behavior

Many developers spend hours wondering why their "Web App" is down after a server reboot or a script run.

Correct Way: Never assume. If you stopped a service hierarchy, your script MUST explicitly have a start command for every service that needs to come back up.

Problem: Starting services that were not running before

If you use sc enumdepend to discover dependents and then start all of them after maintenance, you might accidentally start services that were intentionally stopped before your maintenance began.

Best Practice: Record which dependents were actually running before the stop phase, and only restart those specific services (as shown in Method 2).

Conclusions

Restoring a service stack in a Batch script is about more than just a single command, it's about managing a sequence. By using a combination of sc enumdepend for discovery and net start for execution, you can ensure that your system maintenance doesn't leave key applications in a "Disconnected" state. Automated checks and clear visual feedback make these scripts indispensable for professional system administrators.