Skip to content

Instantly share code, notes, and snippets.

@dend
Last active Jun 24, 2022
Embed
What would you like to do?
Toast Notification in PowerShell
function Show-Notification {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
[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 {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$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")
$Notifier.Show($Toast);
}
@e-t-l
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)?

@dend
Copy link
Author

dend commented Jun 18, 2021

@e-t-l you'll have to explore the command schema component for that: https://docs.microsoft.com/en-us/uwp/schemas/tiles/toastschema/element-command

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

@jpanda11
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
(E_ACCESSDENIED))"
At line:27 char:5

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

@dend
Copy link
Author

dend commented Aug 7, 2021

@jpanda11 this seems related: Windos/BurntToast#27

@brainplot
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.

@dend
Copy link
Author

dend commented Aug 30, 2021

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

@clrogon
Copy link

clrogon commented Sep 15, 2021

In case of run on remote computers?

@lenicyl
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