Skip to content

Instantly share code, notes, and snippets.

@neuralpain
Created June 8, 2024 04:53
Show Gist options
  • Save neuralpain/283a2de1e7078c95e0c97a4fb6cc0e08 to your computer and use it in GitHub Desktop.
Save neuralpain/283a2de1e7078c95e0c97a4fb6cc0e08 to your computer and use it in GitHub Desktop.
Simple Toast Notification for Windows
<#
.SYNOPSIS
Creates and displays a Windows Toast notification
.DESCRIPTION
Creates a Windows Toast notification and displays it to the user.
.PARAMETER ToastTitle
The title of the Toast notification
.PARAMETER ToastText
The text of the Toast notification
.EXAMPLE
New-ToastNotification "Hello World" "This is a Toast notification"
Creates and displays a Toast notification with the title "Hello World" and the text "This is a Toast notification"
.NOTES
The function requires Windows 10 build 10586 (Anniversary Update) or higher
#>
function New-ToastNotification {
[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-Object { $_.id -eq "1" }).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text | Where-Object { $_.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);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment