Connect to Office 365 with Powershell

There are several services of Office 365 you can connect to using PowerShell.  AzureAD, Exchange Online, Skype for Business Online, SharePoint Online, and the Security & Compliance Center are all available to manage using remote PowerShell. But they each require a separate connection be made. You can connect up to each of these services individually or you can import several services into one PowerShell session.

Several modules will need to be installed in order to connect to specific applications. To begin, we need to install the Microsoft Online Service Sign-in Assistant for IT professionals. This gives us end users sign on abilities to connect of Office 365. Note: To get this to install on Windows 10, I had to use the beta version.

We now need to install the Azure Active Directory Module for Window PowerShell. This should be done through an administrative PowerShell session. The new version (V2) has changed from MSOL to AzureAD commands. However, I would recommend installing both versions as I have found some MSOL commands that haven’t been moved to V2 yet.

To Install the older (MSOL) version, run the command:
[PS] C:\> Install-Module MSOnline
To install version 2, run the command:
[PS] C:\> Install-Module -Name AzureAD

To manage SharePoint Online and Skype for Business online using PowerShell, you will need to install those modules as well.

Now that you have installed the required modules, you will need to make sure the account you will use to authenticate with is a member of the Global Administrator role. You can check this in the Admin Center of Office 365 under the accounts permissions.

Once permissions have been verified, you need to get the credentials so you can use them to connect to the various services. Use PowerShells built in feature to securly collect the credentials and save them to a variable:
[PS] C:\> $cred = Get-Credential

 

Connect to Office 365 using Azure Active Directory V2 PowerShell Module.

To connect using an account name and password:
[PS] C:\> Connect-AzureAD -credential $cred
To connect using MFA:
[PS] C:\> Connect-AzureAD

 

Connect to Office 365 using Azure Active Directory PowerShell Module.

To connect using account name and password:
[PS] C:\> Connect-MSOnline -credential $cred
To connect using MFA:
[PS] C:\> Connect-MSOnline

You are now connected to Office 365 Azure AD! Here you are able to manage general things like domains, users, permissions, and so on. To manage Exchange, Skype, SharePoint, and the Security and Compliance center, you will have to connect to those services individually.

To connect to Exchange Online, run the following commands. Keep in mind that you can use the same credentials stored earlier to connect to all the separate services.
[PS] C:\> $EolSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $cred -Authentication "Basic" -AllowRedirection
[PS] C:\> Import-PSSession $EolSession -DisableNameChecking
To connect to Skype for Business Online, run the following set of commands:
[PS] C:\> Import-Module SkypeOnlineConnector
[PS] C:\> $SkypeSession = New-CsOnlineSession -Credential $cred
[PS] C:\> Import-PSSession $SkypeSession
To connect to SharePoint Online run the following commands (be sure to adjust the domain accordingly by adding your domain here):
[PS] C:\> Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
[PS] C:\> Connect-SPOService -Url https://YOURDOMAIN-admin.sharepoint.com -credential $cred
To connect to the Security and Compliance Center run the following commands:
[PS] C:\> $SecComSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $cred -Authentication Basic -AllowRedirection
[PS] C:\> Import-PSSession $SecComSession -Prefix sec

The Security & Compliance Center and Exchange Online have several PowerShell cmdlets that are the same. So if you plan on connecting to to both EOL and the Security & Compliance Center simultaneously, you need to tell PowerShell to add a prefix to the $SecComSession cmdlets. In my example I’ve used  ‘sec’. So all Security and Compliance cmdlets begin with ‘sec’, For example, the cmdlet Get-ComplianceSearch becomes Get-secComplianceSearch.

When you are all done, it’s important to remember to close all your remote PowerShell sessions. Office 365 limits the number of open connections available. If you just close PowerShell, this leaves stale connections in the background. This could potentially cause connection issues due to session limits if you aren’t careful. These sessions will eventually time out, but I find it easier just to close the sessions when I’m done so I don’t cause myself or another administrator a headache. To cleanly close the sessions the following commands can be used. Each connection to a service will need to be closed individually.

To close the Exchange Online session:
[PS] C:\>Remove-PSSession $EolSession
To close the Skype For Business session:
[PS] C:\>Remove-PSSession $SkypeSession
To close the SharePoint online session:
[PS] C:\>Disconnect-SPOService
To close the Security and Compliance session:
[PS] C:\>Remove-PSSession $SecComSession

The connection to Office 365 (Connect-MSOLservice or Connect-AzureAD) doesn’t have a command to close the session so at this point you can just close PowerShell.