Forked from Onurtag/ToastNotification_Windows10.ps1
Created
November 5, 2024 20:13
-
-
Save pa-0/1f58dd6e2a04aa1403e05abd19a47757 to your computer and use it in GitHub Desktop.
Windows 10 toast notification sample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Original source: https://gist.github.com/IISResetMe/79b38678ecf6339ed4f222caeae8d490 | |
function Show-ToastNotification { | |
<# | |
.Synopsis | |
Show a notification toast. | |
.DESCRIPTION | |
Show a notification toast. | |
.EXAMPLE | |
Show-ToastNotification -Title "Toast Title" -Message "Message 11111","Message 22222" -Thumbnail "C:\test.png" -AppName "Toast App Name" | |
.EXAMPLE | |
ls *ps1 | Show-ToastNotification -Title "Toast Title" -Thumbnail "C:\test.png" -AppName "Toast App Name" | |
#> | |
param( | |
#Toast message. Also pipeline value (use responsibly) | |
[Parameter(Mandatory = $false, ValueFromPipeline)] | |
[string[]]$Message = "Test", | |
#Toast title | |
[Parameter(Mandatory = $false)] | |
[string]$Title = "", | |
#App and group name | |
[Parameter(Mandatory = $false)] | |
[string]$AppName = "PowerShell", | |
#Thumbnail image | |
[Parameter(Mandatory = $false)] | |
[ValidateScript({ Test-Path $_ -PathType Leaf })] | |
[string]$Thumbnail, | |
#Timer in seconds. Disabled by default. | |
#When This timer ends the notification will disappear from the action center. | |
#This timer is NOT about "when the notification will disappear from the screen" | |
[Parameter(Mandatory = $false)] | |
[int]$Timer = 0, | |
#Notification tag within the app group (AppName). Disabled by default. | |
#Newer notifications with the same tag override older notifications. | |
[Parameter(Mandatory = $false)] | |
[string]$Tag = "", | |
#Toast template. Selected automatically if not provided. | |
[Parameter(Mandatory = $false)] | |
[Windows.UI.Notifications.ToastTemplateType]$Template = 'ToastText01' | |
) | |
process { | |
if (-not $PSBoundParameters.ContainsKey('Template')) { | |
$Template = if ($PSBoundParameters.ContainsKey('Thumbnail')) { | |
if ($PSBoundParameters.ContainsKey('Message')) { | |
if ($Message.Count -ge 2) { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText04 | |
} | |
else { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText02 | |
} | |
} | |
else { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastImageAndText01 | |
} | |
} | |
else { | |
if ($PSBoundParameters.ContainsKey('Message')) { | |
if ($Message.Count -ge 2) { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastText04 | |
} | |
else { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastText02 | |
} | |
} | |
else { | |
[Windows.UI.Notifications.ToastTemplateType]::ToastText01 | |
} | |
} | |
} | |
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null | |
$TemplateContent = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent($Template) | |
$toastXml = [xml]$TemplateContent.GetXml() | |
$MessageLine1, $MessageLine2, $null = $Message | |
if ($Node1 = $toastXml.SelectSingleNode('//text[@id = ''1'']')) { | |
$Node1.AppendChild($toastXml.CreateTextNode($Title)) > $null | |
} | |
if ($Node2 = $toastXml.SelectSingleNode('//text[@id = ''2'']')) { | |
$Node2.AppendChild($toastXml.CreateTextNode($MessageLine1)) > $null | |
} | |
if ($Node3 = $toastXml.SelectSingleNode('//text[@id = ''3'']')) { | |
$Node3.AppendChild($toastXml.CreateTextNode($MessageLine2)) > $null | |
} | |
if ($NodeImg = $toastXml.SelectSingleNode('//image[@id = ''1'']')) { | |
$NodeImg.SetAttribute('src', $Thumbnail) > $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.Group = $AppName | |
if ($Tag) { | |
$toast.Tag = $Tag | |
} | |
if ($Timer -gt 0) { | |
$toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds($Timer) | |
} | |
# $toast.ExpiresOnReboot = $false | |
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppName) | |
$notifier.Show($toast); | |
} | |
} | |
# Export-ModuleMember -Function Show-ToastNotification |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment