Reboot Device and Notify User

# Function to show a message box
function Show-MessageBox {
    param (
        [string]$message,
        [string]$title = "Notification"
    )
    Add-Type -AssemblyName System.Windows.Forms
    [System.Windows.Forms.MessageBox]::Show($message, $title, [System.Windows.Forms.MessageBoxButtons]::OKCancel, [System.Windows.Forms.MessageBoxIcon]::Information)
}

# Notify user of the upcoming reboot
$message = "The system will reboot in 1 minute. Please save your work."
$title = "System Reboot Notification"

# Show the message box
$userResponse = Show-MessageBox -message $message -title $title

# Check if the user clicked 'OK'
if ($userResponse -eq [System.Windows.Forms.DialogResult]::OK) {
    Write-Output "User acknowledged the reboot. Proceeding with reboot..."
    
    # Wait for 60 seconds before rebooting
    Start-Sleep -Seconds 60
    
    # Reboot the system
    Restart-Computer -Force
} else {
    Write-Output "User canceled the reboot."
}

Explanation of the Script Steps

1. Show-MessageBox Function: This function creates a simple message box using the System.Windows.Forms assembly. It displays a notification message with OK and Cancel buttons.

2. Notify User: The script sets the notification message, indicating that the system will reboot in 1 minute.

3. User Response: The script waits for the user to respond to the message box. If the user clicks OK, the script proceeds to wait for 60 seconds and then reboots the system.

4. Cancel Option: If the user clicks Cancel, the script outputs a message indicating that the reboot has been canceled.

Running the Script

1. Open PowerShell as an administrator.

2. Copy and paste the above script into the PowerShell window.

3. Press Enter to run the script.

Important Notes

• User Notification: This script ensures the user is informed and can choose to cancel the reboot if necessary.

• Customization: You can adjust the countdown timer by changing the Start-Sleep -Seconds 60 line to a different duration as needed.

Last updated