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

  1. 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.
  2. 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


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *

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