Connect PowerShell to Exchange Online / Office 365 / Azure

article #804, updated 430 days ago

The following method is the Microsoft-recommended current one. You may need to use Install-Module instead of Update-Module depending on pre-existing installations et cetera:

Set-ExecutionPolicy RemoteSigned -Force -Scope Process
Update-Module -Name ExchangeOnlineManagement
Update-Module -Name AzureAD
Update-Module -Name MSOnline
Import-Module -Name ExchangeOnlineManagement
Import-Module -Name MSOnline
Import-Module -Name AzureAD
Connect-ExchangeOnline -UserPrincipalName -ShowProgress $true

The following method is the previous, and is the one which works pre-Windows-10 and/or Powershell before 5.1:

$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