Automate Server Reboots with PowerShell

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:

# 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."
}