Skip to content

New-MgmtGraphUserMessage

MgmtGraph: Provisions a new message in a user's mailbox

#Requires -Version 5.1
#Requires -Modules Microsoft.Graph.Mail

[CmdletBinding()]
Param (
    [Parameter(Mandatory = $true, Position = 0)]
    [string]$Identity,

    [Parameter(Mandatory = $true)]
    [string]$Subject,

    [string]$Body,

    [string[]]$Categories
)

Process {
    try {
        $params = @{
            'UserId'      = $Identity
            'Subject'     = $Subject
            'Confirm'     = $false
            'ErrorAction' = 'Stop'
        }

        if ($Body) {
            $params.Add('Body', @{
                ContentType = 'HTML'
                Content     = $Body
            })
        }
        if ($Categories) { $params.Add('Categories', $Categories) }

        $message = New-MgUserMessage @params
        
        $result = [PSCustomObject]@{
            UserId    = $Identity
            Subject   = $Subject
            Id        = $message.Id
            Status    = "MessageCreated"
            Timestamp = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the UserPrincipalName or ID of the user.

Specifies the subject line for the new message.

Optional. Specifies the body content of the message.

Optional. Specifies an array of categories to associate with the message.

An interactive directory of PowerShell scripts.