Connect to Exchange Online PowerShell After Basic Authentication

3 min read James Futhey First published February 18, 2023

Microsoft turned off Basic authentication in Exchange Online, and remote PowerShell went with it. Every script that started with New-PSSession and -Authentication Basic is now broken, along with most of the examples you find online and probably your team's runbooks.

(╯°□°)╯︵ ┻━┻

The replacement is Connect-ExchangeOnline from the ExchangeOnlineManagement module. It uses modern authentication, so it works with MFA.

The script that stopped working

This is the old connection. If your script looks like this, replace it:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session

Microsoft's Basic authentication deprecation page says Basic authentication is now disabled in all tenants, and Remote PowerShell is on the list of removed protocols. Since October 2023, Exchange Online PowerShell runs over REST API connections, which don't need Basic authentication in WinRM. Connect-EXOPSSession from version 1 of the module is gone too.

The cmdlets themselves didn't change. Get-Mailbox, Set-CalendarProcessing and the rest keep the same names and parameters, so only the connection lines need rewriting.

Install the ExchangeOnlineManagement module

Install the module from the PowerShell Gallery:

Install-Module -Name ExchangeOnlineManagement

PowerShell doesn't run scripts by default. If the connect fails with "running scripts is disabled on this system", open PowerShell as administrator and set the execution policy:

Set-ExecutionPolicy RemoteSigned

Update an existing installation with Update-Module:

Update-Module -Name ExchangeOnlineManagement -Scope CurrentUser

Microsoft's module page lists the PowerShell versions each release needs. Every version works in Windows PowerShell 5.1. In PowerShell 7, module 3.10.0 and later need PowerShell 7.6.0 or later. Versions 3.5.0 to 3.9.2 need PowerShell 7.4.0 or later.

Connect with Connect-ExchangeOnline

Run Connect-ExchangeOnline with your admin account. Microsoft's Connect to Exchange Online PowerShell article has the same steps for each cloud:

Connect-ExchangeOnline -UserPrincipalName admin@contoso.onmicrosoft.com -ShowBanner:$false

A sign-in window opens. Enter your password, then the MFA code if your account uses MFA. In PowerShell 7 the sign-in opens in your default web browser.

-ShowBanner:$false hides the long welcome message the module prints on every connect.

To check the connection, run any Exchange cmdlet:

Get-AcceptedDomain

Get-PSSession doesn't show REST API connections. Use Get-ConnectionInformation to see your open connections.

Sign in with a device code

The -Device switch lets you sign in from another device. It works only in PowerShell 7. Use it on a server or a terminal with no web browser:

Connect-ExchangeOnline -Device

PowerShell prints a code and waits:

To sign in, use a web browser to open the page https://microsoft.com/devicelogin and enter the code XXXXXXXXX to authenticate.
  1. Open microsoft.com/devicelogin on any device with a browser.
  2. Enter the code.
  3. Sign in with your admin account and complete MFA.
  4. Select Continue at the confirmation prompt.

Microsoft Enter code device sign-in prompt used for modern authentication

The PowerShell window then finishes connecting.

Other sign-in options

  • -InlineCredential asks for the user name and password inside the PowerShell window. It works in PowerShell 7 for accounts without MFA.
  • -Credential takes a PSCredential from Get-Credential. Microsoft says not to use it for accounts with MFA. To build and store one, see the Get-Credential guide.
  • -DelegatedOrganization connects a Microsoft partner account to a customer tenant, for example -DelegatedOrganization fabrikam.onmicrosoft.com.
  • -ExchangeEnvironmentName picks a government or sovereign cloud, such as O365USGovGCCHigh or O365USGovDoD. Leave it out for commercial Microsoft 365 and GCC.
  • -DisableWAM turns off Web Account Manager. The module uses it by default from version 3.7.0. Add the switch if sign-in fails with a WAM error. It is in version 3.7.2 and later.
  • -LoadCmdletHelp loads help for Get-Help. From version 3.7.0 the module doesn't load it by default.

Connect from an unattended script

A scheduled task can't complete an MFA prompt. Use app-only authentication with a certificate instead:

  1. Register an app in Microsoft Entra ID.
  2. Give the app the Exchange.ManageAsApp API permission and grant admin consent.
  3. Create a certificate and upload the public key to the app.
  4. Assign the app an Exchange role, such as Exchange Administrator.

Then connect with the certificate thumbprint, the app ID and your tenant:

Connect-ExchangeOnline -CertificateThumbPrint "012THISISADEMOTHUMBPRINT" -AppID "36ee4c6c-0812-40a2-b820-b22ebd02bce3" -Organization "contoso.onmicrosoft.com"

-CertificateThumbPrint works only on Windows. On other platforms, use -CertificateFilePath or -Certificate.

An Azure resource with a managed identity can connect without a certificate:

Connect-ExchangeOnline -ManagedIdentity -Organization "contoso.onmicrosoft.com"

Disconnect when you're done

Close the connection at the end of every session or script:

Disconnect-ExchangeOnline -Confirm:$false

Microsoft warns that closing the window without disconnecting can use up all your available sessions. You then have to wait for them to expire before you can connect again.

What doesn't carry over

Invoke-Command doesn't work in REST API connections. Scripts that sent a script block to the old remote session need rewriting.

REST API cmdlets time out after 15 minutes. Split long bulk jobs, such as updating 10,000 distribution group members, into smaller batches.

Add a display to your office

Every room, on the door.

Live availability on every meeting room. Works with Microsoft 365, Exchange and Google Workspace.