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.
- Install Azure AD PowerShell module
- Download SharePoint Online Management Shell
- 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