Search notes:

notif.ps1

This is a PowerShell script that opens a message box (msg.exe) at a specified date in the future
The scheduled time can be specified absolutely…
notif 17:00 'time to go home'
… or relatively. In the following example, the message box pops up in 7 minutes:
notif +0:07 'do xyz'
Using date arithmetic to run the notification tomorrow at 10:58.
notif ([dateTime]::today + '1.10:58')  'release meeting'
The -g option was originally used for rudimentary debugging. It now simpily shows the scheduled messages:
notif -g

Source code

#
#  V0.6
#
[cmdletBinding(defaultParameterSetName = 'set')]
param (

   [parameter(
        mandatory        = $false,
        parameterSetName = '-g')]
       [switch]                       $g,

   [parameter(
        mandatory        = $true,
        position         =  1,
        parameterSetName = 'set')]
       [string]                       $when,

   [parameter(
        mandatory        = $true,
        position         =  2,
        parameterSetName = 'set')]
       [string]                       $msg
)

set-strictMode -version latest

#  How long the message box is shown, in seconds.
$msgBoxShownSecs = 10000

if ($g) {
   $actions = get-scheduledTask | where-object taskname -match 'notif_\d{4}-\d{2}-\d{2}_\d{2}-\d{2}'
   foreach ($action in $actions) {
      "$($action.taskName)  $($action.actions[0].arguments)"
   }
   return
}

if ($when.substring(0, 1) -eq '+') {
  [timespan] $span = $when.substring(1)
  [datetime] $dtWhen = (get-date) + $span
}
else {
  [datetime] $dtWhen = $when
  if ($dtWhen -lt (get-date)) {
    write-textInConsoleErrorColor "specified date $dtWhen is in the past"
    return
  }
}

$trg = new-scheduledTaskTrigger   -once -at $dtWhen

$trg.endBoundary = $dtWhen.addSeconds(1).toString('s')

$set = new-scheduledTaskSettingsSet    `
   -deleteExpiredTaskAfter    00:00:01 `
   -allowStartIfOnBatteries


$act = new-scheduledTaskAction -execute "cmd" -argument "/c msg $env:username /time:$msgBoxShownSecs $msg"

$null = register-scheduledTask                               `
   -force                                                    `
   -taskName "notif_$($dtWhen.toString('yyyy-MM-dd_HH-mm'))" `
   -Trigger    $trg                                          `
   -action     $act                                          `
   -settings   $set
Github repository scripts-and-utilities, path: /notif.ps1

History

2021-04-08 Fixed error The task XML contains a value which is incorrectly formatted or out of range.
2021-05-27 Added -g option to display already scheduled notifications.
2021-06-29 Added the possibility to schedule a notification with a relative time.
2021-07-14 V.4: Create parameter sets to distinguish between -g or using notif to schedule a notification
2021-12-02 V.5: Show message box for 10000 seconds (Default of msg.exe is one minute).
2024-02-06 V.6: Use write-textInConsoleErrorColor if specified date is already in the past.

See also

notify_me_at.bat is a similar script for cmd.exe
register-scheduledTask
In order to trigger the execution of a VBA sub in the future, application.onTime can be used.

Index