Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@dend
Last active April 2, 2024 14:50
Show Gist options
  • Star 66 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save dend/5ae8a70678e3a35d02ecd39c12f99110 to your computer and use it in GitHub Desktop.
Save dend/5ae8a70678e3a35d02ecd39c12f99110 to your computer and use it in GitHub Desktop.
Toast Notification in PowerShell
function Show-Notification {
[cmdletbinding()]
Param (
[string]
$ToastTitle,
[string]
[parameter(ValueFromPipeline)]
$ToastText
)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] > $null
$Template = [Windows.UI.Notifications.ToastNotificationManager]::GetTemplateContent([Windows.UI.Notifications.ToastTemplateType]::ToastText02)
$RawXml = [xml] $Template.GetXml()
($RawXml.toast.visual.binding.text|where {$_.id -eq "1"}).AppendChild($RawXml.CreateTextNode($ToastTitle)) > $null
($RawXml.toast.visual.binding.text|where {$_.id -eq "2"}).AppendChild($RawXml.CreateTextNode($ToastText)) > $null
$SerializedXml = New-Object Windows.Data.Xml.Dom.XmlDocument
$SerializedXml.LoadXml($RawXml.OuterXml)
$Toast = [Windows.UI.Notifications.ToastNotification]::new($SerializedXml)
$Toast.Tag = "PowerShell"
$Toast.Group = "PowerShell"
$Toast.ExpirationTime = [DateTimeOffset]::Now.AddMinutes(1)
$Notifier = [Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier("PowerShell")
$Notifier.Show($Toast);
}
@dend
Copy link
Author

dend commented Aug 30, 2021

Thanks for the heads-up @brainplot - I will have to check it out with PS7.

@clrogon
Copy link

clrogon commented Sep 15, 2021

In case of run on remote computers?

@lenicyl
Copy link

lenicyl commented Mar 7, 2022

Hi, this script works great for me !
However is there any way to add an icon for the toast ?

@Hrxn
Copy link

Hrxn commented Aug 26, 2022

@dend Did you find a workaround for PS7?

As far as I know, it's not possible with default PS7..

@TotallyInformation
Copy link

Agree. Doesn't work with PS7.

@cyntheticfox
Copy link

Does this script have a license? I'd like to try to play around with it and maybe use it a little, but don't know the terms and conditions

@dend
Copy link
Author

dend commented Feb 21, 2023

@houstdav000 - the license is "Use and remix this for whatever you want" 😊

@Hrxn @TotallyInformation haven't gotten a chance to write anything for PS7 yet. I'll have to test it on another box potentially later in the week and see where I can get with it.

@TotallyInformation
Copy link

Thanks Den, looking forward to using it 😁

@localden
Copy link

Did some digging - this is related to PowerShell/PowerShell#13042.

To mitigate the issue, download this package: https://www.nuget.org/packages/Microsoft.Windows.SDK.NET.Ref/#readme-body-tab

Then, you can add the assemblies from PS7:

Add-Type -AssemblyName WinRT.Runtime.dll
Add-Type -AssemblyName Microsoft.Windows.SDK.NET.dll

cc: @TotallyInformation @Hrxn

@TotallyInformation
Copy link

Thanks for digging into that. Sadly, the resolution appears to be more complex than is worth the effort! I'd need to install other apps to manage a nuget package then work out where I'd want the libraries and how to make sure they were kept current. Kind of the point of a toast message is simplicity! 🤣

When Microsoft (or some other kind person/group) have figured out how to use their own libraries with their own modern software, I'll revisit. Until then, I'll use alternatives.

@moonbug
Copy link

moonbug commented Apr 14, 2023

@dend how to prevent the previous toast notifications from being overwritten by new toast in action center? I want to keep all previous toasts and disappear only when I close it from action center

@dbalabka
Copy link

dbalabka commented Feb 7, 2024

Here is an instruction how to use this script from WSL terminal:
https://gist.github.com/dbalabka/20907893ba33f9ceebff3aaa8182f9c8

@reginaldosnunes
Copy link

@dend how to prevent the previous toast notifications from being overwritten by new toast in action center? I want to keep all previous toasts and disappear only when I close it from action center

Adding a unique identifier for each notification.
Change the $Toast.Tag = "PowerShell" to $Toast.Tag = [Guid]::NewGuid().ToString()

@Xavron
Copy link

Xavron commented Apr 1, 2024

@TotallyInformation

Maybe depends on your setup but simply sending it back to the other PowerShell version while in PS7 works (just need to wrap the code and start it using the older PS):

eg run from PS7 7.4.1 .".../testScript.ps1"

get-host

powershell
{
  function Show-Notification
  {
    ...
  }
  Show-Notification "test" "test"
  get-host
}
get-host

For me, pwsh is PS7.4.1 and powershell is 5.1.22621.2506

So, it winds up executing as:
7.4.1
Shows notification and popup
5.1.22621.2506
7.4.1

When running as a Task using PS7 (aka pwsh), it works fine as well.

As long as the older PS version is there it should work. Once that disappears, this will probably fail to work. But, simple answer for the time being. Suppose too, idk, but if its possible to always install an old enough PS version then that might keep it working for even longer once the built-in version gets too high.

E: The example probably needs a slight adjustment depending on how its used. Its just an example that works for anyone that reads this and tries to use copy paste without understanding (I'm not putting in the whole noti code so add that). Also, some use cases may cause a script to not finish running and that needs adjustment for it to work.

@TotallyInformation
Copy link

@Xavron
Thanks for that. Somewhat bizarre that you need such an overhead just to show a Toast! But hey, a workaround anyway. Thanks.

@Xavron
Copy link

Xavron commented Apr 2, 2024

Np. Between this and other silly things, I think my head is spinning :)P

(I know people in the linked issue thread seem to be feeling its not PS at fault... but ultimately its PS that has the problem so it is at fault. Kind of feels like Linux's xorg verses Wayland destruction that is still picking up the pieces after many years. Can't do anything about it but deal with workarounds. PS devs should code in another way but I get the impression they won't from its not their problem type of thinking that is prevalent among many devs. Its only legit to me if they wanted to cut out lots of uses.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment