Efficient communication is a cornerstone of IT management, especially in managed services and enterprise environments. Notifications and alerts play a pivotal role in keeping teams informed and ensuring system reliability. This blog post explores a versatile PowerShell script that empowers IT professionals to create and send toast notifications with customizable snooze and dismiss options. This functionality is particularly useful for Managed Service Providers (MSPs) and IT teams looking for lightweight, user-friendly ways to deliver critical messages to end-users.
Background
In Windows 10 and beyond, toast notifications are a modern way to alert users directly within the operating system. While third-party tools can offer similar functionalities, they often come with licensing costs, unnecessary overhead, or limited customization. This PowerShell script provides a streamlined, cost-effective alternative, allowing IT professionals to create snooze and dismiss notifications tailored to specific needs.
MSPs and IT administrators benefit significantly from this tool. Whether it’s reminding users about pending updates, notifying them of critical outages, or providing status updates during system maintenance, the ability to deliver actionable messages directly enhances operational efficiency and user engagement.
The Script:
#Requires -Version 5.1 <# .SYNOPSIS Sends a toast snooze/dismiss notification to the currently signed in user. Please run as the Current Logged-on User. The script defaults to using NinjaOne's logo if none is provided. .DESCRIPTION Sends a toast snooze/dismiss notification to the currently signed in user. Please run as 'Current Logged on User'. This defaults to using NinjaOne's logo in the Toast Message, but you can specify any png formatted image from a url. You can also specify the "ApplicationId" to any string. The default is "NinjaOne RMM". By using this script, you indicate your acceptance of the following legal terms as well as our Terms of Use at https://www.ninjaone.com/terms-of-use. Ownership Rights: NinjaOne owns and will continue to own all right, title, and interest in and to the script (including the copyright). NinjaOne is giving you a limited license to use the script in accordance with these legal terms. Use Limitation: You may only use the script for your legitimate personal or internal business purposes, and you may not share the script with another party. Republication Prohibition: Under no circumstances are you permitted to re-publish the script in any script library or website belonging to or under the control of any other software provider. Warranty Disclaimer: The script is provided “as is” and “as available”, without warranty of any kind. NinjaOne makes no promise or guarantee that the script will be free from defects or that it will meet your specific needs or expectations. Assumption of Risk: Your use of the script is at your own risk. You acknowledge that there are certain inherent risks in using the script, and you understand and assume each of those risks. Waiver and Release: You will not hold NinjaOne responsible for any adverse or unintended consequences resulting from your use of the script, and you waive any legal or equitable rights or remedies you may have against NinjaOne relating to your use of the script. EULA: If you are a NinjaOne customer, your use of the script is subject to the End User License Agreement applicable to you (EULA). .EXAMPLE -Title "My Title Here" -Message "My Message Here" Sends the title "My Title Here" and message "My Message Here" as a Toast message/notification to the currently signed in user. .EXAMPLE -Title "My Title Here" -Message "My Message Here" -ApplicationId "MyCompany" Sends the title "My Title Here" and message "My Message Here" as a Toast message/notification to the currently signed in user. ApplicationId: Creates a registry entry for your toasts called "MyCompany". PathToImageFile: Downloads a png image for the icon in the toast message/notification. SnoozeTimeOptionsInMinutes: Adds a dropdown to the toast message/notification with options for snoozing the message. .OUTPUTS None .NOTES If you want to change the defaults then with in the param block. ImagePath uses C:\Users\Public\ as that is accessible by all users. If you want to customize the application name to show your company name, then look for $ApplicationId and change the content between the double quotes. Minimum OS Architecture Supported: Windows 10 (IoT editions are not supported due to lack of shell) Release Notes: Renamed script, Updated Script Variables #> [CmdletBinding()] param ( [string]$Title, [string]$Message, [string]$ApplicationId, [string]$SnoozeTimeOptionsInMinutes, [string]$PathToImageFile ) begin { $Base64 = 'iVBORw0KGgoAAAANSUhEUgAAAIAAAACACAMAAAD04JH5AAAAJFBMVEUARF0Apc0AmL8ApM0Aos0Aps7///8Am8ia1ug9rtLd8/jw+/2tMDHwAAAABXRSTlMBrBTIcce4nvwAAAIeSURBVHic7dvrcoMgEAXgiOAivv/7Fm+JBpCLwk7bsz86rcNkPw+Y0Gl5vd4lGtbLKSG7vmF18mwQnWpe3YcghP2Z1svU8OtbIOihm8op25M2gWBov9UqYJj/vSRzAGsEkhMglxngWINbdbxLAAAAAAAAAAAAAKAI8Oz2KRtApPWThEyAbT8NZwDZGpeav6sLIKXNMBwAtuGotTGTvTpMRms9qkxEBsDe/dz+A7B3rufeS/utrCKPkAywzfYmK8BeOHY+lBkzBImALfwDgA4XnNLphCTA4e43AKmL9vNMJD8pCQAna20nP5D+SfkQgJyp1qS9PYsEKQDnpVP627WYJCgBmGj+GRmUAFIraSXWBAwDcwJJk1AXMIzcgHgElQHxCGoDohHcBsybgIvPpei70S2A0csuaNkTBRBTbA7uAOb271E0+gWxOSgHfG87yD+wGsCz7fGONNf9iwGTb89DnlkwkUVQCPD2t1sXz9A6gMDT5YsgsggKARljI/vTMkDo7cU3B1USCL+oOwdVAMGF5RlcAxB+tBoBwq/JDlDcAPYEAGgDuPiNBwkgASSABJAAEkACSAAJIAEkgASQABL4JwlcA9w/9N4GTOZcl1OQMTgRoEannhv9O/+PCAAAAAAAAAAAAACAPwhgP+7HeOCR1jOfjBHI9dBrz9W/34/d9jyHLvvPweP2GdCx/3zyvLlAfZ8+l13LktJzAJ+nfgAP50EVLvPsRgAAAABJRU5ErkJggg==' [string]$ImagePath = "$($env:SystemDrive)\Users\Public\PowerShellToastSnoozeImage.png" # Set the default ApplicationId if it's not provided. Use the Company Name if available, otherwise use the default. $ApplicationId = if ($env:NINJA_COMPANY_NAME) { $env:NINJA_COMPANY_NAME } else { "NinjaOne RMM" } Write-Host "[Info] Using ApplicationId: $($ApplicationId -replace '\s+','.')" if ($env:title -and $env:title -notlike "null") { $Title = $env:title } if ($env:message -and $env:message -notlike "null") { $Message = $env:message } if ($env:applicationId -and $env:applicationId -notlike "null") { $ApplicationId = $env:applicationId } if ($env:pathToImageFile -and $env:pathToImageFile -notlike "null") { $PathToImageFile = $env:pathToImageFile } if ($env:snoozeTimeOptionsInMinutes -and $env:snoozeTimeOptionsInMinutes -notlike "null") { $SnoozeTimeOptionsInMinutes = $env:snoozeTimeOptionsInMinutes } if ([String]::IsNullOrWhiteSpace($Title)) { Write-Host "[Error] A Title is required." exit 1 } if ([String]::IsNullOrWhiteSpace($Message)) { Write-Host "[Error] A Message is required." exit 1 } if ($Title.Length -gt 82) { Write-Host "[Warn] The Title is longer than 82 characters. The title will be truncated by the Windows API to 82 characters." } if ($Message.Length -gt 160) { Write-Host "[Warn] The Message is longer than 160 characters. The message might get truncated by the Windows API." } function Test-IsSystem { $id = [System.Security.Principal.WindowsIdentity]::GetCurrent() return $id.Name -like "NT AUTHORITY*" -or $id.IsSystem } if (Test-IsSystem) { Write-Host "[Error] Please run this script as 'Current Logged on User'." Exit 1 } function Set-RegKey { param ( $Path, $Name, $Value, [ValidateSet("DWord", "QWord", "String", "ExpandedString", "Binary", "MultiString", "Unknown")] $PropertyType = "DWord" ) if (-not $(Test-Path -Path $Path)) { # Check if path does not exist and create the path New-Item -Path $Path -Force | Out-Null } if ((Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore)) { # Update property and print out what it was changed from and changed to $CurrentValue = (Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore).$Name try { Set-ItemProperty -Path $Path -Name $Name -Value $Value -Force -Confirm:$false -ErrorAction Stop | Out-Null } catch { Write-Host "[Error] Unable to Set registry key for $Name please see below error!" Write-Host $_.Exception.Message exit 1 } Write-Host "[Info] $Path\$Name changed from:" Write-Host " $CurrentValue to:" Write-Host " $($(Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore).$Name)" } else { # Create property with value try { New-ItemProperty -Path $Path -Name $Name -Value $Value -PropertyType $PropertyType -Force -Confirm:$false -ErrorAction Stop | Out-Null } catch { Write-Host "[Error] Unable to Set registry key for $Name please see below error!" Write-Host $_.Exception.Message exit 1 } Write-Host "[Info] Set $Path\$Name to:" Write-Host " $($(Get-ItemProperty -Path $Path -Name $Name -ErrorAction Ignore).$Name)" } } function ConvertFrom-Base64 { param( $Base64, $Path ) $bytes = [Convert]::FromBase64String($Base64) $ErrorActionPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue [IO.File]::WriteAllBytes($Path, $bytes) $ErrorActionPreference = [System.Management.Automation.ActionPreference]::Continue } # Utility function for downloading files. function Invoke-Download { param( [Parameter()] [String]$URL, [Parameter()] [String]$Path, [Parameter()] [int]$Attempts = 3, [Parameter()] [Switch]$SkipSleep ) Write-Host "[Info] Used $PathToImageFile for the image and saving to $ImagePath" $SupportedTLSversions = [enum]::GetValues('Net.SecurityProtocolType') if ( ($SupportedTLSversions -contains 'Tls13') -and ($SupportedTLSversions -contains 'Tls12') ) { [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol::Tls13 -bor [System.Net.SecurityProtocolType]::Tls12 } elseif ( $SupportedTLSversions -contains 'Tls12' ) { [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 } else { # Not everything requires TLS 1.2, but we'll try anyways. Write-Host "[Warn] TLS 1.2 and or TLS 1.3 isn't supported on this system. This download may fail!" if ($PSVersionTable.PSVersion.Major -lt 3) { Write-Host "[Warn] PowerShell 2 / .NET 2.0 doesn't support TLS 1.2." } } $i = 1 While ($i -le $Attempts) { # Some cloud services have rate-limiting if (-not ($SkipSleep)) { $SleepTime = Get-Random -Minimum 1 -Maximum 7 Write-Host "[Info] Waiting for $SleepTime seconds." Start-Sleep -Seconds $SleepTime } if ($i -ne 1) { Write-Host "" } Write-Host "[Info] Download Attempt $i" $PreviousProgressPreference = $ProgressPreference $ProgressPreference = 'SilentlyContinue' try { # Invoke-WebRequest is preferred because it supports links that redirect, e.g., https://t.ly # Standard options $WebRequestArgs = @{ Uri = $URL MaximumRedirection = 10 UseBasicParsing = $true OutFile = $Path } # Download The File Invoke-WebRequest @WebRequestArgs $ProgressPreference = $PreviousProgressPreference $File = Test-Path -Path $Path -ErrorAction SilentlyContinue } catch { Write-Host "[Error] An error has occurred while downloading!" Write-Warning $_.Exception.Message if (Test-Path -Path $Path -ErrorAction SilentlyContinue) { Remove-Item $Path -Force -Confirm:$false -ErrorAction SilentlyContinue } $File = $False } if ($File) { $i = $Attempts } else { Write-Host "[Error] File failed to download." Write-Host "" } $i++ } if (-not (Test-Path $Path)) { Write-Host "[Error] Failed to download file!" exit 1 } else { return $Path } } function Show-Notification { [CmdletBinding()] Param ( [string] $ApplicationId, [string] $ToastTitle, [string] [Parameter(ValueFromPipeline)] $ToastText, [string] $SnoozeOptions ) # Import all the needed libraries [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null [Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null [Windows.System.User, Windows.System, ContentType = WindowsRuntime] > $null [Windows.System.UserType, Windows.System, ContentType = WindowsRuntime] > $null [Windows.System.UserAuthenticationStatus, Windows.System, ContentType = WindowsRuntime] > $null [Windows.Storage.ApplicationData, Windows.Storage, ContentType = WindowsRuntime] > $null # Make sure that we can use the toast manager, also checks if the service is running and responding try { $ToastNotifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("$ApplicationId") } catch { Write-Host $_.Exception.Message Write-Host "[Error] Failed to create notification." } # Create the xml for the snooze options $IsFirst = $true if ($($SnoozeOptions -split ',').Count -gt 5) { Write-Host "[Error] Too many snooze options provided. Maximum is 5." exit 1 } $SnoozeXml = $SnoozeOptions -split ',' | ForEach-Object { # Trim whitespace $Option = "$_".Trim() if ($IsFirst) { # Add the input and default selection "<input id='snoozeTime' type='selection' defaultInput='$Option'>" $IsFirst = $false } # Check if the option is a number if ([int]::TryParse($Option, [ref]$null)) { # Convert the number to an integer $Option = [int]$Option } else { # If not a number, exit with an error Write-Host "[Error] Invalid snooze time option '$Option' provided." exit 1 } # Add the selection if ($Option -ge 60) { # Get the number of hours and minutes # Round the number of hours to the nearest hour $Hours = [math]::Round($Option / 60, 0) # Get the number of minutes $MinutesMod = $Option % 60 # Format the number of minutes $Minutes = if ($MinutesMod -eq 0) { # If the number of minutes is 0, don't display anything "" } elseif ($MinutesMod -gt 1) { # If the number of minutes is greater than 1, format it as ' 2 Minutes' " $MinutesMod Minutes" } elseif ($MinutesMod -eq 1) { # If the number of minutes is 1, format it as ' 1 Minute' " $MinutesMod Minute" } # Format the number of hours $Unit = if ($Hours -gt 1) { 'Hours' }else { 'Hour' } "<selection id='$($Option)' content='$($Hours) $($Unit)$($Minutes)'/>" } elseif ($Option -lt 60) { # Format the number of minutes when it's less than 60 minutes $Minutes = $Option $Unit = if ($Minutes -gt 1) { 'Minutes' }else { 'Minute' } "<selection id='$($Option)' content='$($Minutes) $($Unit)'/>" } } # Create a new toast notification $RawXml = [xml] @" <toast> <visual> <binding template='ToastGeneric'> <image placement='appLogoOverride' src='$ImagePath'/> <text id='1'>$ToastTitle</text> <text id='2'>$ToastText</text> </binding> </visual> <actions> $SnoozeXml </input> <action activationType="system" arguments="snooze" hint-inputId="snoozeTime" content="" /> <action activationType="system" arguments="dismiss" content=""/> </actions> </toast> "@ # Serialized Xml for later consumption $SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument $SerializedXml.LoadXml($RawXml.OuterXml) # Setup how are toast will act, such as expiration time $Toast = $null $Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml) $Toast.Tag = "PowerShell" $Toast.Group = "PowerShell" # Show our message to the user $ToastNotifier.Show($Toast) } } process { Write-Host "ApplicationID: $ApplicationId" if (-not $(Split-Path -Path $ImagePath -Parent | Test-Path -ErrorAction SilentlyContinue)) { try { New-Item "$(Split-Path -Path $ImagePath -Parent)" -ItemType Directory -ErrorAction Stop Write-Host "[Info] Created folder: $(Split-Path -Path $ImagePath -Parent)" } catch { Write-Host "[Error] Failed to create folder: $(Split-Path -Path $ImagePath -Parent)" exit 1 } } $DownloadArguments = @{ URL = $PathToImageFile Path = $ImagePath } Set-RegKey -Path "HKCU:\SOFTWARE\Classes\AppUserModelId\$($ApplicationId -replace '\s+','.')" -Name "DisplayName" -Value $ApplicationId -PropertyType String if ($PathToImageFile -like "http*") { Invoke-Download @DownloadArguments } elseif ($PathToImageFile -match "^[a-zA-Z]:\\" -and $(Test-Path -Path $PathToImageFile -ErrorAction SilentlyContinue)) { Write-Host "[Info] Image is a local file, copying to $ImagePath" Copy-Item -Path $PathToImageFile -Destination $ImagePath } elseif ($PathToImageFile -match "^[a-zA-Z]:\\" -and -not $(Test-Path -Path $PathToImageFile -ErrorAction SilentlyContinue)) { Write-Host "[Error] Image does not exist at $PathToImageFile" exit 1 } else { Write-Host "[Info] No image given, converting base64 on line 43 and saving to $ImagePath." Write-Host "[Info] Image will be used for the toast message." ConvertFrom-Base64 -Base64 $Base64 -Path $ImagePath } Set-RegKey -Path "HKCU:\SOFTWARE\Classes\AppUserModelId\$($ApplicationId -replace '\s+','.')" -Name "IconUri" -Value "$ImagePath" -PropertyType String Write-Host "[Info] System is ready to send Toast Messages to the currently logged on user." try { Write-Host "[Info] Attempting to send message to user..." $NotificationParams = @{ ToastTitle = $Title ToastText = $Message ApplicationId = "$($ApplicationId -replace '\s+','.')" SnoozeOptions = $SnoozeTimeOptionsInMinutes } Show-Notification @NotificationParams -ErrorAction Stop Write-Host "[Info] Message sent to user." } catch { Write-Host "[Error] Failed to send message to user." Write-Host $_.Exception.Message exit 1 } exit 0 } end { }
Save time with over 300+ scripts from the NinjaOne Dojo.
Detailed Breakdown
The provided PowerShell script includes several key components designed for flexibility and functionality. Here’s a step-by-step breakdown of how it works:
1. Initialization and Parameters
- The script accepts parameters for notification title, message, application ID, snooze options, and an optional image file path.
- It validates input and sets defaults where parameters are not provided. For example, the default ApplicationId is “NinjaOne RMM,” ensuring identification consistency in the Windows notification center.
2. User Validation
- The script checks if it’s being executed as the “Current Logged-on User.” This ensures that notifications reach the intended recipient rather than running in an elevated system context where toast notifications aren’t supported.
3. Registry Configuration
- Using the Set-RegKey function, the script creates or updates registry entries to register the toast application ID and associate an icon with the notification.
4. Image Handling
Notifications can include a custom image. The script supports three scenarios:
- A URL to download an image.
- A local file path to copy.
- A default base64-encoded NinjaOne logo, converted and saved as an image file if no custom image is provided.
5. Notification XML Generation
- The script generates XML code defining the toast message structure, including title, text, image, and snooze options. The Show-Notification function uses the Windows Runtime API to display the toast notification.
6. Snooze and Dismiss Functionality
- Users can snooze notifications for pre-configured durations or dismiss them entirely. These options are defined in the XML and enable user interaction directly from the notification.
Potential Use Cases
Case Study: System Update Notifications
Imagine an IT administrator in a corporate environment planning to deploy a critical system update overnight. To ensure users are informed, they use this script to send a toast notification titled “System Maintenance Notification” with the message “Your system will reboot at 2:00 AM for updates. Click to snooze or dismiss.”
- Customization: The notification includes the organization’s logo.
- Snooze Options: Users can delay the reminder by 15, 30, or 60 minutes.
- Outcome: The team ensures users are well-informed without manual follow-ups.
Comparisons
Alternative Methods:
- Third-Party Notification Tools: While robust, they may lack integration capabilities and incur costs.
- Built-In Windows Group Policy Notifications: Limited customization and no snooze functionality.
Compared to these methods, this PowerShell script offers:
- Greater customization with image support.
- Enhanced user experience with snooze/dismiss options.
- Cost-efficiency, leveraging native Windows APIs.
FAQs
Q: Can the script run as an administrator?
No, it must be run as the currently logged-on user to display notifications properly.
Q: What happens if no image is provided?
A default NinjaOne logo is used, ensuring every notification is branded.
Q: How are snooze options configured?
Snooze times are defined as a comma-separated list (e.g., “15,30,60” for 15, 30, and 60 minutes).
Q: Is this compatible with Windows IoT editions?
No, due to the lack of a shell in IoT editions.
Implications
Using this script, IT teams can standardize notification delivery, ensuring messages are actionable and relevant. In larger environments, this improves user awareness and reduces downtime caused by missed alerts. Additionally, branding options help MSPs reinforce their identity while maintaining professionalism.
Recommendations
- Test in a Non-Production Environment: Validate the script’s behavior in a controlled setting before deploying widely.
- Customize Application IDs: Use organization-specific identifiers for better management and visibility.
- Monitor User Feedback: Gather input to refine snooze options and notification content for maximum user engagement.
Final Thoughts
This script exemplifies how NinjaOne and similar IT management solutions can empower IT professionals. By combining simplicity with advanced customization, the script enables efficient communication and enhances the user experience. Whether you’re an MSP or an in-house IT team, leveraging tools like this aligns with the best practices for IT operations management.
For more tailored solutions, NinjaOne provides a suite of tools designed to streamline IT workflows and optimize system reliability.