}

Windows Batch: Shutdown, Restart & Logoff Commands

Introduction

The Windows shutdown command is a powerful tool for controlling system power states from the command line or batch files. Whether you need to schedule automatic shutdowns, restart remote computers, or create quick-access scripts, mastering this command will save you time and effort.

Common use cases include:

  • Remote Desktop sessions where shutdown buttons are disabled
  • Scheduled maintenance requiring automatic restarts
  • IT administration for managing multiple computers
  • Automation scripts that need to reboot after changes

Basic Shutdown Command Syntax

The general syntax for the shutdown command is:

shutdown [/s | /r | /h | /l | /a | /g] [/t xxx] [/c "comment"] [/f] [/m \\computer]

You can use either / or - as the parameter prefix (e.g., shutdown /s or shutdown -s).

Core Shutdown Options

Shutdown the Computer (/s)

The /s flag performs a complete system shutdown:

@echo off
REM Immediate shutdown
shutdown /s /t 0

Save this as shutdown.bat and double-click to run.

Restart the Computer (/r)

The /r flag restarts the computer:

@echo off
REM Immediate restart
shutdown /r /t 0

For a restart with a 60-second warning:

@echo off
shutdown /r /t 60
echo Computer will restart in 60 seconds...
pause

Hibernate (/h)

The /h flag puts the computer into hibernation mode, saving the current session to disk:

@echo off
REM Hibernate immediately
shutdown /h

Note: Hibernation must be enabled on your system. Enable it with:

powercfg /hibernate on

Log Off Current User (/l)

The /l flag logs off the current user without shutting down:

@echo off
REM Log off current user
shutdown /l

Note: The /l flag cannot be combined with /t or /m options.

Time Delays with /t

Use /t followed by seconds to delay the action:

@echo off
REM Shutdown in 5 minutes (300 seconds)
shutdown /s /t 300

REM Restart in 30 seconds
shutdown /r /t 30

REM Immediate action (no delay)
shutdown /s /t 0

The maximum value for /t is 315360000 seconds (10 years).

During the countdown, users see a notification. For values over 0, Windows displays a warning dialog.

Abort a Pending Shutdown (/a)

The /a flag cancels any scheduled shutdown, restart, or logoff:

@echo off
REM Cancel pending shutdown
shutdown /a
echo Shutdown cancelled!
pause

This is extremely useful when you accidentally trigger a shutdown or need to stop a scheduled restart.

Display a Custom Message (/c)

Use /c to display a message explaining why the shutdown is occurring:

@echo off
REM Shutdown with message (max 512 characters)
shutdown /s /t 120 /c "System maintenance in progress. Please save your work. Computer will shut down in 2 minutes."

The message appears in the shutdown warning dialog, helping users understand why the action is happening.

Force Close Applications (/f)

The /f flag forces running applications to close without warning:

@echo off
REM Force restart (closes all apps without prompting)
shutdown /r /t 0 /f

Important considerations:

  • Without /f, shutdown may hang waiting for apps to close
  • /f is implied when /t is greater than 0
  • Use /f explicitly for immediate shutdowns (/t 0) to prevent hanging
  • Warning: Users may lose unsaved work

Best practice for remote sessions:

@echo off
REM Reliable remote desktop restart
shutdown /r /t 0 /f

Remote Shutdown (/m)

The /m flag allows you to shutdown or restart remote computers:

@echo off
REM Shutdown a remote computer
shutdown /s /m \\COMPUTER-NAME /t 60 /c "Scheduled maintenance"

REM Restart multiple computers
shutdown /r /m \\SERVER01 /t 0 /f
shutdown /r /m \\SERVER02 /t 0 /f
shutdown /r /m \\SERVER03 /t 0 /f

Requirements for Remote Shutdown

  1. Admin rights on the remote computer
  2. File and Printer Sharing enabled on the remote computer
  3. Remote Registry service running on the remote computer
  4. Firewall exceptions for remote administration

To enable remote shutdown via Windows Firewall:

netsh advfirewall firewall set rule group="Remote Shutdown" new enable=yes

Using IP Address

You can also use IP addresses:

shutdown /r /m \\192.168.1.100 /t 0 /f

Interactive Remote Shutdown Dialog (/i)

For a graphical interface to manage remote shutdowns:

shutdown /i

This opens a dialog where you can add multiple computers and configure shutdown options.

Practical Batch Scripts

Shutdown Menu Script

@echo off
title Shutdown Menu
:menu
cls
echo ================================
echo    SHUTDOWN MENU
echo ================================
echo.
echo  1. Shutdown Now
echo  2. Restart Now
echo  3. Shutdown in 1 hour
echo  4. Restart in 30 minutes
echo  5. Cancel Scheduled Shutdown
echo  6. Log Off
echo  7. Hibernate
echo  8. Exit
echo.
set /p choice="Enter your choice (1-8): "

if "%choice%"=="1" shutdown /s /t 0 /f
if "%choice%"=="2" shutdown /r /t 0 /f
if "%choice%"=="3" (
    shutdown /s /t 3600 /c "Scheduled shutdown in 1 hour"
    echo Shutdown scheduled!
    pause
    goto menu
)
if "%choice%"=="4" (
    shutdown /r /t 1800 /c "Scheduled restart in 30 minutes"
    echo Restart scheduled!
    pause
    goto menu
)
if "%choice%"=="5" (
    shutdown /a
    echo Shutdown cancelled!
    pause
    goto menu
)
if "%choice%"=="6" shutdown /l
if "%choice%"=="7" shutdown /h
if "%choice%"=="8" exit
goto menu

