PowerShell can I use balloons, toasts and notifications?

Posted by

image

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.

image image

 

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/windows.ui.notifications.toastnotificationmanager.aspx?f=255&MSPPError=-2147217396

https://msdn.microsoft.com/en-us/library/windows/apps/hh802768.aspx

https://msdn.microsoft.com/en-us/library/windows/apps/hh465391.aspx

Advertisements

6 comments

  1. It is me again… 🙂

    For Balloon Tip to stay in Action center, use:

    $AppID = ‘Microsoft.Explorer.Notification.{B0AA627D-AE34-F5C9-9971-19C8E1D372A3}’

    To that registry snippet after that, powershell generated Ballon Tips would stay in Action Center

    Like

  2. Sorry, made mistake, correct entry would be:

    Set-ItemProperty “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$AppID” -Name “ShowInActionCenter” -Type Dword -Value “1”

    No need to chop up $AppID, it is exact string what we need.

    Liked by 1 person

  3. Or enable via registry, after first Notification has shown:

    Set-ItemProperty “HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings\$($AppID.Split(‘\’)[0])\WindowsPowerShell\v1.0\powershell.exe” -Name “ShowInActionCenter” -Type Dword -Value “1”

    Like

  4. You can have them in Action Center by a little code change in first example:

    Comment out:
    #$Toast.Tag = “PowerShell”

    Add:
    $AppID = ( ((Get-StartApps -Name ‘*PowerShell*’) | Select-Object -First 1).AppId )

    Change:
    $Toast.Group = $AppID

    Final change:
    [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($AppID).Show($Toast)

    Short Description of the actual problem is, that you use “PowerShell UI” as appID, which is not valid application and does not exist in Start Menu.
    After that code change head into Notifictation Settings and it lists powershell as notifier, now just enable it and everything works as expected.

    Liked by 1 person

Leave a comment

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.