Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

powershell unable to find type [Windows.UI.Notification]

Tags:

powershell

I am trying to generate a windows 10 notification with powershell. The code is as follow:

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

When I run the function, it throws the exception like this: "Unable to find type [Windows.UI.Notificaiotn...." I think this is a UWP class, but why I can not access this class in powershell ?

like image 674
Takiya Avatar asked Sep 01 '26 06:09

Takiya


1 Answers

The build in class Windows.UI.Notification is only available in Powershell 5 (build in Windows 10) not in Powershell 7, like @swbbl pointed out.

To find out the current version of your shell enter

$Host.Version

If under Major you find something else as "5" the Windows.UI.Notifications class cannot be used.

like image 200
theking2 Avatar answered Sep 03 '26 12:09

theking2