Connect To All Office 365 Services With PowerShell

Since I had to accumulate the following through trial and error of outdated links and outdated technet articles, I have created the up to date defacto version of how to connect to all Office 365 sessions through PowerShell. I divided each service up into its own section along with a combined function to connect to all services.

Prerequisites: Download and install the following:
1) Microsoft .NET Framework 4.5.2
2) Windows Management Framework 4.0
3) Microsoft Online Services Sign-in Assistant for IT Professionals RTW (64-bit version)
4) Windows Azure Active Directory Module for Windows PowerShell (64-bit version)
5) Skype for Business Online Windows PowerShell Module
6) SharePoint Online Management Shell
7) Execution policy of PowerShell needs to be at least RemoteSigned.

Step #1: The first step is to enter your office 365 credentials. This would be your Office 365 sign in information that has the required privileges.

$credential = Get-Credential

Step #2: Office 365 tenant – The next step is to connect to the online tenant.

Import-Module MsOnline
Connect-MsolService -Credential $credential

Step #3: SharePoint Online – In no particular order we can next connect to SharePoint Online. *Replace yourtenantnamehere with your tenant name.

Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://yourtenantnamehere-admin.sharepoint.com -credential $credential

Step #4: Skype For Business Online – Next up on our list is Skype For Business Online. If you have Lync / Skype For Business on premise you will need to utilize the -OverrideAdminDomain parameter. *Replace yourtenantnamehere with your tenant name.

Import-Module LyncOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential -OverrideAdminDomain "yourtenantnamehere.onmicrosoft.com"
Import-PSSession $sfboSession

Step #5: Exchange Online – The following command will connect to Exchange Online.

$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking

Step #6: Office 365 Compliance – The final service is Office 365 Compliance.

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

Step #7: We can now combine all of our service connections into one function called Connect-Office365.

function Connect-Office365
{<# Credentials #>
$credential = Get-Credential

<# Office 365 Tenant #>
Import-Module MsOnline
Connect-MsolService -Credential $credential

<# SharePoint Online #>
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://yourtenantnamehere-admin.sharepoint.com -credential $credential

<# Skype For Business Online #>
Import-Module LyncOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential -OverrideAdminDomain "yourtenantnamehere.onmicrosoft.com"
Import-PSSession $sfboSession

<# Exchange Online #>
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession -DisableNameChecking

<# Office 365 Compliance #>
$ccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://ps.compliance.protection.outlook.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $ccSession -Prefix cc
}

Step #8: As with any remote PowerShell session, you will want to clean up your sessions when you disconnect. Here is a function for that very purpose.

function Disconnect-Office365
{ Remove-PSSession $sfboSession; Remove-PSSession $exchangeSession; Remove-PSSession $ccSession; Disconnect-SPOService }

Feel free to leave comments, suggestions, or requests in the comments.

5 Responses

  1. ajhstn says:

    You can also encrypt your credential with Export-Clixml -Path, then just import and decrypt it at connect time to automate.

    Also with the newest Azure AD cmdlets we don’t need the signin assistant anymore.

  2. Rob de Jong says:

    Hi Steve,

    I noticed you are using the older MSOnline PowerShell module in your examples. It may be useful to start using the newer Azure Active Directory PowerShell V2 module instead, as we will begin deprecating the MSOnline module when we have migrated the functionality of the MSOnline module to the newer module – currently planned for the Spring of 2017.

    Thanks,

    Rob de Jong

Leave a Reply to ajhstn Cancel reply

Your email address will not be published. Required fields are marked *