How to Migrate EWS ImpersonatedUserID to Microsoft Graph Application Permissions & Scoped Access Policies

In legacy Exchange Web Services (EWS) development, developers and administrators routinely relied on the ImpersonatedUserId header to act on behalf of other mailboxes without possessing individual user passwords. Backed by the Exchange RBAC role ApplicationImpersonation, a single service account or daemon application could access every mailbox in an organization.

However, Microsoft has officially retired ApplicationImpersonation in Exchange Online and is deprecating legacy EWS endpoints in favor of the Microsoft Graph API.

In this modernized guide, we examine why legacy EWS impersonation was retired, how the ImpersonatedUserId pattern translates into modern Microsoft Graph Application Permissions, and how to enforce least-privilege boundaries using Exchange Online Application Access Policies (New-ApplicationAccessPolicy).


The Security Paradigm Shift: God-Mode vs. Scoped App Roles

Under the legacy EWS model, granting an application ApplicationImpersonation was an all-or-nothing security hazard. If the service account credentials or certificate were compromised, the adversary possessed unrestricted read/write access to every executive, legal, and employee mailbox across the entire tenant.

Under the modern Microsoft Graph model:

  1. Applications register in Microsoft Entra ID and acquire app-only tokens using certificate-based authentication (RFC 7523).
  2. The application requests specific Graph scopes (e.g., Mail.ReadWrite, Calendars.ReadWrite).
  3. To prevent the application from accessing every mailbox in the organization, Exchange administrators create an Application Access Policy linked to a mail-enabled security group, restricting the app to only explicitly approved mailboxes.
Architecture: Legacy EWS Impersonation vs. Modern Scoped Graph API Legacy EWS Architecture (Retired) • Client Header: ImpersonatedUserId • Basic Auth or Legacy OAuth • Role: ApplicationImpersonation Security Risks: ✖ God-Mode: Unrestricted tenant-wide mailbox access ✖ Deprecated by Microsoft; subject to blocking ✖ Heavy EWS Managed API .NET DLL dependency Modern Microsoft Graph (Recommended) • Certificate-Based Service Principal (RFC 7523) • Specific Scopes: Mail.ReadWrite, Calendars.ReadWrite • Endpoint: /v1.0/users/{upn}/messages Governance Controls: ✔ Application Access Policies (New-ApplicationAccessPolicy) ✔ Mailbox boundary restricted to designated Security Group ✔ Native REST JSON; zero client DLLs required Action Plan for Systems Administrators: 1. Identify all legacy scripts importing Microsoft.Exchange.WebServices.dll 2. Register an Entra ID App, grant Graph Application Scopes, and scope with New-ApplicationAccessPolicy

Step 1: Registering the Entra ID Application & Granting Graph Scopes

Instead of granting tenant-wide administrative rights to a service account:

  1. Register an Application in the Microsoft Entra admin center named Enterprise-Mailbox-Automation.
  2. Navigate to API Permissions > Add a permission > Microsoft Graph > Application permissions.
  3. Select only the exact permissions needed:

- Mail.ReadWrite (to create drafts, send emails, or organize folders) - Tasks.ReadWrite or Calendars.ReadWrite

  1. Grant Admin Consent for your tenant.
  2. Upload the public key of an enterprise certificate under Certificates & secrets.

Step 2: Restricting Access via Exchange Online Application Access Policies

By default, an application with Mail.ReadWrite can access every mailbox in Exchange Online. To replicate the controlled nature of impersonation without god-mode risks, use Exchange Online Application Access Policies:

POWERSHELL
# Connect to Exchange Online PowerShell via Certificate Authentication
Connect-ExchangeOnline -AppId "c841ae33-4093-4385-8a90-6a530761f9b9" `
                       -CertificateThumbprint "BEAC429DE404B46FF030811C0C9DE00DE701C027" `
                       -Organization "lab365.in"

# 1. Create a Mail-Enabled Security Group containing allowed target mailboxes
New-DistributionGroup -Name "App-Mailbox-Sync-Allowed" `
                      -Type "Security" `
                      -Members "[email protected]", "[email protected]"

