Scheduled reporting and automation tasks require unattended PowerShell authentication to Exchange Online and on-premises Exchange Server Subscription Edition (SE) / 2019 without interactive MFA prompts or insecure plain-text passwords in scripts.
Important Security & Deprecation Notice: Storing plain-text passwords in.ps1scripts or passingPSCredential(-Credential) toConnect-ExchangeOnlineis retired in Exchange Online due to Basic Auth and ROPC deprecation. Modern Exchange Online automation requires Certificate-Based App-Only Authentication (CBA) or Azure Managed Identities, while on-premises Exchange Server SE automation uses Kerberos +Microsoft.PowerShell.SecretManagement.
Unattended Exchange PowerShell Authentication Architecture
Method 1: Unattended Login to Exchange Online (Certificate-Based App-Only Auth)
In ExchangeOnlineManagement v3+, use an Entra ID App Registration granted the Exchange.ManageAsApp API permission and an X.509 certificate installed in the task runner's certificate store:
#Requires -Modules ExchangeOnlineManagement
$AppId = "c841ae33-4093-4385-8a90-6a530761f9b9"
$Thumbprint = "BEAC429DE404B46FF030811C0C9DE00DE701C027"
$Tenant = "lab365.onmicrosoft.com"
# Connect unattended with zero prompts
Connect-ExchangeOnline `
-AppId $AppId `
-CertificateThumbprint $Thumbprint `
-Organization $Tenant `
-ShowBanner:$false
# Run automated daily report
Get-EXOMailbox -ResultSize 10 | Select-Object DisplayName, PrimarySmtpAddress
Disconnect-ExchangeOnline -Confirm:$false
For full step-by-step instructions on generating the self-signed certificate and registering the Entra application, see our Connect-ExchangeOnline Certificate Authentication Runbook.
Method 2: Unattended Login to On-Premises Exchange Server SE / 2019 (DPAPI Encrypted Credential)
When running a scheduled task from a domain-joined management server against on-premises Exchange Server SE / 2019:
Step 1: Save the Service Account Credential Encrypted with Windows DPAPI (One-Time Setup)
Run this once logged in as the exact Windows service account that will execute the Scheduled Task. Windows Data Protection API (DPAPI) encrypts the password so that only that specific user account on that specific machine can decrypt it:
$CredPath = "C:\Automation\Vault\ExchOnPremCred.xml"
New-Item -ItemType Directory -Path (Split-Path $CredPath) -Force | Out-Null
Get-Credential | Export-Clixml -Path $CredPath
Step 2: Connect via Kerberos Remote PowerShell in Your Scheduled Script
$CredPath = "C:\Automation\Vault\ExchOnPremCred.xml"
$ExchServer = "exse01.lab365.in"
$Cred = Import-Clixml -Path $CredPath
$Session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri "http://$ExchServer/PowerShell/" `
-Authentication Kerberos `
-Credential $Cred
Import-PSSession $Session -DisableNameChecking -AllowClobber | Out-Null
# Execute on-premises Exchange commands
Get-MailboxDatabaseCopyStatus * | Format-Table Name, Status, CopyQueueLength -AutoSize
Remove-PSSession $Session
💬 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