I have this script, I'm bringing it to you and I want to add to the shutdown alert message that is displayed a timer that shows how much time is left to shutdown
Without touching the script itself with something else unrelated



Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Diagnostics.Stopwatch

# Set up logging
$logFile = "C:\ShutdownTimer.log"

function Write-Log {
    param([string]$message)
    $timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss.fff"
    "$timestamp - $message" | Out-File -FilePath $logFile -Append
}

Write-Log "Script started"

# Wait for system to stabilize
$stabilizationTime = 300 # 5 minutes
$script:startTime = Get-Date

# Define idle threshold and warning time
$idleThreshold = New-TimeSpan -Minutes 15
$warningTime = New-TimeSpan -Minutes 5

# Create a Stopwatch for accurate time measurement
$idleStopwatch = [System.Diagnostics.Stopwatch]::new()
$idleStopwatch.Start()

# Add Windows API definitions for mouse movement monitoring
Add-Type @"
    using System;
    using System.Runtime.InteropServices;
    public class Win32 {
        [DllImport("user32.dll")]
        public static extern bool GetCursorPos(out POINT lpPoint);
    }
    public struct POINT {
        public int X;
        public int Y;
    }
"@

$lastPosition = New-Object POINT

function Reset-IdleTime {
    $idleStopwatch.Restart()
}

function Check-MouseMovement {
    $currentPosition = New-Object POINT
    [Win32]::GetCursorPos([ref]$currentPosition) | Out-Null
    $currentTime = Get-Date
    
    if (($currentTime - $script:startTime).TotalSeconds -ge $stabilizationTime) {
        if ($currentPosition.X -ne $lastPosition.X -or $currentPosition.Y -ne $lastPosition.Y) {
            Write-Log "Mouse movement detected"
            Reset-IdleTime
            $global:lastPosition = $currentPosition
        }
    } else {
        Reset-IdleTime
    }
}

function Show-ShutdownWarning {
    Write-Log "Displaying shutdown warning"
    $form = New-Object System.Windows.Forms.Form
    $form.Text = "Shutdown Warning"
    $form.Size = New-Object System.Drawing.Size(300,150)
    $form.StartPosition = "CenterScreen"
    $form.TopMost = $true

    $label = New-Object System.Windows.Forms.Label
    $label.Location = New-Object System.Drawing.Point(10,20)
    $label.Size = New-Object System.Drawing.Size(280,40)
    $label.Text = "המחשב יכבה בעוד 5 דקות."
    $label.TextAlign = [System.Drawing.ContentAlignment]::MiddleCenter
    $form.Controls.Add($label)

    $cancelButton = New-Object System.Windows.Forms.Button
    $cancelButton.Location = New-Object System.Drawing.Point(100,80)
    $cancelButton.Size = New-Object System.Drawing.Size(100,23)
    $cancelButton.Text = "ביטול כיבוי"
    $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
    $form.Controls.Add($cancelButton)
    $form.CancelButton = $cancelButton

    $warningStartTime = Get-Date
    $warningTimer = New-Object System.Windows.Forms.Timer
    $warningTimer.Interval = 1000 # Check every second
    $warningTimer.Add_Tick({
        $elapsedWarningTime = (Get-Date) - $warningStartTime
        if ($elapsedWarningTime -ge $warningTime) {
            $form.DialogResult = [System.Windows.Forms.DialogResult]::OK
            $form.Close()
        }
    })
    $warningTimer.Start()

    $result = $form.ShowDialog()
    $warningTimer.Stop()
    
    if ($result -eq [System.Windows.Forms.DialogResult]::Cancel) {
        Write-Log "Shutdown cancelled by user"
        Reset-IdleTime
        return $false
    }
    
    return $true
}

$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 100  # Check every 100 milliseconds
$timer.Add_Tick({
    Check-MouseMovement
    $idleTime = $idleStopwatch.Elapsed
    
    if ($idleTime -ge $idleThreshold) {
        $timer.Stop()
        $shouldShutdown = Show-ShutdownWarning
        if ($shouldShutdown) {
            Write-Log "Initiating system shutdown"
            Stop-Computer -Force
        }
        else {
            Reset-IdleTime
            $timer.Start()
        }
    }
})

$timer.Start()

$appContext = New-Object System.Windows.Forms.ApplicationContext
[System.Windows.Forms.Application]::Run($appContext)