How to Remotely Restart a Computer in Batch Script
Restarting a remote computer is a common task when performing system updates, applying configuration changes, or resolving performance issues. Instead of logging into each machine individually via Remote Desktop, you can easily automate remote reboots using a Batch script.
In this guide, we will explore how to use the built-in shutdown command to remotely restart computers on your network.
Understanding the shutdown Command
The native Windows shutdown.exe command is versatile and simple to use. It allows you to log off, power down, or restart local and remote machines.
The general syntax for a remote restart is:
shutdown /m \\RemoteComputerName /r /t delay_in_seconds /c "Comment"
Let's break down the critical switches:
/m \\ComputerName: Specifies the target remote computer by hostname or IP address./r: Instructs the computer to restart (reboot). (Use/sfor shutdown/power off)./t <seconds>: The time delay before the restart begins. This gives users a warning. Default is 30 seconds if omitted. Use0for an immediate restart./c "Comment": An optional message displayed to any logged-in users, explaining why the system is restarting. Limited to 512 characters./f: Force running applications to close without warning users. Use this with caution, as it can result in lost work.
Basic Script to Restart a Remote Computer
Here is a straightforward script that asks for a computer name and restarts it with a 1-minute warning.
@echo off
setlocal EnableDelayedExpansion
:: Prompt the user for the target computer name
set /p "TARGET_PC=Enter the name or IP of the computer to restart: "
if "!TARGET_PC!"=="" (
echo No computer name entered. Exiting.
pause
exit /b
)
echo Attempting to restart \\!TARGET_PC! in 60 seconds...
:: Execute the remote restart command
shutdown /m \\!TARGET_PC! /r /t 60 /c "IT Maintenance: Save your work. The system will restart in 1 minute."
if !ERRORLEVEL! equ 0 (
echo Restart command successfully sent to !TARGET_PC!.
) else (
echo Failed to send restart command. Check network connectivity and permissions.
)
endlocal
pause
Script Breakdown
- Prompt:
set /pasks the user to input the target machine. - Validation: It checks if the input is empty and exits if it is.
- Command Execution: The
shutdown /mcommand is sent with a 60-second delay (/t 60) and a helpful warning message (/c). - Error Handling: We check
ERRORLEVEL. If it's0, the command was successfully dispatched to the target machine's RPC service. Any other number indicates an error (offline, access denied, etc.).
Handling Common Errors
When automating remote restarts, you will inevitably encounter two main errors:
1. The Computer is Offline or Unreachable
If the target machine is turned off, hibernating, or blocked by a firewall, the command will fail and hang for several seconds while timing out.
Output:
The entered computer name is not valid or remote shutdown is not supported on the target computer. Check the name and then try again or contact your system administrator.(53)
Best Practice: Before attempting a shutdown, always verify the computer is reachable using ping.
2. Access is Denied (Error 5)
Remote shutdown over RPC requires Administrative privileges on the target machine.
Output:
Access is denied.(5)
If your current user account doesn't have local admin rights on the Remote PC, the command will fail. You must run the batch file from an elevated command prompt belonging to a Domain Admin or a user configured in the target's local Administrators group.
Best Practice: The Ping and Force Restart Script
A robust script should ping the computer first to see if it's alive, then issue the restart command. Often, administrators need to force (/f) the restart to prevent hung applications from blocking the reboot process.
Warning: Using /f means users will lose unsaved work. Use it responsibly! The /f flag is implicitly applied if your /t delay is greater than 0.
@echo off
setlocal
set "TARGET_PC=Server01"
set "WAIT_TIME=10"
set "MESSAGE=Emergency Reboot. System will restart in 10 seconds."
echo Checking connection to %TARGET_PC%...
:: Ping the target once, wait up to 1000ms
ping -n 1 -w 1000 %TARGET_PC% >nul 2>nul
if %ERRORLEVEL% neq 0 (
echo [ERROR] %TARGET_PC% is offline or unreachable. Cannot restart.
endlocal
pause
exit /b
)
echo Connection successful. Sending restart command...
:: Send restart, force apps to close, wait 10 seconds
shutdown /m \\%TARGET_PC% /r /f /t %WAIT_TIME% /c "%MESSAGE%"
if %ERRORLEVEL% equ 0 (
echo Success. %TARGET_PC% is rebooting.
) else (
echo [ERROR] Command failed. You may lack administrative privileges.
)
endlocal
pause
How to Abort a Pending Restart
If you accidentally trigger a restart with a delay (e.g., 60 seconds), you have a window of time to cancel it using the /a (abort) switch.
Syntax:
shutdown /m \\RemoteComputerName /a
To integrate this quickly, you can create a simple CancelRestart.bat file and run it immediately if you make a mistake:
@echo off
setlocal EnableDelayedExpansion
set /p "TARGET_PC=Enter computer to cancel restart: "
if "!TARGET_PC!"=="" (
echo No computer name entered.
pause
exit /b
)
shutdown /m \\!TARGET_PC! /a
if !ERRORLEVEL! equ 0 (
echo Successfully cancelled restart on !TARGET_PC!.
) else (
echo Failed to cancel restart on !TARGET_PC!. It may have already rebooted.
)
endlocal
pause
Conclusion
The native shutdown command makes remote restarts incredibly simple. By writing simple batch scripts, you can wrap this command with connectivity checks (ping), custom delays, and warning messages to safely and efficiently manage system reboots across your network. Always ensure you have the required administrative permissions before executing these commands remotely.