SOLVED

Clear notifications

Brass Contributor

Hello!

Is it possible to clear the current Windows notifications from PowerShell?

By notifications I mean the ones shown by the bell in the rightmost point of the taskbar. I don't want to avoid receiving notifications, but just to clear the ones that are present in this moment.

I am using Windows 11 and PowerShell 7.4.

 

The only solution I found is this one, but if I try

[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null

 I got an error:

InvalidOperation: Unable to find type [Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime].

both from a user shell and from an Administrator shell.

9 Replies

@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)
}

 

(Source: https://www.reddit.com/r/PowerShell/comments/qzxh5n/how_to_programmatically_clear_win10_notification...)

 

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.

@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].

 

 

I'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 :)
Seems to be something with the built-in WinRT support that has been removed in PS7?

@Harm_Veenstra

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

 

 

 

Do you have the Microsoft Windows Desktop Runtime installed?
I didn't. Now I just installed it and the script works with Windows PowerShell 5.1, but not in PowerShell 7.4. It's better than before, but there is still something missing.
best response confirmed by Paige__Tanner (Brass Contributor)
Solution
Yes, 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.
Ok! At least, it works on Windows PowerShell 5.1.
Thanks for all your suggestions and your help.
1 best response

Accepted Solutions
best response confirmed by Paige__Tanner (Brass Contributor)
Solution
Yes, 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.

View solution in original post