Scheduled Shutdown with User Confirmation

@echo off
echo Computer will shutdown in 5 minutes.
echo Press any key to cancel...
shutdown /s /t 300 /c "Automatic shutdown in 5 minutes"
pause >nul
shutdown /a
echo Shutdown cancelled!
pause

Shutdown at Specific Time

@echo off
REM Calculate seconds until 11:00 PM
set target_hour=23
set target_minute=0

for /f "tokens=1-2 delims=:" %%a in ("%time%") do (
    set /a current_hour=%%a
    set /a current_minute=%%b
)

set /a target_seconds=(%target_hour%*3600)+(%target_minute%*60)
set /a current_seconds=(%current_hour%*3600)+(%current_minute%*60)
set /a wait_seconds=%target_seconds%-%current_seconds%

if %wait_seconds% lss 0 (
    echo Target time has already passed today.
    pause
    exit
)

echo Shutdown scheduled for %target_hour%:%target_minute%
shutdown /s /t %wait_seconds% /c "Scheduled shutdown at %target_hour%:%target_minute%"

Task Scheduler Integration

For more reliable scheduled shutdowns, use Windows Task Scheduler.

Create a Scheduled Shutdown via Command Line

@echo off
REM Create a task to shutdown daily at 11 PM
schtasks /create /tn "DailyShutdown" /tr "shutdown /s /t 60 /c \"Nightly shutdown\"" /sc daily /st 23:00 /f

echo Task created successfully!

Common schtasks Options

REM Run once at specific date/time
schtasks /create /tn "OneTimeShutdown" /tr "shutdown /s /t 0 /f" /sc once /sd 12/31/2026 /st 18:00

REM Run weekly on Fridays
schtasks /create /tn "WeeklyRestart" /tr "shutdown /r /t 300" /sc weekly /d FRI /st 22:00

REM Run at system startup
schtasks /create /tn "StartupRestart" /tr "shutdown /r /t 3600" /sc onstart

REM Delete a scheduled task
schtasks /delete /tn "DailyShutdown" /f

REM List all scheduled tasks
schtasks /query /tn "DailyShutdown"

Using Task Scheduler GUI

  1. Press Win + R, type taskschd.msc, press Enter
  2. Click Create Basic Task
  3. Set a name and trigger (daily, weekly, etc.)
  4. Choose Start a program
  5. Program: shutdown
  6. Arguments: /s /t 60 /c "Scheduled shutdown"

PowerShell Alternatives

PowerShell offers more flexibility and better error handling for power management.

Basic PowerShell Commands

# Shutdown
Stop-Computer -Force

# Restart
Restart-Computer -Force

# Shutdown with delay (PowerShell 7+)
Stop-Computer -Force -Delay 120

Remote Computer Management

# Restart a remote computer
Restart-Computer -ComputerName "SERVER01" -Force -Credential (Get-Credential)

# Restart multiple computers
$computers = @("SERVER01", "SERVER02", "SERVER03")
Restart-Computer -ComputerName $computers -Force

# With timeout and wait
Restart-Computer -ComputerName "SERVER01" -Wait -Timeout 300 -Force

Scheduled Shutdown in PowerShell

# Schedule shutdown in 1 hour
$action = New-ScheduledTaskAction -Execute "shutdown.exe" -Argument "/s /t 0 /f"
$trigger = New-ScheduledTaskTrigger -Once -At (Get-Date).AddHours(1)
Register-ScheduledTask -TaskName "ScheduledShutdown" -Action $action -Trigger $trigger

# Remove the scheduled task
Unregister-ScheduledTask -TaskName "ScheduledShutdown" -Confirm:$false

Check System Uptime

# Get system uptime
(Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime

# Or using systeminfo
systeminfo | find "Boot Time"

Complete Command Reference

Option Description
/s Shutdown the computer
/r Restart the computer
/g Restart and relaunch registered applications
/h Hibernate the computer
/l Log off the current user
/a Abort a pending shutdown
/p Turn off without warning (instant power off)
/t xxx Set timeout before action (seconds)
/c "msg" Display a comment message (max 512 chars)
/f Force close applications
/m \\computer Specify a remote computer
/d [p\|u:]xx:yy Provide shutdown reason code
/i Display graphical interface
/o Go to Advanced Boot Options (with /r)
/e Document reason for unexpected shutdown

Troubleshooting

"Access Denied" on Remote Shutdown

  1. Ensure you have admin rights on the remote computer
  2. Enable Remote Registry service: batch sc \\COMPUTER-NAME start RemoteRegistry
  3. Check firewall settings

Shutdown Command Not Working

  1. Run Command Prompt as Administrator
  2. Check for pending Windows updates
  3. Verify no Group Policy restrictions

Application Preventing Shutdown

Use /f flag to force close applications:

shutdown /s /t 0 /f

Conclusion

The Windows shutdown command is an essential tool for system administrators and power users. Combined with batch scripts, Task Scheduler, and PowerShell, you can automate virtually any power management scenario.

Key takeaways:

  • Use /f for reliable shutdowns, especially over Remote Desktop
  • Use /a to cancel accidental shutdowns
  • Task Scheduler provides more reliable scheduled shutdowns than /t delays
  • PowerShell offers better remote management capabilities
  • Always test scripts before deploying in production environments