Home > OS >  How do I add a new line in a desktop notification in PowerShell
How do I add a new line in a desktop notification in PowerShell

Time:05-12

This is the project

echo $ErrorActionPreference = "Stop"
echo $notificationTitle = "Aa1234567890"
echo [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
echo $template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText01)
echo $toastXml = [xml] $template.GetXml()
echo $toastXml.GetElementsByTagName("text").AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
echo $xml = New-Object Windows.Data.Xml.Dom.XmlDocument
echo $xml.LoadXml($toastXml.OuterXml)
echo $toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
echo $toast.Tag = "Test1"
echo $toast.Group = "Test2"
echo $toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds(5)
echo $notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("A7  WIFI  (password saved in clipboard)")
echo $notifier.Show($toast);

Currently, the notification appears like this

I want it to be like this

I want to add a new line in the notification is there a way, note: I don't want to change the code I just want to know how to add a new line

CodePudding user response:

Like @Jeroen Mostert said, you have to use the template ToastText02, you will be able to use <text id="1">$Title</text> for the title and <text id="2">$Text</text> for the text in your xml

$ErrorActionPreference = "Stop"
$notificationTitle = "Aa1234567890"
$notificationText = "The password has been copied to the clipboard"
$AppID = "A7  WIFI"

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$toastXml = [xml] $template.GetXml()
($toastXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($toastXml.CreateTextNode($notificationTitle)) > $null
($toastXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($toastXml.CreateTextNode($notificationText)) > $null

$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($toastXml.OuterXml)
$toast = [Windows.UI.Notifications.ToastNotification]::new($xml)
$toast.Tag = "Test1"
$toast.Group = "Test2"
$toast.ExpirationTime = [DateTimeOffset]::Now.AddSeconds(5)
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID)
$notifier.Show($toast)
  • Related