Connect PowerShell to Exchange Online / Office 365 / Azure

article #804, updated 241 days ago

The following method is appearing to work always, at least it has not yet failed in many attempts on many platforms:

$UserCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri -Credential $UserCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking

When you’re done, be sure to do the following afterwards, or you may have to wait for automatic session expiration.

Remove-PSSession $Session

There is a newer method, below, which recently we have sometimes seen fail. Visually it appears to use newer APIs. Requires Powershell 5.1:

Set-ExecutionPolicy RemoteSigned -Force
Install-Module -Name AzureAD
Install-Module MSOnline
Import-Module AzureAD