Access Exchange Online with PowerShell: Replace EWS with Microsoft Graph
Microsoft has been gradually retiring EWS in Exchange Online since October 2026, with full deactivation in April 2027. Learn how to register an app, authenticate a PowerShell script with a certificate, restrict access to individual mailboxes, and process messages and attachments through Microsoft Graph.
Microsoft has been gradually retiring Exchange Web Services (EWS) in Exchange Online since October 2026; EWS will be fully disabled in April 2027. Scripts that retrieve messages or attachments from a mailbox must therefore switch to Microsoft Graph.
The example in this article runs without user sign-in: it downloads ZIP attachments from a mailbox, extracts them, moves processed messages, and then sends a report. This requires an app registration, a certificate, and two Graph permissions. Values such as example.com, tenant ID, and app ID are placeholders.
1. Required PowerShell modules
Three modules from the Microsoft Graph SDK are sufficient; you do not need the entire meta-module Microsoft.Graph:
Install-Module Microsoft.Graph.Authentication, Microsoft.Graph.Mail, Microsoft.Graph.Users.Actions -Scope AllUsers
2. App registration in Entra ID
An unattended script signs in as an application with its own permissions. Create a new registration in the Entra Admin Center under App registrations, then assign these two permissions under API permissions → Microsoft Graph → Application permissions:
-
Mail.ReadWrite: read emails and move them after processing -
Mail.Send: send the report email
Then grant administrator consent and note the tenant ID and Application (Client) ID.
3. Certificate instead of client secret
An app-only sign-in works with a client secret or a certificate. A certificate is the better choice for scheduled tasks: the private key remains in the certificate store, and the script contains no password. Create the certificate on the server that runs the script and export only the public portion:
$cert = New-SelfSignedCertificate -Subject "CN=eCall-Graph" `
-CertStoreLocation "Cert:\LocalMachine\My" `
-KeyExportPolicy NonExportable -KeySpec Signature `
-KeyLength 2048 -NotAfter (Get-Date).AddYears(2)
Export-Certificate -Cert $cert -FilePath .\eCall-Graph.cer
$cert.Thumbprint # -> im Skript als Thumbprint verwenden
Upload the exported .cer-file in the app registration under Certificates & secrets. The Scheduled Task account needs read access to the private key (certlm.msc → certificate → All Tasks → Manage Private Keys).
4. Restrict access to individual mailboxes
Application permissions initially apply to all mailboxes in the tenant. Therefore, restrict the app using an Application Access Policy for a mail-enabled security group. Perform this step once in Exchange Online PowerShell. Microsoft now refers to RBAC for Applications as the replacement for Application Access Policies; there, the scope is defined through a Management Scope or an Administrative Unit, and permission is granted in Exchange Online rather than Entra ID.
New-ApplicationAccessPolicy -AppId "<App-ID>" `
-PolicyScopeGroupId "graph-mailboxes@example.com" `
-AccessRight RestrictAccess `
-Description "eCall Graph: nur Log-Postfach"
# Check effectiveness
Test-ApplicationAccessPolicy -AppId "<App-ID>" -Identity "ecall-logs@example.com"
5. Establish a connection
Sign-in uses the tenant ID, app ID, and certificate thumbprint, entirely without user interaction:
$TenantId = "00000000-0000-0000-0000-000000000000"
$ClientId = "00000000-0000-0000-0000-000000000000"
$Thumbprint = "0000000000000000000000000000000000000000"
$Mailbox = "ecall-logs@example.com"
Import-Module Microsoft.Graph.Authentication, Microsoft.Graph.Mail, Microsoft.Graph.Users.Actions
Connect-MgGraph -TenantId $TenantId -ClientId $ClientId `
-CertificateThumbprint $Thumbprint -NoWelcome
6. Read messages and download ZIP attachments
The script can now process the inbox, save and extract ZIP attachments, and move processed messages to “Deleted Items.” The download uses the endpoint /$value and Invoke-MgGraphRequest -OutputFilePath. This writes the raw content directly to a file without holding a large attachment entirely in memory:
Add-Type -AssemblyName System.IO.Compression.FileSystem
$Zielordner = "D:\Import\{0:yyyyMMdd_HHmmss}" -f (Get-Date)
$messages = Get-MgUserMessage -UserId $Mailbox -Top 100 `
-Property id, subject, hasAttachments
foreach ($msg in $messages) {
$ordner = Join-Path $Zielordner $msg.Id
New-Item -Path $ordner -ItemType Directory -Force | Out-Null
$anhaenge = Get-MgUserMessageAttachment -UserId $Mailbox -MessageId $msg.Id |
Where-Object {
$_.AdditionalProperties['@odata.type'] -eq '#microsoft.graph.fileAttachment' -and
$_.Name -like '*.zip'
}
foreach ($att in $anhaenge) {
$zip = Join-Path $ordner $att.Name
$uri = "https://graph.microsoft.com/v1.0/users/$Mailbox/messages/$($msg.Id)/attachments/$($att.Id)/`$value"
Invoke-MgGraphRequest -Method GET -Uri $uri -OutputFilePath $zip
[System.IO.Compression.ZipFile]::ExtractToDirectory($zip, $ordner)
}
# Move processed email to "Deleted Items"
Move-MgUserMessage -UserId $Mailbox -MessageId $msg.Id -DestinationId "deleteditems" | Out-Null
}
For more than 100 emails, work with Get-MgUserMessage -All or paging; one batch is usually sufficient for a monthly run.
7. Send report email through Graph
Send-MailMessage is also deprecated. Using the same app registration (permission Mail.Send), the email is sent directly through Graph, here with a file as a base64-encoded attachment:
$pfad = "D:\Reports\report.csv"
$body = @{
message = @{
subject = "eCall Report"
body = @{ contentType = "HTML"; content = "<b>Lauf erfolgreich</b>" }
toRecipients = @(@{ emailAddress = @{ address = "empfaenger@example.com" } })
attachments = @(@{
"@odata.type" = "#microsoft.graph.fileAttachment"
name = Split-Path $pfad -Leaf
contentBytes = [Convert]::ToBase64String([IO.File]::ReadAllBytes($pfad))
})
}
saveToSentItems = $true
}
Send-MgUserMail -UserId "reporting@example.com" -BodyParameter $body
8. Run unattended
As a scheduled task, the script runs without sign-in because the certificate is in the account’s store:
$action = New-ScheduledTaskAction -Execute "powershell.exe" `
-Argument '-NoProfile -ExecutionPolicy Bypass -File "D:\Scripts\graph-import.ps1"'
$trigger = New-ScheduledTaskTrigger -Daily -At 06:00
Register-ScheduledTask -TaskName "eCall-Graph-Import" -Action $action -Trigger $trigger `
-User "DOMAIN\svc-ecall" -Password (Read-Host "Passwort")
The complete example with logging and error handling is available on GitHub: pfstr/ecall-log-analyzer.
Change log
- October 25, 2026: Corrected: EWS in Exchange Online has been gradually retired since October 2026 and will not be fully disabled until April 2027. Added: RBAC for Applications as Microsoft’s replacement for Application Access Policies, with a new source and current timeline.
Comments
Comments are loaded from GitHub / Giscus.