11 July 2026 Updated: 25 October 2026 5 min read

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
Options explained
OptionEffect
Microsoft.Graph.Authentication, Microsoft.Graph.Mail, Microsoft.Graph.Users.ActionsThe three required submodules as a positional argument -Name: authentication, mail cmdlets, and actions such as Send-MgUserMail
-Scope AllUsersInstalls the modules machine-wide under Program Files; required so they are also available to the service account configured later for the Scheduled Task (requires administrator privileges)

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
Options explained
OptionEffect
New-SelfSignedCertificate -SubjectCertificate subject name; used only for recognition in the certificate store
-CertStoreLocation "Cert:\LocalMachine\My"Stores the certificate in the computer store rather than the user store; this makes it available regardless of the signed-in user
-KeyExportPolicy NonExportablePrevents export of the private key; it never leaves the server
-KeySpec SignatureCreates a signing key; the app sign-in uses it to sign the token request assertion
-KeyLength 2048RSA key length of 2048 bits
-NotAfter (Get-Date).AddYears(2)Expiration date in two years; the certificate must then be renewed and uploaded again
Export-Certificate -CertCertificate object to export
-FilePathDestination file; as .cer, it contains only the public portion

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"
Options explained
OptionEffect
New-ApplicationAccessPolicy -AppIdApplication (Client) ID of the app registration to which the policy applies
-PolicyScopeGroupIdMail-enabled security group whose members define the scope
-AccessRight RestrictAccessRestricts the app to the group’s mailboxes; the alternative DenyAccess would block precisely those mailboxes
-DescriptionFree text for documenting the policy
Test-ApplicationAccessPolicy -IdentityChecks whether the app may access a specific mailbox (AccessCheckResult: Granted or Denied)

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
Options explained
OptionEffect
Connect-MgGraph -TenantIdTenant against which the app signs in
-ClientIdApplication (Client) ID of the app registration
-CertificateThumbprintSelects the sign-in certificate by its thumbprint from the local certificate store; the combination of -ClientId and certificate provides an app-only sign-in without a user
-NoWelcomeSuppresses the welcome text after sign-in; useful for script output and logs

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
}
Options explained
OptionEffect
Add-Type -AssemblyName System.IO.Compression.FileSystemLoads the .NET assembly containing the ZipFile class for extraction
Get-MgUserMessage -UserIdMailbox whose messages are read; specifying it is required for app-only sign-in
-Top 100Limits the query to a maximum of 100 messages per call
-Property id, subject, hasAttachmentsRequests only the required fields; this reduces the response size and speeds up the call
Get-MgUserMessageAttachment -MessageIdMessage whose attachments are listed
Invoke-MgGraphRequest -Method GETHTTP method for the direct call to the Graph API
-UriEndpoint called; the appended /$value returns the attachment’s raw file content rather than a JSON object
-OutputFilePathWrites the response directly to the destination file without retaining the entire attachment in memory
Move-MgUserMessage -DestinationId "deleteditems"Moves the processed message to the destination folder; deleteditems is the well-known folder name for “Deleted Items”

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
Options explained
OptionEffect
-UserIdMailbox on whose behalf the email is sent; it must be covered by the scope of the Application Access Policy
-BodyParameterThe complete message as a hashtable in the Graph schema: message with subject, body, recipients, and attachments, plus saveToSentItems for saving it in “Sent Items”

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")
Options explained
OptionEffect
New-ScheduledTaskAction -ExecuteProgram to run, here powershell.exe
-ArgumentCommand line for the program: -NoProfile skips profile scripts, -ExecutionPolicy Bypass bypasses the script policy for this invocation, and -File specifies the script
New-ScheduledTaskTrigger -Daily -At 06:00Daily trigger at 6:00 AM
Register-ScheduledTask -TaskNameTask name in Task Scheduler
-Action / -TriggerLinks the previously created action and trigger to the task
-UserAccount under which the task runs; the private key must be readable in its certificate store
-Password (Read-Host "Passwort")Prompts interactively for the account password so the task can start even without a signed-in user; this keeps it out of the script and history file

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.

Sources

  1. Microsoft – “Retirement of Exchange Web Services in Exchange Online”

    2023 announcement with the original October 2026 deadline for the end of EWS in Exchange Online.

    https://techcommunity.microsoft.com/blog/exchange/retirement-of-exchange-web-services-in-exchange-online/3924440
  2. Microsoft Learn – “Get access without a user (App-only)”

    app-only authentication to Microsoft Graph using a certificate.

    https://learn.microsoft.com/en-us/graph/auth-v2-service
  3. Microsoft Learn – “Role Based Access Control for Applications in Exchange Online”

    restricting an app to individual mailboxes; RBAC for Applications replaces Application Access Policies, including migration steps.

    https://learn.microsoft.com/en-us/exchange/permissions-exo/application-rbac
  4. Microsoft Learn – “Deprecation of Exchange Web Services in Exchange Online”

    current timeline, gradual retirement beginning in October 2026, and full deactivation in April 2027.

    https://learn.microsoft.com/en-us/exchange/clients-and-mobile-in-exchange-online/deprecation-of-ews-exchange-online

Comments

Comments are loaded from GitHub / Giscus.