Connect to all Office 365 services from single Powershell session


To totally unlock this section you need to Log-in


Login

When you use PowerShell to manage Office 365, it is possible to have up to five different Windows PowerShell sessions open at the same time corresponding to Office 365 admin center, SharePoint Online, Exchange Online, Skype for Business Online, and the Security & Compliance Center.

This is not optimal for managing Office 365 because you can't exchange data among those five windows for cross-service management. This topic describes how to use a single instance of Windows PowerShell from which you can manage Office 365, Skype for Business Online, Exchange Online, SharePoint Online, and the Security & Compliance Center.

Before you begin

Before you can manage all of Office 365 from a single instance of Windows PowerShell, consider the following prerequisites:

  • The Office 365 work or school account that you use for these procedures needs to be a member of an Office 365 admin role.

You can use the following 64-bit versions of Windows:

  • Windows 10
  • Windows 8.1 or Windows 8
  • Windows Server 2016
  • Windows Server 2012 R2 or Windows Server 2012
  • Windows 7 Service Pack 1 (SP1)*
  • Windows Server 2008 R2 SP1*: you need to install the Microsoft .NET Framework 4.5.x and then either the Windows Management Framework 3.0 or the Windows Management Framework 4.0.

Windows PowerShell needs to be configured to run signed scripts for Skype for Business Online, Exchange Online, and the Security & Compliance Center. To do this, run the following command in an elevated Windows PowerShell session (a Windows PowerShell window you open by selecting Run as administrator).

Set-ExecutionPolicy RemoteSigned

Connection steps when using a password

Here are the steps to connect to all the services in a single PowerShell window.

  • Open Windows PowerShell as an administrator (use Run as administrator).
  • Run this command, and enter your Office 365 work or school account credentials.
$credential = Get-Credential

Run this command to connect to Azure Active Directory (AD).

Connect-AzureAD -Credential $credential

Run these commands to connect to SharePoint Online. Replace <domainhost> with the actual value for your domain. For example, for litwareinc.onmicrosoft.com, the <domainhost> value is litwareinc.

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

Run these commands to connect to Skype for Business Online. A warning about increasing the WSMan NetworkDelayms value is expected the first time you connect and should be ignored.

Import-Module SkypeOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession

Run these commands to 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

Run these commands to connect to the Security & Compliance Center.

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

Here are all the commands in a single block. Specify the name of your domain host, and then run them all at one time.

$domainHost=""
$credential = Get-Credential
Connect-AzureAD -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$domainHost-admin.sharepoint.com -credential $credential
Import-Module SkypeOnlineConnector
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri "https://outlook.office365.com/powershell-liveid/" -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $exchangeSession
$SccSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid/ -Credential $credential -Authentication "Basic" -AllowRedirection
Import-PSSession $SccSession -Prefix cc

When you are ready to close down the Windows PowerShell window, run this command to remove the active sessions to Skype for Business Online, Exchange Online, SharePoint Online, and the Security & Compliance Center:

Remove-PSSession $sfboSession ; Remove-PSSession $exchangeSession ; Remove-PSSession $SccSession ; Disconnect-SPOService

Connection steps when using multi-factor authentication

Here are all the commands in a single block to connect to Azure AD, SharePoint Online, and Skype for Buiness using multi-factor authentication in a single window. Specify the user principal name (UPN) name of a global administrator account and your domain host name, and then run them all at one time.

$acctName="<upn of a global administrator account>"
$domainHost="<domain host name, such as litware for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-AzureAD
#SharePoint Online
Connect-SPOService -Url https://$domainHost-admin.sharepoint.com
#Skype for Business Online
$sfboSession = New-CsOnlineSession -UserName $acctName
Import-PSSession $sfboSession