Automated System Reboot Script

An essential guide for IT professionals to schedule, execute and manage system reboots efficiently across their Windows environment.

In this Guide:

Introduction

Rebooting servers and endpoints is a critical task for system health, security patches and performance issues. This script is a PowerShell reboot script for automated system reboot and management. This will help IT pros and system admins to reboot multiple machines at once, reduce manual effort and minimize business disruption.

Background

In large IT environments, rebooting servers or user machines manually is not only time consuming but also prone to human error. A single missed reboot can leave a system vulnerable to security threats or prevent critical updates from installing. The challenge is to orchestrate these reboots at scale, often during off hours, without impacting user productivity. This server maintenance script solves that problem by providing a reliable and repeatable way to schedule and execute reboots automatically, making it the foundation of Windows automation.

Script Code

Here is the complete PowerShell script. You can save this as a .ps1 file and customize it to fit your environment.

Scheduled-SystemReboot.ps1
Copy to clipboard
# SCRIPT_NAME: Scheduled-SystemReboot.ps1
# DESCRIPTION: Automates the reboot of a list of computers, with options for scheduling and batching.

# PARAMETERS:
# -ComputerList: Path to a text file containing a list of computer names (one per line).
# -RebootTime: The time of day to schedule the reboot (e.g., "02:00 AM").
# -Message: The message to display to users before the reboot.
# -Delay: The delay in minutes before the reboot begins after the scheduled time.

param(
    [string]$ComputerList,
    [string]$RebootTime = "03:00 AM",
    [string]$Message = "This system will reboot for scheduled maintenance. Please save your work.",
    [int]$Delay = 15
)

# Validate the computer list file
if (-not (Test-Path $ComputerList)) {
    Write-Host "Error: The specified computer list file does not exist." -ForegroundColor Red
    exit
}

# Read the list of computers
$computers = Get-Content -Path $ComputerList

# Calculate the reboot time
$rebootSchedule = [DateTime]::ParseExact($RebootTime, "hh:mm tt", [System.Globalization.CultureInfo]::InvariantCulture)
$rebootDate = (Get-Date).Date + $rebootSchedule.TimeOfDay
if ($rebootDate -lt (Get-Date)) {
    $rebootDate = $rebootDate.AddDays(1)
}

# Loop through each computer and schedule the reboot
foreach ($computer in $computers) {
    Write-Host "Scheduling reboot for $computer at $rebootDate..." -ForegroundColor Green
    
    # Create the scheduled job
    $action = New-ScheduledTaskAction -Execute 'PowerShell.exe' -Argument "-NoProfile -ExecutionPolicy Bypass -Command 'Restart-Computer -Force -ComputerName $computer -Wait'"
    $trigger = New-ScheduledTaskTrigger -At $rebootDate
    
    # Register the task
    try {
        Register-ScheduledTask -TaskName "AutomatedReboot-$computer" -Action $action -Trigger $trigger -Description "Automated reboot for maintenance." -Force | Out-Null
        Write-Host "Successfully scheduled reboot task for $computer." -ForegroundColor Green
    }
    catch {
        Write-Host "Failed to schedule reboot for $computer. Error: $($_.Exception.Message)" -ForegroundColor Red
    }
}

Write-Host "Script execution complete. Reboot tasks have been scheduled."

Detailed Breakdown

  • Handling Machine Lists: The script begins by accepting a file path to a list of computer names. This simple but effective approach allows you to manage hundreds or thousands of machines by simply updating a text file. This is crucial for remote management script deployment.

  • Scheduling the Reboot: The script uses the New-ScheduledTaskTrigger and New-ScheduledTaskAction cmdlets to create a scheduled task on the local machine. This task is configured to execute at the specified time (-RebootTime), which provides precise control over when the reboot occurs. For more details on these cmdlets, refer to the Microsoft Docs on Scheduled Tasks.

  • Executing the Reboot: The scheduled task action is configured to run the Restart-Computer cmdlet. This is the core command for initiating a reboot. The -Force parameter ensures the reboot happens without user confirmation, and -Wait can be used in advanced scenarios to pause script execution until the reboot is complete.

  • Error Handling: The try...catch block is a fundamental part of robust scripting. It ensures that if a scheduled task fails to register for a specific computer (e.g., the machine is offline or permissions are denied), the script will not stop. Instead, it will report the error and continue with the next machine in the list.

Potential Use Cases

  • Patch Management Cycles: After deploying Windows updates, a reboot is often required to finalize the installation. This script can be used to schedule reboots of all updated systems, ensuring a complete and successful patch cycle.

  • Scheduled Maintenance: IT departments can use this script to schedule weekly or monthly reboots of all servers during a pre-defined maintenance window, which can help clear up memory leaks and other long-running processes.

  • Remote Server Management: This script is an ideal solution for remote management of servers in a data center or cloud environment, as it eliminates the need for manual, server-by-server interaction.

Final Thoughts

This automated system reboot script is a vital tool for any system administrator's toolkit. By leveraging the power of PowerShell and Windows' native task scheduling capabilities, you can significantly reduce the administrative overhead associated with routine reboots.

Deploy this script across your Endpoints with Zecurit

Upload this script to Zecurit's Script Repository and execute it across hundreds of endpoints in minutes. Support for PowerShell, Bash, Python,and more with full audit trails and scheduling.

Frequently Asked Questions (FAQs)

  • How do I run this script with different permissions?

    The script requires administrative privileges on the target machines to create a scheduled task. You can either run the script from an elevated PowerShell console or use a service account with the necessary permissions.

  • Can I run this script on a batch of computers?

    Yes, the primary function of this script is to handle a list of computers. You simply need to provide a text file with one computer name per line using the -ComputerList parameter.

  • Why is my scheduled task not running at the correct time?

    Ensure that the system time is synchronized on both the machine where you are running the script and the target machines. Also, check for any time zone differences that might be affecting the scheduled time.

  • Is this script safe to use?

    Yes, the script is safe. It uses built-in PowerShell cmdlets and Windows features to schedule a command that reboots the computer. However, always test the script on a non-production system first and ensure you have proper communication channels in place to alert users before a reboot.

  • How can I troubleshoot a failed reboot?

    Check the Windows Event Logs on the target machine for events related to the scheduled task (under TaskScheduler/Operational) and the Restart-Computer command. These logs can provide details on why the task failed to run.