Related post: How-to connect to Office 365 with PowerShell and multi-factor authentication
There are several Office 365 services that can be managed with PowerShell. These are:
- Exchange Online
- Security & Compliance Center
- SharePoint Online
- Skype for Business Online
- StaffHub
- Teams
Related services are:
- Azure Active Directory
- Microsoft Graph
This blog post shows how to connect with PowerShell to these services. For the connection, a global administrator account is used. Multi-factor authentication is not configured for this account. A description about how to connect to Office 365 services with an account that has multi-factor authentication configured is saved for another blog post. The shown PowerShell commands are run on a Windows 10 Pro device.
On several places, the object $credential
is shown as a value of the cmdlet parameter “Credential”. This object can be created as described in this post or by executing the following code:
$credential = Get-Credential
Exchange Online
There are some pre-requisites before a connection to the Exchange Online can be made. The pre-requisites are described here.
Connecting to Exchange Online can be done with the following code:
$exchangeSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' -ConnectionUri 'https://outlook.office365.com/powershell-liveid/' -Credential $credential -Authentication 'Basic' -AllowRedirection Import-PSSession $exchangeSession
<YOUR CODE>
Remove-PSSession $exchangeSession
, where <YOUR CODE> is the PowerShell code you want to run.
Security & Compliance Center
There are some pre-requisites before a connection to the “Security & Compliance Center” can be made. The pre-requisites are described here.
Connecting to the “Security & Compliance Center” can be done with the following code:
$SCCSession = New-PSSession -ConfigurationName 'Microsoft.Exchange' -ConnectionUri 'https://ps.compliance.protection.outlook.com/powershell-liveid/' -Credential $credential -Authentication 'Basic' -AllowRedirection Import-PSSession $SCCSession
<YOUR CODE>
Remove-PSSession $SCCSession
, where <YOUR CODE> is the PowerShell code you want to run.
SharePoint Online
There are 2 different options to connect to SharePoint Online, both having their own scope:
- SharePoint Online Management Shell
- PnP PowerShell
The first focuses mainly on the Tenant and site collection level. The second focuses on the site collection level and “below”. For both to work, the Tenant setting LegacyAuthProtocolsEnabled
must be “True”.
SharePoint Online Management Shell
There are some pre-requisites before a connection to SharePoint Online can be made. The pre-requisites are described here.
Connecting to SharePoint Online can be done with the following code:
$url = <URL>
Connect-SPOService -Url $url -Credential $credential
<YOUR CODE>
Disconnect-SPOService
, where <YOUR CODE> is the PowerShell code you want to run. <URL> is the url to the SharePoint admin center. Example: https://contoso-admin.sharepoint.com
PnP PowerShell
There are some pre-requisites before a connection to SharePoint Online can be made. The pre-requisites are described here. Also, a credential can be added to the Credential Store as described here. This is not a necessity, but it is really useful. Connecting to SharePoint Online can then be done with the following code:
$url = <URL>
Connect-PnPOnline -Url $url
<YOUR CODE>
Disconnect-PnPOnline
, where <YOUR CODE> is the PowerShell code you want to run. <URL> is the url to a site. Example: https://contoso.sharepoint.com
Skype for Business Online
A PowerShell module needs to be installed before PowerShell can connect to Skype for Business Online. The software to install the module can be downloaded here.
Connecting to Skype for Business Online with PowerShell can be done with the following code:
$skypeSession = New-CSOnlineSession -Credential $credential
Import-PSSession $skypeSession
<YOUR CODE>
Remove-PSSession $skypeSession
, where <YOUR CODE> is the PowerShell code you want to run.
StaffHub
There is currently no PowerShell module for StaffHub. As can been seen from this page, cmdlets seem to be coming to us.
Teams
A PowerShell module needs to be installed before PowerShell can connect to Microsoft Teams.
- Open a PowerShell console window using “Run as Administrator”.
- Run the following code:
Install-Module -Name 'MicrosoftTeams'
This has to be done only once. Updating the module could be needed though.
Connecting to Microsoft Teams with PowerShell can be done with the following code:
Connect-MicrosoftTeams -Credential $credential
<YOUR CODE>
Disconnect-MicrosoftTeams
, where <YOUR CODE> is the PowerShell code you want to run.
Azure Active Directory
There are 2 ways to connect to Azure Active Directory (Azure AD):
- Version 1
- Version 2
As the name implies, version 2 is the newer one. Reference [1] and [2] contain some useful information about the differences and limitations.
Version 1
There are some pre-requisites before a connection to the Azure AD can be made. The pre-requisites are described here.
Connecting to Azure AD can be done with the following code:
Connect-MsolService -Credential $credential
<YOUR CODE>
, where <YOUR CODE> is the PowerShell code you want to run.
Version 2
A PowerShell module needs to be installed before PowerShell can connect to Azure AD.
- Open a PowerShell console window using “Run as Administrator”.
- Run the following code:
Install-Module -Name 'AzureAD'
This has to be done only once. Updating the module could be needed though.
Connecting to Azure AD can be done with the following code:
Connect-AzureAD -Credential $credential
<YOUR CODE>
Disconnect-AzureAD
, where <YOUR CODE> is the PowerShell code you want to run.
Microsoft Graph
Connecting to Microsoft Graph is a bit more complex then a simple connect cmdlet. How to connect to Microsoft Graph with PowerShell is saved for a future blog post.
References
[1] What’s different about the v2.0 endpoint?
[2] Should I use the v2.0 endpoint?