PowerShell Command to Restart a Device with Task Scheduler.

Task Scheduler Script to be pushed from SCCM or through GPO.
$taskAction = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-File C:\Path\To\NotifyAndRestart.ps1"
$taskTrigger = New-ScheduledTaskTrigger -Daily -At "02:00AM"  # Set the desired time for the task to run
$taskSettings = New-ScheduledTaskSettingsSet -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -AllowHardTerminate -StartWhenAvailable

$taskPrincipal = New-ScheduledTaskPrincipal -UserId "SYSTEM" -LogonType ServiceAccount
$task = New-ScheduledTask -Action $taskAction -Trigger $taskTrigger -Settings $taskSettings -Principal $taskPrincipal

Register-ScheduledTask -TaskName "NotifyAndRestartTask" -InputObject $task

Powershell to restart the client machine that has an uptime of more than 2 days.

This script includes:

  1. A check for uptime greater than 2 days.
  2. Two notifications: one immediately and another 15 minutes before the restart.
  3. A 30-minute grace period before the restart.
$os = Get-CimInstance Win32_OperatingSystem
$lastBootUpTime = $os.LastBootUpTime
$currentDate = Get-Date
$uptimeDays = ($currentDate - $lastBootUpTime).TotalDays

if ($uptimeDays -ge 2) {
    # First Notification
    $firstMessage = "Your computer has been online for more than 2 days. It will restart in 30 minutes to ensure optimal performance. Please save your work."
    $title = "Scheduled Restart Notification"
    
    [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
    $notification = New-Object system.windows.forms.notifyicon
    $notification.icon = [System.Drawing.SystemIcons]::Information
    $notification.BalloonTipText = $firstMessage
    $notification.BalloonTipTitle = $title
    $notification.Visible = $true
    $notification.ShowBalloonTip(60000)  # Display the first notification for 1 minute

    # Wait for 15 minutes before sending the second notification
    Start-Sleep -Seconds 900

    # Second Notification
    $secondMessage = "Reminder: Your computer will restart in 15 minutes. Please save your work."
    $notification.BalloonTipText = $secondMessage
    $notification.ShowBalloonTip(60000)  # Display the second notification for 1 minute

    # Wait for the remaining 15 minutes before restarting (30 minutes total - 15 minutes already waited)
    Start-Sleep -Seconds 900

    # Restart the computer
    Restart-Computer -Force
}

Powershell to restart the client machine that has an uptime of more than 2 days.

This script includes:

  1. A check for uptime greater than X Hours.
  2. Two notifications: one immediately and another 15 minutes before the restart.
  3. A 30-minute grace period before the restart.
$os = Get-CimInstance Win32_OperatingSystem
$lastBootUpTime = $os.LastBootUpTime
$currentDate = Get-Date
$uptimeHours = ($currentDate - $lastBootUpTime).TotalHours

if ($uptimeHours -ge 3) {
    # Display the first notification
    $firstMessage = "Your computer has been online for more than 3 hours. It will restart in 30 minutes to ensure optimal performance. Please save your work."
    $title = "Scheduled Restart Notification"
    
    [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
    $notification = New-Object system.windows.forms.notifyicon
    $notification.icon = [System.Drawing.SystemIcons]::Information
    $notification.BalloonTipText = $firstMessage
    $notification.BalloonTipTitle = $title
    $notification.Visible = $true
    $notification.ShowBalloonTip(60000)  # Display the first notification for 1 minute

    # Wait for 15 minutes before sending the second notification
    Start-Sleep -Seconds 900

    # Display the second notification
    $secondMessage = "Reminder: Your computer will restart in 15 minutes. Please save your work."
    $notification.BalloonTipText = $secondMessage
    $notification.ShowBalloonTip(60000)  # Display the second notification for 1 minute

    # Wait for the remaining 15 minutes before restarting (30 minutes total - 15 minutes already waited)
    Start-Sleep -Seconds 900

    # Restart the computer
    Restart-Computer -Force
}
Same for minutes with one notification.
$os = Get-CimInstance Win32_OperatingSystem
$lastBootUpTime = $os.LastBootUpTime
$currentDate = Get-Date
$uptimeMinutes = ($currentDate - $lastBootUpTime).TotalMinutes

if ($uptimeMinutes -ge 5) {
    # Display a notification to the user
    $message = "Your computer has been online for more than 5 minutes. It will restart in 3 minutes to ensure optimal performance. Please save your work."
    $title = "Scheduled Restart Notification"
    
    [System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null
    $notification = New-Object system.windows.forms.notifyicon
    $notification.icon = [System.Drawing.SystemIcons]::Information
    $notification.BalloonTipText = $message
    $notification.BalloonTipTitle = $title
    $notification.Visible = $true
    $notification.ShowBalloonTip(60000)  # Display notification for 1 minute

    # Wait for 3 minutes (180 seconds) to give users time to save their work
    Start-Sleep -Seconds 180

    # Restart the computer
    Restart-Computer -Force
}

Leave a Comment