Create notification from command line in Windows 10

9,082

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);
Share:
9,082

Related videos on Youtube

daka
Author by

daka

Updated on September 18, 2022

Comments

  • daka
    daka over 1 year

    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"

    • Aardwolf
      Aardwolf about 4 years
      echo msgbox "the message that you want" > "%temp%\popup.vbs" wscript.exe "%temp%\popup.vbs"
  • daka
    daka about 4 years
    Is there a way to auto clear it after a few seconds?
  • daka
    daka about 4 years
    Also, I don't want an icon to show in the taskbar, notification only.
  • Yisroel Tech
    Yisroel Tech about 4 years
    @daka, added 2nd method that has does show in the taskbar and has an expireing-seconds parts
  • daka
    daka about 4 years
    Regarding the second way, removing Notification: from $notificationTitle makes it stop working, why?
  • Yisroel Tech
    Yisroel Tech about 4 years
    @daka works for me (see i.imgur.com/AzjMFwu.png). Did you make sure to leave the opening quotes before it?
  • daka
    daka about 4 years
    Yep I did, very weird.
  • Wasif
    Wasif over 3 years
    To clear it after a few seconds, Start-Sleep then .Dispose().
  • Ben Racicot
    Ben Racicot almost 3 years
    @YisroelTech any way to keep it from disappearing? I'm trying to work on some tasks WHILE a notif is open. Perhaps making it manual close only by the "X" button?
  • Yisroel Tech
    Yisroel Tech almost 3 years
    @BenRacicot I do not believe so