Connecting to Office 365 using PowerShell

A brief set of instructions to connect to Office 365 online services using PowerShell, including Azure AD, Exchange Online, and Skype for Business Online.

Note: If you are behind a proxy server, you will need to follow this in order for this to work: https://www.msdonkey.com/powershell/configuring-powershell-to-work-behind-a-proxy-server/

1. Install the 64-bit version of the Microsoft Online Services Sign-in Assistant: Microsoft Online Services Sign-in Assistant for IT Professionals RTW.
2. Install the Microsoft Azure Active Directory Module for Windows PowerShell with these steps:
○ Open an administrator-level PowerShell command prompt
○ Run the command:

Install-Module MSOnline

Get your credentials and connect:

$creds = Get-Credential
Connect-MsolService -Credential $creds
Get-MsolDomain (to confirm if you are connected to the right tenant)

Note: If your account is 2FA enabled, just use the command: Connect-MsolService and then enter your credentials and 2FA authentication.

I would also highly recommend changing the window title, especially if you connect to multiple tenants. This reduces the chances of making a change on the wrong tenant!

$host.ui.RawUI.WindowTitle = "CustomerX: Production"

If you also want to manage Skype Online:

Download and install the Skype for Business Online Connector module.

To connect to Skype Online:

Import-Module SkypeOnlineConnector
$SkypeSession = New-CsOnlineSession -Credential $creds
Import-PSSession $SkypeSession
Get-CsExternalUserCommunicationPolicy (to test)

To connect to Exchange Online:

$ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection
Import-PSSession $ExchangeSession
get-mailbox (to test and verify which tenant you are connected to Skype)
Get-Organizationconfig (useful)

If you need to use your proxy with remote PSSessions, connect as follows:

$proxyOptions = New-PSSessionOption -ProxyAccessType IEConfig
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $creds -Authentication Basic -AllowRedirection -SessionOption $proxyOptions
Import-PSSession $Session

Note: If you have MFA enabled you need to install the old Exchange PowerShell module, see here: https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps

To connect to the Security& Compliance center:

$creds = get-credential
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $creds -Authentication "Basic" -AllowRedirection
Import-PSSession $SccSession -Prefix cc

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $creds -Authentication Basic –AllowRedirection
Import-PSSession $Session
Get-RetentionCompliancePolicy (to test)

See https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-office-365-powershell and https://docs.microsoft.com/en-us/office365/enterprise/powershell/manage-skype-for-business-online-with-office-365-powershell for more information.

Also https://docs.microsoft.com/en-us/office365/enterprise/powershell/connect-to-all-office-365-services-in-a-single-windows-powershell-window

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.