Skip to content

Instantly share code, notes, and snippets.

Last active Jun 24, 2022
What would you like to do?
Toast Notification in PowerShell
function Show-Notification {
Param (
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text|where {$ -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$ -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Toast.Tag = "PowerShell"
$Toast.Group = "PowerShell"
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
Copy link

e-t-l commented Jun 1, 2021

I like this function because it's much shorter and simpler than most Toast-Notification-via-Powershell code I've found, but is it possible to add clickable buttons like these (scroll to the "In the Toaster" section)?

Copy link

dend commented Jun 18, 2021

@e-t-l you'll have to explore the command schema component for that:

I'll add this to my TODO list for future updates.

Copy link

jpanda11 commented Aug 7, 2021

Hi, @dend
I could not use the function when I run the script as a local admin on my computer, whereas it works on my normal domain account.
not sure what permission it would need.
can you please help?
Thank you :)

Exception calling "Show" with "1" argument(s): "Access is denied. (Exception from HRESULT: 0x80070005
At line:27 char:5

  • $Notifier.Show($Toast);
  • ~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : NotSpecified: (:) [], MethodInvocationException
    • FullyQualifiedErrorId : UnauthorizedAccessException

Copy link

dend commented Aug 7, 2021

@jpanda11 this seems related: Windos/BurntToast#27

Copy link

brainplot commented Aug 29, 2021

Doesn't work with PowerShell 7 but does work with Window's built-in version. Unfortunately I'm not familiar with PowerShell enough to suggest an edit. Line 11 appears to be the incompatible line.

Copy link

dend commented Aug 30, 2021

Thanks for the heads-up @brainplot - I will have to check it out with PS7.

Copy link

clrogon commented Sep 15, 2021

In case of run on remote computers?

Copy link

lenicyl commented Mar 7, 2022

Hi, this script works great for me !
However is there any way to add an icon for the toast ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment