← script library

Automate Server Reboots with PowerShell

System AdministrationDecember 24, 2024

In this post, we will share a PowerShell script that automates the process of rebooting servers across your network. Regular maintenance and reboots may be necessary to apply updates and maintain system performance. This script allows administrators to efficiently reboot specified servers, or even groups of servers, with minimal manual intervention, ensuring that your systems remain up-to-date and running smoothly. Here is the PowerShell script for automating server reboots:

powershell
# Define the list of servers to reboot
$servers = @("Server1", "Server2", "Server3")  # Replace with your server names
# Define a message for confirmation before reboot
$confirmationMessage = "This will restart the following servers: $($servers -join ', '). Do you wish to continue? (Y/N)"
# Confirm before proceeding
if ((Read-Host $confirmationMessage) -eq 'Y') {
    foreach ($server in $servers) {
        try {
            Restart-Computer -ComputerName $server -Force -ErrorAction Stop
            Write-Host "Successfully rebooted $server" -ForegroundColor Green
        } catch {
            Write-Host "Failed to reboot $server: $_" -ForegroundColor Red
        }
    }
} else {
    Write-Host "Reboot operation cancelled."
}

Run it across your fleet

This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.

Ready when you are.

Try ServerEngine free for 7 days.