# 2. Scope the Entra ID Application to only this group
New-ApplicationAccessPolicy -AppId "c841ae33-4093-4385-8a90-6a530761f9b9" `
                            -PolicyScopeGroupId "[email protected]" `
                            -AccessRight RestrictAccess `
                            -Description "Restrict Enterprise Automation app to approved pilot mailboxes"

# 3. Test the access policy against allowed and non-allowed mailboxes
Test-ApplicationAccessPolicy -AppId "c841ae33-4093-4385-8a90-6a530761f9b9" `
                             -Identity "[email protected]"
# Output: AccessCheckResult: Granted

Test-ApplicationAccessPolicy -AppId "c841ae33-4093-4385-8a90-6a530761f9b9" `
                             -Identity "[email protected]"
# Output: AccessCheckResult: Denied

Step 3: Modern PowerShell Replacement for EWS ImpersonatedUserId

Below is the complete, modern PowerShell script replacing the legacy EWS script. It authenticates to Microsoft Graph using an app-only certificate and creates a task or email directly in the target user's mailbox without installing any legacy EWS DLLs.

POWERSHELL
#Requires -Version 5.1
<#
.SYNOPSIS
    Modern Replacement for EWS Impersonation Script using Microsoft Graph SDK v2.
.DESCRIPTION
    Replaces Microsoft.Exchange.WebServices.Data.ImpersonatedUserId with
    scoped Microsoft Graph Application calls.
#>

$TenantId       = "ab85c7a1-366d-44ba-acc6-924fa0950045"
$ClientId       = "c841ae33-4093-4385-8a90-6a530761f9b9"
$CertThumbprint = "BEAC429DE404B46FF030811C0C9DE00DE701C027"

# 1. Connect to Microsoft Graph with Certificate
Connect-MgGraph -TenantId $TenantId -ClientId $ClientId -CertificateThumbprint $CertThumbprint -NoWelcome

# Target Mailbox (formerly ImpersonatedUserId)
$TargetMailbox = "[email protected]"

# 2. Create a Message Draft directly in the target mailbox
$MessageBody = @{
    subject = "Automated Migration Notice: Microsoft Graph Integration"
    importance = "Normal"
    body = @{
        contentType = "HTML"
        content = "<p>This message was created via <b>Microsoft Graph REST API</b> replacing legacy EWS impersonation.</p>"
    }
    toRecipients = @(
        @{
            emailAddress = @{
                address = "[email protected]"
            }
        }
    )
}

Write-Host "Creating draft item in target mailbox: $TargetMailbox..." -ForegroundColor Cyan

# Graph endpoint: /v1.0/users/{user-id | userPrincipalName}/messages
$NewMessage = New-MgUserMessage -UserId $TargetMailbox -BodyParameter $MessageBody

Write-Host "[+] Message created successfully! ID: $($NewMessage.Id)" -ForegroundColor Green

# 3. Send the message
Send-MgUserMessage -UserId $TargetMailbox -MessageId $NewMessage.Id

Write-Host "[+] Message sent on behalf of $TargetMailbox via Microsoft Graph!" -ForegroundColor Green

Comparing Legacy EWS vs. Modern Graph Cmdlets

ActionLegacy EWS Managed APIModern Microsoft Graph SDK v2
AuthenticationBasic Auth / WebCredentialsApp-Only Certificate (Connect-MgGraph)
Impersonation Target$service.ImpersonatedUserId-UserId "[email protected]" parameter
RBAC RoleApplicationImpersonation (Retired)App Registration Scopes (Mail.ReadWrite)
Security BoundaryNone (All mailboxes in tenant)New-ApplicationAccessPolicy -AccessRight RestrictAccess
Client RequirementMicrosoft.Exchange.WebServices.dllZero DLLs (Pure HTTPS REST / Graph SDK)

Summary

Continuing to use legacy EWS impersonation scripts introduces severe security vulnerabilities and imminent operational disruption as Microsoft enforces modern authentication and API retirements. By transitioning to Microsoft Graph SDK v2 and constraining applications with Exchange Online Application Access Policies, administrators achieve greater security, auditability, and long-term cloud supportability.