/commands/exchange-online/connect-exchangeonline-certificate-app-only
Connect-ExchangeOnline -AppId -CertificateThumbprint -Organization (app-only, unattended scripts)
The commands on this page have not been executed by our verification runner yet.updated
- tool
- Exchange Online PowerShell
- versions
- ExchangeOnlineManagement 3.10.1
- os
- windows, linux, macos
For scripts that run without a person signing in (scheduled tasks, Azure Automation, CI), connect to Exchange Online with certificate-based app-only authentication:
Connect-ExchangeOnline -AppId "36ee4c6c-0812-40a2-b820-b22ebd02bce3" `
-CertificateThumbprint "83213AEAC56D61C97AEE5C1528F4AC5EBA7321C1" `
-Organization "contoso.onmicrosoft.com"-AppId is the application (client) ID of the app registration, -CertificateThumbprint the thumbprint of a certificate in the local certificate store that holds the private key, and -Organization the tenant's primary .onmicrosoft.com domain (not a custom domain). Replace the three values with yours.
Parameters
The block below lists the authentication parameters of Connect-ExchangeOnline in module 3.10.1 with their types, read from the module itself:
Import-Module ExchangeOnlineManagement
$p = (Get-Command Connect-ExchangeOnline).Parameters
foreach ($n in 'AppId','CertificateThumbprint','Certificate','CertificateFilePath','CertificatePassword','Organization','ManagedIdentity','ManagedIdentityAccountId','AccessToken') {
'{0,-25} {1}' -f $n, $p[$n].ParameterType.Name
}| Certificate source | Parameters | Notes |
|---|---|---|
| Windows certificate store | -AppId -CertificateThumbprint -Organization | CertificateThumbprint is supported only on Windows |
| X509Certificate2 object | -AppId -Certificate -Organization | For certificates loaded at runtime, for example from Azure Key Vault |
| .pfx file | -AppId -CertificateFilePath -CertificatePassword -Organization | Works on every platform; the password protects the private key |
| Azure managed identity | -ManagedIdentity -Organization | Azure Automation, Azure VMs, Azure Functions; add -ManagedIdentityAccountId for a user-assigned identity |
One-time setup
- Register an application in Microsoft Entra ID (App registrations > New registration).
- API permissions: add the application permission Office 365 Exchange Online > Exchange.ManageAsApp and grant admin consent. For Security & Compliance PowerShell (
Connect-IPPSSession) the resource is Microsoft Exchange Online Protection > Exchange.ManageAsApp. - Certificate: create one and upload its public part (
.cer) under Certificates & secrets. A self-signed certificate is accepted:
$mycert = New-SelfSignedCertificate -DnsName "contoso.org" -CertStoreLocation "cert:\CurrentUser\My" -NotAfter (Get-Date).AddYears(1) -KeySpec KeyExchange
$mycert | Export-Certificate -FilePath mycert.cer
$mycert.Thumbprint- Role: assign the app's service principal a Microsoft Entra role such as Exchange Recipient Administrator, Exchange Administrator or Global Reader, or an Exchange role group through a service principal. Without a suitable role the cmdlets the script needs are not available.
Common failures
AADSTS700016: the-AppIdvalue does not exist in the tenant named by-Organization(details).- The certificate is not found: the certificate with that thumbprint (and its private key) is not in a certificate store available to the account that runs the script.
Get-Mailboxis not recognized after connecting: the app has no Exchange role (details).
Sources
Related pages
flags · exchange-onlineConnect-ExchangeOnline -Device (device code sign-in, PowerShell 7 only)Connect-ExchangeOnline -Device prints a code to enter at microsoft.com/devicelogin from any browser. It exists only in PowerShell 7, not in Windows PowerShell.errors · exchange-onlineYou must call Connect-ExchangeOnline before calling any other cmdlet.Get-EXOMailbox and the other Get-EXO cmdlets fail with this message when no Exchange Online session exists. Run Connect-ExchangeOnline first, then retry.errors · entra-idAADSTS700016: Application with identifier was not found in the directoryEntra ID found no app with that client ID in the tenant: a wrong -AppId or -ClientId, an object ID instead of the app ID, the wrong tenant, or no consent.