Connect to all Office 365 Services PowerShell (Supports MFA too)

As an Office 365 admin, you spend most of the time in PowerShell to accomplish administrative tasks. Office 365 includes a wide variety of cloud services like Exchange Online, Azure Active Directory, SharePoint Online, Skype for Business Online, Teams, and Security & Compliance center.  

Each Office 365 service has their own PowerShell module to create a connection via PowerShell. If you want to work with multiple services, you need to remember (copy & paste 😉) various cmdlets and modules to create sessions manually, which is time-consuming and painful task. And the most important part comes here! MFA. Nowadays, most admin accounts are MFA enabled, which requires a different approach to create a PowerShell session.  

Don’t worry! We have created “All-in-One” PowerShell script that connects all Office 365 services using PowerShell with MFA/non-MFA account. You can connect to any/all Office 365 services, just with a single cmdlet. 

 

Script Highlights: 
  • Installs Office 365 PowerShell modules. ie, Modules required for Office 365 services are automatically downloaded and installed upon your confirmation.
  • You can connect to one or more Office 365 services via PowerShell using a single cmdlet. 
  • You can connect to Office 365 services with MFA enabled account. 
  • For non-MFA account, you don’t need to enter credential for each service. You’ll be asked to enter your credential only once! 
  • The script is scheduler friendly. i.e., credentials can be passed as a parameter instead of saving inside the script. 
  • You can disconnect all service connections using a single cmdlet. 

 

Script to Connect all Office 365 Services using PowerShell (Works for MFA too) 

We have an All-in-One PowerShell script to connect Office 365 services like Exchange Online, Azure AD, SharePoint Online, Skype for Business, Teams, and Compliance Center using PowerShell. 

As mentioned earlier, each service requires a different module, we designed our script to install the required module (if it is not installed already) after your confirmation. If you want to download and install manually, you can follow the steps we’ve given inside the script. 

 

You can Download the PowerShell script from TechNet Gallery 

 

Now I am going to explain how to use our script to connect Office 365 services, with MFA and without MFA. You can use this script to connect single service or combination of services. Also, you can use this script to install Office 365 PowerShell modules.

First, you need to download the above script.

 

Connect to all Office 365 Services (Without MFA): 

Using below cmdlet, you can connect to Office 365 services like Exchange Online, Azure Active Directory, SharePoint Online, Skype for Business Online, Teams, and Security & Compliance center.  

 

You can also pass the credential as a parameter.

Note: You can also give script’s absolute path if it’s not in the current directory. 

 

Connect to all Office 365 Services (With MFA) 

Using below cmdlet, you can connect to Office 365 services like Exchange Online, Azure Active Directory, SharePoint Online, Skype for Business Online, Teams, and Security & Compliance center with MFA.

If you use MFA enabled account, credential should be entered for each service.

For detailed usage of this script, check the use cases. 

 

More Use Cases of “Connect all Office 365 Services PowerShell Script”: 

 

Connect to Exchange Online PowerShell: 

To connect Exchange Online, run the below cmdlet. 

If you want to know detailed explanation about cmdlet, refer connect to Exchange Online PowerShell blog. 

 

Connect to Exchange Online PowerShell with MFA: 

To connect Exchange Online PowerShell with MFA, you need Microsoft’s “Exchange Online Remote PowerShell Module”. Our script will install Exchange Online MFA module (After your confirmation), when you execute a script with -MFA switch and then connects Exchange Online using MFA. 

If you want to install module manually, you can refer Connect Exchange Online with MFA blog. 

 

Connect to Office 365 PowerShell: 

To connect with the Microsoft Azure Active Directory Module for Windows PowerShell, run the below cmdlet. 

Note: Above cmdlet will install MSOnline module if it is not installed already. 

 

Connect to Office 365 PowerShell using MFA: 

To connect with the Microsoft Azure Active Directory Module for Windows PowerShell with MFA, run the below cmdlet. 

Note: Above cmdlet will install MSOnline module if it is not installed already. 

 

Connect to SharePoint Online PowerShell: 

To connect SharePoint Online using PowerShell, SharePoint Online Management Module is required. When you run a below cmdlet, it will ask for installing that module if it is not already installed.  

SharePointHostName used to connect SharePoint Online Administartion Center. For admin@Contoso.onmicrosoft.com, organization name is Contoso. 

 

Connect to SharePoint Online PowerShell with MFA: 

To connect SharePoint Online PowerShell with MFA enabled account, run the below cmdlet. 

SharePointHostName used to connect SharePoint Online Administration Center. For admin@Contoso.onmicrosoft.com, organization name is Contoso. 

 

Connect to Skype for Business Online PowerShell: 

To connect Skype for Business Online PowerShell, you need to download and install Skype for Business Online Connector module. You can download the module directly using below link:  

https://download.microsoft.com/download/2/0/5/2050B39B-4DA5-48E0-B768-583533B42C3B/SkypeOnlinePowerShell.Exe 

After installing module, run the below cmdlet to connect Skype for Business Online PowerShell 

 

Connect to Skype for Business Online PowerShell with MFA: 

Download and install Skype for Business Online Connector module using above mentioned link and then run the below cmdlet to connect Skype for Business Online PowerShell. 

 

Connect to Teams PowerShell: 

To connect Teams PowerShell, it requires Microsoft Teams Module. When you run the below cmdlet, it will install Microsoft’s Team PowerShell module and then connects to Teams. 

 

Connect to Teams PowerShell with MFA: 

To connect Teams PowerShell with MFA, run the below cmdlet. 

 

Connect to Office 365 Security & Compliance Center PowerShell: 

To manage Office 365 Security and Compliance Center from the PowerShell, run the below cmdlet 

 

Connect to Office 365 Security & Compliance Center PowerShell with MFA: 

To connect Office 365 Security & Compliance Center with MFA, you need Microsoft’s “Exchange Online Remote PowerShell Module”. Our script will install Exchange Online MFA module (After getting confirmation from you) when you execute a script with -MFA param and then connects Security & Compliance Center. 

If you want to install module manually, you can refer Install Exchange Online Remote PowerShell Module blog. 

 

Connect Multiple Office 365 Services Using PowerShell:

If you want to connect multiple services, mention the required services by using -Services param.

 

If you want to connect multiple Office 365 services with MFA, mention the required services with -MFA switch.

 

After executing the script, connected services will be listed as shown in below screenshot. 

Connect to all Office 365 Services PowerShell

Now you have connected to Office 365 services and you can manage them through PowerShell. 

 

Disconnect Office 365 Services: 

Make sure to disconnect the remote PowerShell session when you’re finished. Else you would end up using all remote PowerShell sessions available to you and you will get the following error. 

New-PSSession : outlook.office365. com Processing data from remote server outlook.office365. com failed with the following error message: Fail to create a runspace because you have exceeded the maximum number of connections allowed: 10 for the policy party: MaxConcurrency. Please close existing runspace and try again.
 

To disconnect all the Office 365 PowerShell session in the current window, run the below command. 

 

I hope this blog can help you to create a PowerShell session to Office 365 services. If you face any issues during connection, share with us through the comment section. 

 

Even if you face any other challenges in Office 365 Environment or in need of any PowerShell scripts related to Office 365, let us know in the comment section. Happy Scripting!