Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save loge5/7ec41e2e2f0e0293fdcc5155499e9072 to your computer and use it in GitHub Desktop.
Save loge5/7ec41e2e2f0e0293fdcc5155499e9072 to your computer and use it in GitHub Desktop.
[Windows](PowerShell) toast notification sample
[CmdletBinding()]
Param(
[Parameter(Mandatory=$True,Position=1)]
[string]$Message,
[Parameter(Mandatory=$False,Position=2)]
[string]$Wait = 0
)
Start-Sleep -s $Wait
$ErrorActionPreference = "Stop"
$notificationTitle = $Message
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
#Convert to .NET type for XML manipuration
$toastXml = [xml] $template.GetXml()
$toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
#Convert back to WinRT type
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "PowerShell"
$toast.Group = "PowerShell"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(5)
#$toast.SuppressPopup = $true
$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