Skip to content

Instantly share code, notes, and snippets.

@dend
Last active April 2, 2024 14:50
Show Gist options
  • Star 66 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save dend/5ae8a70678e3a35d02ecd39c12f99110 to your computer and use it in GitHub Desktop.
Save dend/5ae8a70678e3a35d02ecd39c12f99110 to your computer and use it in GitHub Desktop.
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);
}
@TotallyInformation
Copy link

@Xavron
Thanks for that. Somewhat bizarre that you need such an overhead just to show a Toast! But hey, a workaround anyway. Thanks.

@Xavron
Copy link

Xavron commented Apr 2, 2024

Np. Between this and other silly things, I think my head is spinning :)P

(I know people in the linked issue thread seem to be feeling its not PS at fault... but ultimately its PS that has the problem so it is at fault. Kind of feels like Linux's xorg verses Wayland destruction that is still picking up the pieces after many years. Can't do anything about it but deal with workarounds. PS devs should code in another way but I get the impression they won't from its not their problem type of thinking that is prevalent among many devs. Its only legit to me if they wanted to cut out lots of uses.)

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