Toast notifications are a great way to provide users with timely information. This PowerShell script demonstrates how to create and display a toast notification in Windows 10.
Prerequisites
Before you start, ensure you have:
- Windows 10 with PowerShell.
- A custom logo image (e.g., Logo.jpg) in the script’s directory.
PowerShell Script
Here is the PowerShell script to create a Windows 10 toast notification:
powershellCopier le code# Get the current path of the script
$CurrentPath = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
# Define notification parameters
$notificationTitle = "Migration Windows 10"
$Scenario = "short" # Possible values: reminder | short | long
$Logo = $CurrentPath + "\Logo.jpg"
$AttributionText = "Upgrade"
$TitreToast = "LANDESK"
$TitleMessage = "Windows 10 upgrade"
$Message1 = "The Windows 10 migration has started"
$Message2 = "Please do not restart your computer"
$TextBouton = "Close"
# Define the XML for the toast notification
$Toast = @"
<toast scenario="$Scenario">
<visual>
<binding template="ToastGeneric">
<image id="1" placement="appLogoOverride" hint-crop="circle" src="$Logo"/>
<text placement="attribution">$AttributionText</text>
<text>$TitreToast</text>
<group>
<subgroup>
<text hint-style="title" hint-wrap="true">$TitleMessage</text>
</subgroup>
</group>
<group>
<subgroup>
<text hint-style="body" hint-wrap="true">$Message1</text>
</subgroup>
</group>
<group>
<subgroup>
<text hint-style="body" hint-wrap="true">$Message2</text>
</subgroup>
</group>
</binding>
</visual>
<actions>
<action activationType="system" arguments="dismiss" content="$TextBouton"/>
</actions>
</toast>
"@
# Load the necessary Windows Runtime assemblies
$Load = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime]
$Load = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime]
# Define the application ID (PowerShell)
$App = "{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe"
# Load the notification into the required format
$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument
$ToastXml.LoadXml($Toast.OuterXml)
# Display the toast notification
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml)
Using the Script
- Download and Prepare the Script:
- Save the above script into a file named
ShowToastNotification.ps1
. - Ensure you have a logo image (e.g.,
Logo.jpg
) in the same directory as the script.
- Save the above script into a file named
- Run the Script:
- Open PowerShell with administrative rights.
- Navigate to the directory where the script is saved.
- Execute the script:powershellCopier le code
.\ShowToastNotification.ps1
Explanation of the Script
- Current Path: The script determines its current directory to locate the logo image.
- Notification Parameters: The script sets the title, scenario, logo, attribution text, main title, messages, and button text.
- Toast XML: The XML structure defines the layout and content of the toast notification.
- Windows Runtime Assemblies: These are loaded to create and display the notification.
- Application ID: Identifies PowerShell as the source of the notification.
- Show Notification: The notification is displayed using the
Show
method.
https://github.com/DavidWuibaille/Repository/tree/main/Windows/Windows11/ToastNotification
Last Updated :
09 Sep, 2024
Python is a versatile programming language that can be used for various applications, including desktop notifications. The win10toast
package is a handy tool for creating simple and effective notifications on Windows 10. This article will guide you through installing and using win10toast
to create desktop notifications.
What is win10toast
?
win10toast
is a Python package that allows you to create desktop notifications for Windows 10. Notifications can include a title, message, duration, and an optional icon. This is useful for alerting users to important events or updates without requiring them to open your application.
win10toast
To get started, you need to install the win10toast package. You can install it using pip:
pip install win10toast
About
show_toast()
function:
Syntax: show_toast(title=’Notification’, message=’Here comes the message’, icon_path=None, duration=5, threaded=False) Parameters: title: It contains notification title. message: It contains notification message. icon_path: It contains the path to .ico file. duration«: It specifies the notification destruction active duration.
To create notifications we have to import the win10toast module. Then create an object to
ToastNotifier
class and by using the method
show_toast
we create a notification. It contains
header
or title of that notification, actual message, duration of that notification and icon for that notification.
show_toast
method is a instance of notification settings.
Code #1:
Python
# import win10toast from win10toast import ToastNotifier # create an object to ToastNotifier class n = ToastNotifier() n.show_toast("GEEKSFORGEEKS!", "You got notification", duration = 10, icon_path ="https://media.geeksforgeeks.org/wp-content/uploads/geeks.ico")
Output:
Code #2:
Python
# import win10toast from win10toast import ToastNotifier # create an object to ToastNotifier class n = ToastNotifier() n.show_toast("GEEKSFORGEEKS", "Notification body", duration = 20, icon_path ="https://media.geeksforgeeks.org/wp-content/uploads/geeks.ico")
Output:
An easy-to-use Python library for displaying Windows 10 Toast Notifications which is useful for Windows GUI development.
from win10toast import ToastNotifier
toaster = ToastNotifier()
toaster.show_toast("Hello World!!!",
"Python is 10 seconds awsm!",
icon_path="custom.ico",
duration=10)
toaster.show_toast("Example two",
"This notification is in it's own thread!",
icon_path=None,
duration=5,
threaded=True)
# Wait for threaded notification to finish
while toaster.notification_active(): time.sleep(0.1)
System events on Windows 10 trigger toast notifications. These notifications can also be sent by apps to let you know, for example, that you have a new email message. Developers have the option to add support for the Windows 10 toast notifications. They’re pretty useful but can only be triggered by a system or app event. You cannot configure notifications to appear in response to, for example, a scheduled task running. There’s no built-in UI that can do that. The good news is, it’s not that hard to show a custom toast notification on Windows 10. All you need is a simple PowerShell script.
This tutorial works on Windows 10. You must be running PowerShell version 5+. You must have administrative rights on your system.
Check PowerShell Version
Open PowerShell. In Windows search, type PowerShell, right-click it, and select ‘Run as administrator’, from the context menu. In the PowerShell window, type the following;
Get-Host
Look at what the Version line returns to check the PowerShell Version. If you’re running the latest version of Windows 10 i.e. the Fall Creators Update, you probably have PowerShell v5.
Install BurntToast Module
Make sure you’re running PowerShell with administrative rights. In order to show custom toast notifications on Windows 10, you will need to install the BurntToast module. This module lets you create custom toast notifications on Windows 10 with little to no effort, and zero coding skills.
In PowerShell, enter the following,
Install-Module -Name BurntToast
You will, likely get a message that says you need to install the NuGet provider. If you do, simply type in Y to proceed and PowerShell will take care of the rest. Once it’s installed, run the above command again.
This time, you will likely get a message saying you’re installing a module from an untrusted repository. Again, type Y to proceed. The module will now be installed.
Now that you’ve installed the module, you can create your custom toast notification. A custom toast notification on Windows 10 has three parts that you need to concern yourself with;
- A title
- The message body
- An icon
Find a nice JPG or PNG image that you can use for the icon. The icon will appear in the toast notification. All the notifications will be sent from PowerShell so it’s a good idea to use an icon that will tell you a bit about what the notification is for.
Open Notepad and paste the following in it;
New-BurntToastNotification -Text "Title of notifications", 'Body of notification' -AppLogo path to your icon image
Replace Title of notification with the title of your notification. Do not remove any of the inverted commas or apostrophes. Simple replace the text where required. Save the file with the ps1 file extension.
The following is the custom toast notification we generated using BurntToast;
New-BurntToastNotification -Text "Power cable plugged/Unplugged", 'Power source has changed' -AppLogo C:\Users\fatiw\Desktop\power-plug-png-image-69656.png
When you run the PS1 file (with PowerShell), the toast notification will appear on your screen.
You can dismiss it like other Windows 10 notifications. In the Action Center, these notifications will all appear under PowerShell. You cannot change the size of the notification. The background color will always be the same as the accent color you’ve picked in Windows 10. The notification will conform to the default settings for notifications on Windows 10. When you run the PowerShell script, a PowerShell window will open for a brief second and close automatically. This is normal and nothing to worry about.
You can use the Task Scheduler to trigger the notification.
Fatima Wahab
Fatima has been writing for AddictiveTips for six years. She began as a junior writer and has been working as the Editor in Chief since 2014.
Fatima gets an adrenaline rush from figuring out how technology works, and how to manipulate it. A well-designed app, something that solves a common everyday problem and looks
post views: 37,606
Introduction
Short and sweet. My Windows 10 Toast Notification Script have received a minor update. Now being at version 1.2. The changes mentioned in details below.
- Find the original page for the Windows 10 Toast Notification Script here: https://www.imab.dk/windows-10-toast-notification-script/
Changes
Personal Greeting
Coming this update, the Windows 10 Toast Notification Script is now able to greet the end user personally by given name, making the toast even more appealing and interesting (hopefully).
The script tries to retrieve the given name from the user in Active Directory. If no Active Directory (or given name) is found, the given name is replaced with colleague: Good evening colleague.
The greeting changes based on time of the day and switches between “Good morning, Good afternoon and Good evening. The personal greeting replaces the HeaderText option in the config.xml file.
Also, there are other means than Active Directory to find a given name (for non-domain joined computers). I’m probably going to add a few more options later on 🙂
ToastReboot Protocol
This is something I have been asked a lot: How can I make the toast initiate the actual reboot. For that I have created a custom protocol called ToastReboot which when used is running shutdown.exe /r /t 0 /d p:0:0 /c “Toast Notification Reboot”.
It’s not what I consider really pretty, but it works. The toast is limited to protocol based actions thus you have to be creative this way.
The ToastReboot protocol can be installed using the Windows 10 Toast Notification Script Reboot Protocol.msi which is included in the 1.2 download
The installation creates the protocol in the registry HKEY_CLASSES_ROOT\ToastReboot as well as installs a .cmd in ProgramData\ToastNotificationScript
Download
>> https://github.com/imabdk/Toast-Notification-Script <<