/errors/exchange-online/must-call-connect-exchangeonline
You must call Connect-ExchangeOnline before calling any other cmdlet.
- tool
- Exchange Online PowerShell
- exit code
- 1
- versions
- ExchangeOnlineManagement 3.x
- os
- windows, linux, macos
You must call Connect-ExchangeOnline before calling any other cmdlet. is what the Exchange Online PowerShell module (ExchangeOnlineManagement, "EXO V3") prints when you run one of its built-in cmdlets, such as Get-EXOMailbox, without an open Exchange Online connection. Connect first, in the same PowerShell session:
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName admin@contoso.com
Get-EXOMailbox -Identity user@contoso.comReplace admin@contoso.com with an account that has an Exchange admin role, and user@contoso.com with a mailbox.
Reproduce it
With the module imported and no connection, Get-EXOMailbox throws this terminating error (module 3.10.1, Windows PowerShell 5.1):
Import-Module ExchangeOnlineManagement
try {
Get-EXOMailbox -Identity user@contoso.com -ErrorAction Stop
} catch {
$_.Exception.Message
}Output
You must call Connect-ExchangeOnline before calling any other cmdlet.
Which cmdlets print it
The module ships a small set of local cmdlets: Connect-ExchangeOnline, Connect-IPPSSession, Disconnect-ExchangeOnline, Get-ConnectionInformation and the nine Get-EXO* cmdlets (Get-EXOMailbox, Get-EXORecipient, Get-EXOMailboxPermission and so on). The Get-EXO* cmdlets exist as soon as the module is imported, so without a connection they fail with this message.
All the other Exchange cmdlets (Get-Mailbox, Set-Mailbox, Add-MailboxPermission, ...) are created only by Connect-ExchangeOnline. Without a connection they do not exist at all, and PowerShell prints a different error: The term 'Get-Mailbox' is not recognized.
Check the connection in a script
if (-not (Get-ConnectionInformation)) {
Connect-ExchangeOnline -UserPrincipalName admin@contoso.com -ShowBanner:$false
}Get-ConnectionInformation returns one object per open connection and nothing when there is none. For scripts that run without a person signing in, use certificate-based app-only authentication.
Sources
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/module/exchangepowershell/connect-exchangeonline
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/exchange/connect-to-exchange-online-powershell
- learn.microsoft.comlearn.microsoft.com/en-us/powershell/exchange/exchange-online-powershell-v2