Connect PowerShell to Exchange Online / Office 365 / Azure

article #804, updated 107 days ago

First, as with many things in Powershell, we make sure we can run lots of things:

Set-ExecutionPolicy RemoteSigned -Force

The method below appears best as of this writing for Server 2016 and Windows 10. It uses the Office 365 GUI box rather than the archaic. The first two steps probably have to be done just once per machine, until Microsoft updates the modules.

Install-Module -Name AzureAD
Install-Module MSOnline
Import-Module AzureAD

And the second method, below, will work with 2012/8, as well as 10/2016. It does appear to use at least some older APIs.

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

When you’re done with the second method, be sure to do the following afterwards, or you may have to wait for automatic session expiration. This does not apply to the first method:

Remove-PSSession $Session