Forum Discussion
Clear notifications
- Mar 12, 2024Yes, because the RunTime isn't included in PowerShell 7.4. I'm afraid we can't fix that since the support for WinRT has been removed after just a few versions of PowerShell 7.
Paige__Tanner I tested this, and that works:
# get the list of all registry keys
$notifications = Get-ChildItem HKCU:\SOFTWARE\Microsoft\Windows\CurrentVersion\Notifications\Settings | Select-Object Name
# iterate through the keys, extract the name that will be used in the clear function, and clear the notifications
for ($index = 0; $index -lt $notifications.Count; $index++) {
$name = $notifications[$index]
$split = $name -split "\\"
$last = $split[$split.Count - 1]
$last = $last.Substring(0, $last.Length - 1)
([Windows.UI.Notifications.ToastNotificationManager]::History).clear($last)
}
Please click Mark as Best Response & Like if my post helped you to solve your issue.
This will help others to find the correct solution easily. It also closes the item.
If one of the posts was helpful in other ways, please consider giving it a Like.
- Paige__TannerMar 11, 2024Copper Contributor
Harm_Veenstra Thanks for having tried. This doesn't work for me. Running the same script, I get many errors equal to this one:
InvalidOperation: C:\test_script.ps1:10 Line | 10 | ([Windows.UI.Notifications.ToastNotificationManager]::History).cl … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Unable to find type [Windows.UI.Notifications.ToastNotificationManager].
which are probably related to the error mentioned in my first post. I wonder why my system does not have [Windows.UI.Notifications.ToastNotificationManager].
- Mar 11, 2024Seems to be something with the built-in WinRT support that has been removed in PS7?
- Paige__TannerMar 11, 2024Copper Contributor
It could be something similar, yes. Some missing module or framework? But what exactly?
I can only add that I tried your script also on Windows PowerShell 5.1 and in my case it gives error as well:
Unable to find type [Windows.UI.Notifications.ToastNotificationManager]. At C:\test_script.ps1:10 char:6 + ([Windows.UI.Notifications.ToastNotificationManager]::History).cl ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Windows.UI.Noti...ficationManager:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound
- Mar 11, 2024I'm also running Windows 11 and PowerShell 7.4 and I ran the script there without issues... But I tried it again now, and I have the same error as you. It does work in PowerShell v5, however... Confused 🙂