For over a decade, messaging administrators automated Exchange mailbox tasks in PowerShell by loading Microsoft.Exchange.WebServices.dll (EWS Managed API 2.0), creating an ExchangeService object, and passing WebCredentials against https://outlook.office365.com/EWS/Exchange.asmx.
With Exchange Web Services (EWS) retiring in Exchange Online and Basic Authentication permanently disabled, scripts built on Microsoft.Exchange.WebServices.Data.ExchangeService must be migrated to Microsoft Graph (Microsoft.Graph.Mail, Microsoft.Graph.Calendar, Microsoft.Graph.Users.Actions) using OAuth 2.0 Certificate-Based Authentication (CBA).
This runbook maps every major EWS Managed API class and method from our original 2014 guide to its exact Microsoft Graph PowerShell SDK and REST equivalent, verified against the LAB 365 tenant.
1. EWS Managed API to Microsoft Graph Translation Matrix
| EWS Managed API Operation | Legacy EWS Class / Method | Modern Microsoft Graph Cmdlet | Graph REST Endpoint & Permission |
|---|---|---|---|
| Authenticate Client | New-Object ...ExchangeService + WebCredentials | Connect-MgGraph -ClientId -TenantId -CertificateThumbprint | OAuth 2.0 JWT Client Assertion |
| Create Draft Message | $msg = New-Object ...EmailMessage($service); $msg.Save() | New-MgUserMessage -UserId $upn -BodyParameter $params | POST /users/{id}/messages (Mail.ReadWrite) |
| Send & Save Copy | $msg.SendAndSaveCopy() | Send-MgUserMail -UserId $upn -BodyParameter $params | POST /users/{id}/sendMail (Mail.Send) |
| Search Folder Items | $service.FindItems([WellKnownFolderName]::Inbox, $filter, $view) | Get-MgUserMailFolderMessage -UserId $upn -MailFolderId Inbox -Filter ... | GET /users/{id}/mailFolders/Inbox/messages (Mail.Read) |
| Manage Inbox Rules | $service.GetInboxRules($upn) | Get-MgUserMailFolderMessageRule -UserId $upn -MailFolderId Inbox | GET /users/{id}/mailFolders/inbox/messageRules (MailboxSettings.Read) |
| Out of Office (OOF) | $service.GetUserOofSettings($upn) | Get-MgUserMailboxSetting -UserId $upn | GET /users/{id}/mailboxSettings (MailboxSettings.Read) |
| Calendar Free/Busy | $service.GetUserAvailability(...) | Get-MgUserCalendarSchedule -UserId $upn -BodyParameter ... | POST /users/{id}/calendar/getSchedule (Calendars.Read) |
2. Step-by-Step Migration: Draft & Send Email Script
In our original 2014 script (SendEmail_EWS.ps1), we loaded Microsoft.Exchange.WebServices.dll, created an EmailMessage object, saved a draft, and called $message.SendAndSaveCopy().
Here is the production 2026 replacement using Microsoft Graph PowerShell SDK with unattended Certificate-Based Authentication:
Option A: Create a Draft First, Verify, Then Send (Matches $message.Save())
#Requires -Modules Microsoft.Graph.Authentication, Microsoft.Graph.Mail, Microsoft.Graph.Users.Actions
[CmdletBinding()]
param(
[string]$TenantId = "ab85c7a1-366d-44ba-acc6-924fa0950045",
[string]$ClientId = "c841ae33-4093-4385-8a90-6a530761f9b9",
[string]$Thumbprint = "BEAC429DE404B46FF030811C0C9DE00DE701C027",
[string]$SenderUpn = "[email protected]",
[string]$Recipient = "[email protected]"
)
# 1. Connect using Certificate-Based App-Only Auth (Replaces ExchangeService + WebCredentials)
Connect-MgGraph -TenantId $TenantId -ClientId $ClientId -CertificateThumbprint $Thumbprint -NoWelcome
# 2. Create Draft Message in Drafts Folder (Replaces $message.Save())
# Requires Application Permission: Mail.ReadWrite
$draftParams = @{
Subject = "This Message Was Created via Microsoft Graph SDK (EWS Migration)"
Body = @{
ContentType = "HTML"
Content = "<p>This is a verified test message from <strong>LAB 365</strong> replacing EWS Managed API 2.0.</p>"
}
ToRecipients = @(
@{
EmailAddress = @{ Address = $Recipient }
}
)
}
$draft = New-MgUserMessage -UserId $SenderUpn -BodyParameter $draftParams
Write-Host "[+] Draft saved in Drafts folder. MessageId: $($draft.Id)" -ForegroundColor Green
# 3. Send the Draft and Save Copy to Sent Items (Replaces $message.SendAndSaveCopy())
# Requires Application Permission: Mail.Send
Send-MgUserMessage -UserId $SenderUpn -MessageId $draft.Id
Write-Host "[+] Draft transmitted and saved to Sent Items." -ForegroundColor Green
Option B: Direct One-Call Send (Replaces $message.SendAndSaveCopy() Without Pre-Drafting)
If you don't need to inspect the draft before sending, Send-MgUserMail requires only Mail.Send (no Mail.ReadWrite needed — following strict least-privilege principles):
$mailPayload = @{
Message = @{
Subject = "Automated Notification via Microsoft Graph"
Body = @{
ContentType = "HTML"
Content = "<p>Sent directly via <code>POST /users/$SenderUpn/sendMail</code>.</p>"
}
ToRecipients = @(
@{ EmailAddress = @{ Address = $Recipient } }
)
}
SaveToSentItems = $true
}
Send-MgUserMail -UserId $SenderUpn -BodyParameter $mailPayload
3. Live Lab Verification: Least-Privilege Permission Enforcement
When we tested GET https://graph.microsoft.com/v1.0/users/[email protected]/messages against the LAB 365 Dev Tenant using an app registration that holds User.ReadWrite.All and Directory.ReadWrite.All (but not Mail.Read), Microsoft Graph immediately returned HTTP 403 Forbidden.
Unlike legacy EWS ApplicationImpersonation (which historically granted full read/write access to every mailbox in the organization), Microsoft Graph enforces resource-specific permissions, and you can further restrict an application to a single distribution group or security group using Exchange Online RBAC for Applications (New-ManagementScope + New-ManagementRoleAssignment -App).
Restricting Your Graph Mail App to Specific Mailboxes
Connect-ExchangeOnline
# 1. Create a scope limiting which mailboxes the Graph app can touch
New-ManagementScope -Name "Allowed-Automation-Mailboxes" `
-RecipientRestrictionFilter "Department -eq 'IT Automation'"
# 2. Register the Entra Service Principal in Exchange Online RBAC
New-ServicePrincipal -AppId $ClientId -ObjectId "<Enterprise-App-ServicePrincipal-ObjectId>" `
-DisplayName "LAB365 Graph Mail Automation"
# 3. Scope Mail.Send / Mail.ReadWrite to ONLY those mailboxes
New-ManagementRoleAssignment -App $ClientId `
-Role "Application Mail.Send" `
-CustomResourceScope "Allowed-Automation-Mailboxes"
4. What If YouStill Run On-Premises Exchange Server SE?
EWS deprecation applies to Exchange Online (Microsoft 365). If you run on-premises Exchange Server Subscription Edition (SE) or Exchange 2019, EWS remains supported for on-premises mailboxes, though you should replace Basic Credentials with OAuth / Windows Integrated Auth and enforce Extended Protection (EPA). For full details on auditing remaining EWS traffic in Exchange Online, see our EWS Retirement & AllowList Runbook.
💬 Reader Comments & Feedback
Did this runbook help, or did Microsoft change a cmdlet or UI menu? Share your feedback below.
Leave a Comment or Report an Update