3

I have a script that is associated with a keyboard shortcut, when I trigger the shortcut, I want a notification to appear confirming that the script executed.

How do I create a notification from inside a script?

EDIT

On Linux, I used to do command && notify-send "My message"

1
  • echo msgbox "the message that you want" > "%temp%\popup.vbs" wscript.exe "%temp%\popup.vbs"
    – Aardwolf
    Commented Mar 17, 2020 at 22:24

1 Answer 1

7

In PowerShell you can run (from here):

[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"Script Completed!","Your script ran succesfully!",[system.windows.forms.tooltipicon]::None)

Or this (from here):

$ErrorActionPreference = "Stop"
$notificationTitle = "Notification: Your script has been completed successfully"
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "Test1"
$toast.Group = "Test2"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds(5)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("Script Completed!")
$notifier.Show($toast);
9
  • Is there a way to auto clear it after a few seconds?
    – olfek
    Commented Mar 17, 2020 at 22:39
  • Also, I don't want an icon to show in the taskbar, notification only.
    – olfek
    Commented Mar 17, 2020 at 22:47
  • @daka, added 2nd method that has does show in the taskbar and has an expireing-seconds parts Commented Mar 18, 2020 at 0:36
  • Regarding the second way, removing Notification: from $notificationTitle makes it stop working, why?
    – olfek
    Commented Mar 18, 2020 at 10:57
  • @daka works for me (see i.imgur.com/AzjMFwu.png). Did you make sure to leave the opening quotes before it? Commented Mar 18, 2020 at 12:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .