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 a CIO of eight cities and municipalities surrounding Tampere, the largest inland city in Nordic countries. He also runs his own consultation business Gerenios. Before moving to his current position, Dr Syynimaa worked as a consultant, trainer, and university lecturer for almost 20 years. He is a regular speaker on Office 365 and Azure security in scientific and professional conferences. Dr Syynimaa holds MCSA (Office 365) and is Microsoft Certified Trainer.
comments powered by Disqus