Connect to all Microsoft 365 services in a single PowerShell window

When you use PowerShell to manage Microsoft 365, you can have multiple PowerShell sessions open at the same time. You might have different PowerShell windows to manage user accounts, SharePoint Online, Exchange Online, Skype for Business Online, Microsoft Teams, and the Security & Compliance center.

This scenario isn't optimal for managing Microsoft 365, because you can't exchange data among those windows for cross-service management. This article describes how to use a single instance of PowerShell to manage Microsoft 365 accounts, Skype for Business Online, Exchange Online, SharePoint Online, Microsoft Teams, and the Security & Compliance Center.

Note

This article currently only contains the commands to connect to the Worldwide (+GCC) cloud. Notes provide links to articles about connecting to the other Microsoft 365 clouds.

Before you begin

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

  • The Microsoft 365 work or school account that you use must be a member of a Microsoft 365 admin role. For more information, see About admin roles. This is a requirement for PowerShell for Microsoft 365, but not necessarily for all other Microsoft 365 services.

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

    • Windows 10

    • Windows 8.1 or Windows 8

    • Windows Server 2019

    • 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 Microsoft .NET Framework 4.5.x and then Windows Management Framework 3.0 or 4.0. For more information, see Windows Management Framework.

      You need to use a 64-bit version of Windows because of the requirements for the Skype for Business Online module and one of the Microsoft 365 modules.

  • You need to install the modules that are required for Azure Active Directory (Azure AD), Exchange Online, SharePoint Online, Skype for Business Online and Teams:

  • PowerShell must be configured to run signed scripts for Skype for Business Online and the Security & Compliance Center. Run the following command in an elevated PowerShell session (a PowerShell session that you Run as administrator).

    Set-ExecutionPolicy RemoteSigned
    

Exchange Online and Security & Compliance Center with the Exchange Online PowerShell V2 module

The procedures in this article use the Exchange Online PowerShell V2 module to connect to both Exchange Online and the Security & Compliance Center. But currently you can't connect to both in the same PowerShell window. So you have to choose to connect to one or the other when you configure a PowerShell window for multiple Microsoft 365 services.

Connection steps when using just a password

Follow these steps to connect to all the services in a single PowerShell window when you're using just a password for sign-in.

  1. Open Windows PowerShell.

  2. Run this command and enter your Microsoft 365 work or school account credentials.

    $credential = Get-Credential
    
  3. Run this command to connect to Azure AD by using the Azure Active Directory PowerShell for Graph module.

    Connect-AzureAD -Credential $credential
    

    Or if you're using the Microsoft Azure Active Directory Module for Windows PowerShell module, run this command.

    Connect-MsolService -Credential $credential
    

    Note

    PowerShell Core doesn't support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. You must run these cmdlets from PowerShell.

  4. Run these commands to connect to SharePoint Online. Specify the organization name for your domain. For example, for "litwareinc.onmicrosoft.com", the organization name value is "litwareinc".

    $orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
    Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $Credential
    
  5. Run these commands to connect to Skype for Business Online. A warning about increasing the WSMan NetworkDelayms value will appear the first time that you connect. Ignore it.

    Note

    Skype for Business Online Connector is currently part of the latest Teams PowerShell module. If you're using the latest Teams PowerShell public release, you don't need to install the Skype for Business Online Connector.

    Import-Module MicrosoftTeams
    $sfboSession = New-CsOnlineSession -Credential $credential
    Import-PSSession $sfboSession
    
  6. Run this command to connect to Exchange Online.

    Import-Module ExchangeOnlineManagement
    Connect-ExchangeOnline -Credential $credential -ShowProgress $true
    

    Note

    To connect to Exchange Online for Microsoft 365 clouds other than Worldwide, see Connect to Exchange Online PowerShell.

    Alternatively, run these commands to connect to the Security & Compliance Center.

    $acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
    Import-Module ExchangeOnlineManagement
    Connect-IPPSSession -UserPrincipalName $acctName
    

    Note

    To connect to the Security & Compliance Center for Microsoft 365 clouds other than Worldwide, see Connect to Security & Compliance Center PowerShell.

    Run these commands to connect to Teams PowerShell.

    Import-Module MicrosoftTeams
    Connect-MicrosoftTeams -Credential $credential
    

    Note

    To connect to Microsoft Teams clouds other than Worldwide, see Connect-MicrosoftTeams.

Azure Active Directory PowerShell for Graph module

Here are the commands for all the services except Security & Compliance Center in a single block when you use the Azure Active Directory PowerShell for Graph module. Specify the name of your domain host and run them all at the same time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$credential = Get-Credential
Connect-AzureAD -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -Credential $credential -ShowProgress $true
Import-Module MicrosoftTeams
Connect-MicrosoftTeams -Credential $credential

Here are the commands for all the services except Exchange Online in a single block when you use the Azure Active Directory PowerShell for Graph module. Specify the name of your domain host and the UPN for the sign-in and run them all at the same time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$credential = Get-Credential -UserName $acctName
Connect-AzureAD -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName $acctName
Import-Module MicrosoftTeams
Connect-MicrosoftTeams -Credential $credential

Microsoft Azure Active Directory Module for Windows PowerShell module

Here are the commands for all the services except Security & Compliance Center in a single block when you use the Microsoft Azure Active Directory Module for Windows PowerShell module. Specify the name of your domain host and run them all at one time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$credential = Get-Credential
Connect-MsolService -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -Credential $credential -ShowProgress $true
Import-Module MicrosoftTeams
Connect-MicrosoftTeams -Credential $credential

Here are the commands for all the services except Exchange Online in a single block when you use the Microsoft Azure Active Directory Module for Windows PowerShell module. Specify the name of your domain host and the UPN for the sign-in and run them all at one time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$credential = Get-Credential -UserName $acctName
Connect-AzureAD -Credential $credential
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName $acctName
Import-Module MicrosoftTeams
Connect-MicrosoftTeams -Credential $credential

Connection steps when using multi-factor authentication

Azure Active Directory PowerShell for Graph module

Here are all the commands in a single block to connect to multiple Microsoft 365 services except Security & Compliance Center when you use multi-factor authentication with the Azure Active Directory PowerShell for Graph module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-AzureAD
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -UserName $acctName
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName $acctName -ShowProgress $true
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

Here are all the commands in a single block to connect to multiple Microsoft 365 services except Exchange Online with multi-factor authentication when you use the Azure Active Directory PowerShell for Graph module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-AzureAD
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -UserName $acctName
Import-PSSession $sfboSession
#Security & Compliance Center
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

Microsoft Azure Active Directory Module for Windows PowerShell module

Here are all the commands in a single block to connect to multiple Microsoft 365 services except Security & Compliance Center when you use multi-factor authentication with the Microsoft Azure Active Directory Module for Windows PowerShell module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-MsolService
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -UserName $acctName
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName $acctName -ShowProgress $true
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

Here are all the commands in a single block to connect to multiple Microsoft 365 services except Exchange Online when you use multi-factor authentication with the Microsoft Azure Active Directory Module for Windows PowerShell module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-MsolService
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
Import-Module MicrosoftTeams
$sfboSession = New-CsOnlineSession -UserName $acctName
Import-PSSession $sfboSession
#Security & Compliance Center
Import-Module ExchangeOnlineManagement
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

Close the PowerShell window

To close down the PowerShell window, run this command to remove the active sessions to Skype for Business Online, SharePoint Online, and Teams:

Remove-PSSession $sfboSession ; Disconnect-SPOService ; Disconnect-MicrosoftTeams 

See also