Ever wondered if you can do any notifications, balloons or toasts with Windows 10 and PowerShell? Lets say that instead of waiting for a script to finish you can just add a notification when it is done.
The only thing I haven’t solved is the toast to stay in the Action Center in Windows 10, the notification will just show for x amount of seconds and then leave no traces.
There is a second way and this is to have a notification as a tray icon, this icon will stay until you close it, but it will be hidden behind the “arrow”, like the nice picture below.
First example is the normal toast. I will not go into details but there are some highlights I made in the comments
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
# Toasts templates: https://msdn.microsoft.com/en-us/library/windows/apps/hh761494.aspx
$template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
# Convert to .NET type for XML manipuration
$toastXml = [xml] $template.GetXml()
# Customize the toast message
$toastXml.GetElementsByTagName(“text”)[0].AppendChild($toastXml.CreateTextNode(“Script test”)) > $null
$toastXml.GetElementsByTagName(“text”)[1].AppendChild($toastXml.CreateTextNode(“Customizated notification: ” + [DateTime]::Now.ToShortTimeString())) > $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)
# Unique Application id/tag and group
$toast.Tag = “PowerShell UI”
$toast.Group = “PowerShell UI”
#$toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(50)
# This will suppress the popup/toast, and should only view the toast in the Action Center, but it does not work with PowerShell
#$toast.SuppressPopup = $true
# Use this to register an event, but it does not work on PowerShell. This example should display a messagebox if you click the toast in the Action Center
#Register-ObjectEvent -InputObject $toast -EventName Activated -SourceIdentifier Activated_Event -Action {[System.Windows.Forms.MessageBox]::Show(“Clicked”,”Information”);}
# Create the toats and show the toast. Make sure to include the AppId
$notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($toast.Tag)
$notifier.Show($toast);
Example 2, this will add a notification from the system tray. Normally this should stay in the Action Center, but again does not work with PowerShell
[void] [System.Reflection.Assembly]::LoadWithPartialName(“System.Windows.Forms”)
# Remove and Unregister events if created earlier. Tip, remove the events you haven’t created earlier
#Remove-Event BalloonClicked_event -ea SilentlyContinue
#Unregister-Event -SourceIdentifier BalloonClicked_event -ea silentlycontinue
#Remove-Event BalloonClosed_event -ea SilentlyContinue
#Unregister-Event -SourceIdentifier BalloonClosed_event -ea silentlycontinue
Remove-Event Clicked_event -ea SilentlyContinue
Unregister-Event -SourceIdentifier Clicked_event -ea silentlycontinue
# Create the object and customize the message
$objNotifyIcon = New-Object System.Windows.Forms.NotifyIcon
$objNotifyIcon.Icon = [System.Drawing.SystemIcons]::Error
$objNotifyIcon.BalloonTipIcon = “Info”
$objNotifyIcon.BalloonTipTitle = “Script test”
$objNotifyIcon.BalloonTipText = “Customizated notification: ” + [DateTime]::Now.ToShortTimeString()
$objNotifyIcon.Text = “Mouse over system tray icon text”
# This will show or hide the icon in the system tray
$objNotifyIcon.Visible = $True
# Register a click event with action to take based on event, you can use the following events BalloonTipClicked, BalloonTipClosed, Click
# System tray icon clicked – will hide the system tray icon
Register-ObjectEvent -InputObject $objNotifyIcon -EventName Click -SourceIdentifier Clicked_event -Action {[System.Windows.Forms.MessageBox]::Show(“Clicked”,”Information”);$objNotifyIcon.Visible = $False} | Out-Null
# This is the show the notification
$objNotifyIcon.ShowBalloonTip(1000)
Happy coding, let me know if you have any questions!
Some interesting resources:
https://msdn.microsoft.com/en-us/library/windows/apps/hh802768.aspx
https://msdn.microsoft.com/en-us/library/windows/apps/hh465391.aspx
Leave a comment