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.
[PS] C:\> Install-Module MSOnline
[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.
[PS] C:\> $cred = Get-Credential
Connect to Office 365 using Azure Active Directory V2 PowerShell Module.
[PS] C:\> Connect-AzureAD -credential $cred
[PS] C:\> Connect-AzureAD
Connect to Office 365 using Azure Active Directory PowerShell Module.
[PS] C:\> Connect-MSOnline -credential $cred
[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.
[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
[PS] C:\> Import-Module SkypeOnlineConnector
[PS] C:\> $SkypeSession = New-CsOnlineSession -Credential $cred
[PS] C:\> Import-PSSession $SkypeSession
[PS] C:\> Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
[PS] C:\> Connect-SPOService -Url https://YOURDOMAIN-admin.sharepoint.com -credential $cred
[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.
[PS] C:\>Remove-PSSession $EolSession
[PS] C:\>Remove-PSSession $SkypeSession
[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.