Skip to content

Instantly share code, notes, and snippets.

@tusharsnx
Created January 16, 2025 13:22
Show Gist options
  • Save tusharsnx/822c854b2a24ee11fa177118ae8ef753 to your computer and use it in GitHub Desktop.
Save tusharsnx/822c854b2a24ee11fa177118ae8ef753 to your computer and use it in GitHub Desktop.
Powershell script to show Windows notification toast
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)
$ServiceName = "PowerShell"
$Toast.Tag = $ServiceName
$Toast.Group = $ServiceName
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$Notifier.Show($Toast);
}
# To show notification toast based on cli args, run:
# show-notification $args[0] $args[1]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment