PowerShell

PowerShell

This article explains how to connect Office 365 using PowerShell and more!

How to connect to Office 365

Before you start

Before you can use any Office 365 PowerShell cmdlets, you need to download and install them following the links below.

  1. Install Azure AD PowerShell module
  2. Download SharePoint Online Management Shell
  3. Download Skype for Business, Windows PowerShell Module

After installing the modules, you’re ready to go!

First we save your credentials and tenant name to variables, so we can use them later. For tenant, use the first part of your tenant name: yourtenant.onmicrosoft.com

$cred=Get-Credential
$tenant="yourtenant"

Office 365

To connect to Office 365, please use the following command:

Connect-MsolService -credential $credential

SharePoint Online

To connect to SharePoint online, please use the following command:

Connect-SPOService -Url https://$tenant-admin.sharepoint.com -Credential $cred

Skype for Business

There are two phases to connect to Skype for Business. First you create a remote session and then you import it to your local PowerShell session:

$s4bses = New-CsOnlineSession -Credential $cred
Import-PSSession $s4bses

Exchange Online

Connecting to Exchange Online is similar to Skype for Business connection. However, you do not need to install any module to connect to Exchange Online.

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

The complete connection script

# Save credentials and tenant for later use

$cred=Get-Credential
$tenant="yourtenant"

# Connect to Office 365 (Azure AD)

Connect-MsolService -credential $cred

# Connect to SharePoint Online

Connect-SPOService -Url https://$tenant-admin.sharepoint.com -Credential $cred

# Connect to Skype for Business

$s4bses = New-CsOnlineSession -Credential $cred
Import-PSSession $s4bses

# Connect to Exchange Online

$exses = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
Import-PSSession $exses
Dr Nestori Syynimaa avatar
About Dr Nestori Syynimaa
Dr Syynimaa works as Senior Principal Information Security Researcher at Secureworks CTU (Counter Threat Unit).
Before moving to his current position, Dr Syynimaa worked as a CIO, consultant, trainer, and university lecturer for over 20 years. He is a regular speaker in scientific and professional conferences related to Microsoft 365 and Azure AD security.

Dr Syynimaa is Microsoft Certified Expert (Microsoft 365), Microsoft Certified Azure Solutions Architect Expert, Microsoft Certified Trainer, and Microsoft MVP (Enterprise Mobility, Identity and Access & Intune).
comments powered by Disqus