In this quick post I will demonstrate a custom toast notification created using WPF and PowerShell. This is not the built-in Windows 10 toast notification created using the [Windows.UI.Notifications] namespace (check out the excellent BurntToast module for that), but simply to demonstrate how to create something similar in code that would also work in older operating systems like Windows 7, and that is completely customisable without any predefined style templates.
To add an image I recommend to convert the image to a base64 string. This means you can include the image in the script and distribute it without having to include any additional files. To create a base64 string from an image file, use the following code, then use Out-File to save the $Base64 variable to a text file. You can then copy and paste the content of the text file into the $Base64 variable in the notification script.
$File = "C:\Users\tjones\Pictures\smsagent.png" $Image = [System.Drawing.Image]::FromFile($File) $MemoryStream = New-Object System.IO.MemoryStream $Image.Save($MemoryStream, $Image.RawFormat) [System.Byte]$Bytes = $MemoryStream.ToArray() $Base64 = [System.Convert]::ToBase64String($Bytes) $Image.Dispose() $MemoryStream.Dispose()
You can customise a few parameters such as height and width, image size and text content at the top of the script. In the example, I have set the property ‘IsHitTestVisible’ to $False on the textboxes so that you can click anywhere on the notification to open the webpage, but the beauty here is you can customise this as you like:
- perform whatever action you want or none at all
- add your own custom content to the notification, including any WPF element
- change the animation style
There are a couple of limitations:
- I haven’t added support for touch devices, ie swipe to dismiss
- The notification won’t move to the Action Center in Windows 10 on expiry
Here’s the code: