Created
October 29, 2024 21:25
-
-
Save SMSAgentSoftware/95426dd0e7d6e3a0e73c159991ff780d to your computer and use it in GitHub Desktop.
Example code to pop a toast notification in either PowerShell desktop or core editions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
If ($PSVersionTable.PSVersion.Major -lt 6) | |
{ | |
$null = [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | |
$null = [Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | |
} | |
else | |
{ | |
# Check for the NuGet package provider and install it if necessary | |
if ($null -eq (Get-PackageProvider -Name NuGet)) | |
{ | |
try | |
{ | |
$null = Install-PackageProvider -Name NuGet -Force -Scope CurrentUser -ErrorAction Stop | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
# Check for the Microsoft.Windows.SDK.NET.Ref NuGet package and install it if necessary | |
If ($null -eq (Get-Package -ProviderName NuGet -Name Microsoft.Windows.SDK.NET.Ref -AllVersions -ErrorAction SilentlyContinue)) | |
{ | |
try | |
{ | |
$null = Install-Package -Name Microsoft.Windows.SDK.NET.Ref -ProviderName NuGet -Force -Scope CurrentUser -ErrorAction Stop | |
} | |
catch | |
{ | |
throw $_.Exception.Message | |
} | |
} | |
# Get the latest version of the WinRT.Runtime.dll and Microsoft.Windows.SDK.NET.dll files | |
$WinRTRuntime = Get-ChildItem -Path "$env:LOCALAPPDATA\PackageManagement\NuGet\Packages\Microsoft.Windows.SDK.NET.Ref.*" -Filter "WinRT.Runtime.dll" -Recurse -ErrorAction SilentlyContinue | | |
Sort-Object -Property VersionInfo.FileVersion -Desc | Select -ExpandProperty FullName | Select -First 1 | |
$WinSDKNet = Get-ChildItem -Path "$env:LOCALAPPDATA\PackageManagement\NuGet\Packages\Microsoft.Windows.SDK.NET.Ref.*" -Filter "Microsoft.Windows.SDK.NET.dll" -Recurse -ErrorAction SilentlyContinue | | |
Sort-Object -Property VersionInfo.FileVersion -Desc | Select -ExpandProperty FullName | Select -First 1 | |
# Load the WinRT.Runtime.dll and Microsoft.Windows.SDK.NET.dll files | |
Add-Type -Path $WinRTRuntime -ErrorAction Stop | |
Add-Type -Path $WinSDKNet -ErrorAction Stop | |
} | |
# Notification parameters | |
$Title = "Hello matey!" | |
$AudioSource = "ms-winsoundevent:Notification.Default" | |
$SubtitleText = "I say, ol' chap, it's time for a brew. Polly put the kettle on, and all that. While you prepare a cuppa, we'll install some updates." | |
# Define the toast notification in XML format | |
[xml]$ToastTemplate = @" | |
<toast duration="long"> | |
<visual> | |
<binding template="ToastGeneric"> | |
<text>Windows Updates - you know you love 'em</text> | |
<group> | |
<subgroup> | |
<text hint-style="title" hint-wrap="true" >$Title</text> | |
</subgroup> | |
</group> | |
<group> | |
<subgroup> | |
<text hint-style="subtitle" hint-wrap="true" >$SubtitleText</text> | |
</subgroup> | |
</group> | |
</binding> | |
</visual> | |
<audio src="$AudioSource"/> | |
</toast> | |
"@ | |
# Load the notification into the required format | |
$ToastXml = New-Object -TypeName Windows.Data.Xml.Dom.XmlDocument | |
$ToastXml.LoadXml($ToastTemplate.OuterXml) | |
# Display | |
$App = "Windows.SystemToast.WindowsUpdate.MoNotification2" | |
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($App).Show($ToastXml) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment