Entra / Microsoft 365 · Exchange Online
Send welcome email mg
How to send a welcome message to new mailboxes using the Send-MgUserMessage cmdlet from the Microsoft Graph SDK for PowerShell.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -NoWelcome -Scopes "Mail.Send, Mail.ReadWrite"
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
param([int] $LookbackDays = 7)$ModulesLoaded = Get-Module | Select-Object -ExpandProperty NameIf (!($ModulesLoaded -match "ExchangeOnlineManagement")) {Write-Host "Please connect to the Exchange Online Management module and then restart the script";break}# Now connect to the Microsoft Graph SDK for PowerShellConnect-MgGraph -NoWelcome -Scopes "Mail.Send, Mail.ReadWrite"$TenantName = (Get-MgOrganization).DisplayNameClear-Host# Message is from the logged in account$MsgFrom = (Get-MgContext).Account# Define some variables used to construct the HTML content in the message body#HTML header with styles$htmlhead="<html><style>BODY{font-family: Arial; font-size: 10pt;}H1{font-size: 22px;}H2{font-size: 18px; padding-top: 10px;}H3{font-size: 16px; padding-top: 8px;}</style>"#Content for the message - obviously this is very customizable to reflect what you want to say to new users$HtmlBody = $HtmlHead + "<body><h1>Welcome to $($TenantName)</h1><p><strong>Generated:</strong> $(Get-Date -Format g)</p><h2><u>We're Pleased to Have You Here</u></h2><p><b>Welcome to your new Office 365 account</b></p><p>You can open your account to access your email and documents by clicking <a href=http://www.portal.office.com>here</a> </p><p>Have a great time and be sure to call the help desk if you need assistance. And be sure to read all the great articles about Office 365 published on Practical365.com.</p>"$MsgSubject = "A warm welcome to $($TenantName)"# Date to Check for new accounts - we use the last 7 days here, but that's easily changable.[string]$CheckDate = (Get-Date).AddDays(-$LookbackDays)# Find all mailboxes created in the target period[array]$Users = (Get-ExoMailbox -Filter "WhenMailboxCreated -gt '$CheckDate'" -RecipientTypeDetails UserMailbox `-ResultSize Unlimited -Properties WhenMailboxCreated | Select-Object WhenMailboxCreated, DisplayName, UserPrincipalName, PrimarySmtpAddress)If (!($Users)) {Write-Host ("No mailboxes found that were created before {0}. Exiting!" -f $CheckDate)Break}# Define attachment - I use a document on a web site here, but you could use a local file$WebAttachmentFile = "https://office365itpros.com/wp-content/uploads/2022/02/WelcomeToOffice365ITPros.docx"$AttachmentFile = "c:\temp\WelcomeNewEmployeeToOffice365itpros.docx"Invoke-WebRequest -uri $WebAttachmentFile -OutFile $AttachmentFile$EncodedAttachmentFile = [Convert]::ToBase64String([IO.File]::ReadAllBytes($AttachmentFile))$MsgAttachment = @(@{"@odata.type" = "#microsoft.graph.fileAttachment"Name = ($AttachmentFile -split '\\')[-1]ContentBytes = $EncodedAttachmentFile})# Create and send welcome email message to each of the new mailboxesForEach ($User in $Users) {# Add the recipient using the mailbox's primary SMTP address$EmailAddress = @{address = $User.PrimarySmtpAddress}$EmailRecipient = @{EmailAddress = $EmailAddress}Write-Host "Sending welcome email to" $User.DisplayName# Customize the message$HtmlHeaderUser = "<h2>New User " + $User.DisplayName + "</h2>"$HtmlBody = $HtmlHeaderUser + $HtmlBody + "<p><p>Created on: " + $User.WhenMailboxCreated + "</p>"# Construct the message body$MessageBody = @{content = "$($HtmlBody)"ContentType = 'html'}# Create a draft message in the signed-in user's mailbox$NewMessage = New-MgUserMessage -UserId $MsgFrom -Body $MessageBody -ToRecipients $EmailRecipient -Subject $MsgSubject -Attachments $MsgAttachment# Send the messageSend-MgUserMessage -UserId $MsgFrom -MessageId $NewMessage.Id} # End ForEach UserWrite-Host "All done. Messages sent!"# Clean upRemove-item $attachmentfile
Parameters
ParameterDefaultNotes
-LookbackDays7How many days back to search for newly created mailboxes or recent activity.Attribution
Author
Office365itpros