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.
Download Script: ConnectO365Services.ps1
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, MSOnline, SharePoint Online, SharePoint PnP, Skype for Business Online, Teams, and Security & Compliance center.
1 |
./ConnectO365Services.ps1 |
You can also pass the credential as a parameter.
1 |
./ConnectO365Services.ps1 -UserName Admin@Contoso.com -Password "XXX" |
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.
1 |
./ConnectO365Services.ps1 -MFA |
If you use MFA enabled account, credential should be entered for each service.
For detailed usage of this script, you can check the use cases given at the bottom.
Challenges in Generating Report with PowerShell:
Most admins prefer PowerShell to manage their Microsoft 365 environment. But when it comes to reporting, it is always difficult to get the desired report with PowerShell. Because
- PowerShell requires a lot of effort to generate the needed reports, which is time-consuming.
- Automating report generation is difficult when you are using MFA.
- If you do not retrieve the audit data properly, it will end up with data loss which spoils the purpose.
- If you are a newbie, you might lose in search of finding the right cmdlet.
So, what if there is an easier way to generate Microsoft 365 reports? A tool like AdminDroid will help you in reporting and auditing your Microsoft 365 environment.
AdminDroid offers 1500+ pre-built reports and 20 smart analytical dashboards in which you can get statistics about your Office 365 environment effortlessly. This tool provides reports on various categories such as,
- Azure Active Directory reports
- Exchange Online reports
- SharePoint Online reports
- Microsoft Teams reports
- Security reports
- OneDrive for Business reports
- Skype for Business Online reports
- Yammer reports
- Stream reports
- PowerBI reports
- OneNote reports
- General Office 365 reports
Besides, AdminDroid offers 100+ reports and a handful of dashboards completely for free. It includes reports on Users, Licenses, Groups, Group Members, Devices, Login Activities, Password Changes, License Changes, and more. The free edition doesn’t have any restrictions in reporting functionalities such as customization, scheduling, and exporting. Download Free Office 365 reporting tool by AdminDroid and see how it helps for you.
More Use Cases of “Connect all O365 Services PowerShell Script”:
- Connect all Office 365 Services at once
- Connect all Office 365 Services at once with MFA
- PowerShell script to connect Exchange Online PowerShell
- PowerShell script to connect Exchange Online with MFA
- PowerShell script to connect Office 365 PowerShell
- PowerShell script to connect O365 with MFA
- PowerShell script to connect Azure AD PowerShell
- PowerShell script to connect Azure AD PowerShell with MFA
- PowerShell script to connect SharePoint Online PowerShell
- PowerShell script to connect SharePoint Online with MFA
- PowerShell script to connect SharePoint PnP PowerShell
- PowerShell script to connect SharePoint PnP PowerShell with MFA
- PowerShell script to connect Skype for Business PowerShell
- PowerShell script to connect Skype for Business with MFA
- PowerShell script to connect Teams PowerShell
- PowerShell script to connect Teams with MFA
- PowerShell script to connect Security and Compliance Center PowerShell
- PowerShell script to connect Security and Compliance Center with MFA
- Connect multiple Office 365 Services using PowerShell
- Disconnect all Office 365 Services at once
Connect to Exchange Online PowerShell:
To connect Exchange Online, run the below cmdlet.
1 |
./ConnectO365Services.ps1 -Services ExchangeOnline |
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.
1 |
./ConnectO365Services.ps1 -Services ExchangeOnline -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.
1 |
./ConnectO365Services.ps1 -Services MSOnline |
Note: Above cmdlet will install MSOnline module if it is not installed already.
Connect to O365 PowerShell using MFA:
To connect with the Microsoft Azure Active Directory Module for Windows PowerShell with MFA, run the below cmdlet.
1 |
./ConnectO365Services.ps1 -Services MSOnline -MFA |
Note: Above cmdlet will install MSOnline module if it is not installed already.
Connect to Azure Active Directory PowerShell:
To connect with the Microsoft Azure Active Directory PowerShell for Graph module, run the below cmdlet.
1 |
./ConnectO365Services.ps1 -Services AzureAD |
Note: Above cmdlet will install AzureAD module if it is not installed already.
Connect to Azure Active Directory PowerShell using MFA:
To connect with the Microsoft Azure Active Directory PowerShell with MFA, run the below cmdlet.
1 |
./ConnectO365Services.ps1 -Services AzureAD -MFA |
Note: Above cmdlet will install AzureAD 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.
1 |
./ConnectO365Services.ps1 -Services SharePoint -SharePointHostName <Organization Name> |
SharePointHostName used to connect SharePoint Online Administration 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.
1 |
./ConnectO365Services.ps1 -Services SharePoint -SharePointHostName <Organization Name> -MFA |
SharePointHostName used to connect SharePoint Online Administration Center. For admin@Contoso.onmicrosoft.com, organization name is Contoso.
Connect to SharePoint PnP PowerShell:
SharePoint Patterns and Practices (PnP) allows you to perform complex provisioning and artifact management actions in the SharePoint.
To connect SharePoint PnP using PowerShell, run a below cmdlet. It will ask for installing that module if it is not already installed.
1 |
./ConnectO365Services.ps1 -Services SharePointPnP -SharePointHostName <Organization Name> |
SharePointHostName used to connect SharePoint Online Administration Center. For admin@Contoso.onmicrosoft.com, organization name is Contoso.
Connect to SharePoint PnP PowerShell with MFA:
To connect SharePoint PnP PowerShell with MFA enabled account, run a below cmdlet.
1 |
./ConnectO365Services.ps1 -Services SharePointPnP -SharePointHostName <Organization Name> -MFA |
SharePointHostName used to connect SharePoint Online Administration Center. For admin@Contoso.onmicrosoft.com, organization name is Contoso.
Connect to Skype for Business Online PowerShell:
Since Skype for Business Online Connector module and the New-CSOnlineSession cmdlet were deprecated, you can use Teams PowerShell module to manage *-CsOnline* cmdlets.
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.
1 |
./ConnectO365Services.ps1 -Services Teams |
Connect to Teams PowerShell with MFA:
To connect Teams PowerShell with MFA, run the below cmdlet.
1 |
./ConnectO365Services.ps1 -Services Teams -MFA |
Connect to Office 365 Security & Compliance Center PowerShell:
To manage Office 365 Security and Compliance Center from the PowerShell, run the below cmdlet
1 |
./ConnectO365Services.ps1 -Services SecAndCompCenter |
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.
1 |
./ConnectO365Services.ps1 -Services SecAndCompCenter -MFA |
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.
1 |
./ConnectO365Services.ps1 -Services AzureAD,ExchangeOnline |
If you want to connect multiple Office 365 services with MFA, mention the required services with -MFA switch.
1 |
./ConnectO365Services.ps1 -Services AzureAD,ExchangeOnline,Skype -MFA |
After executing the script, connected services will be listed as shown in below screenshot.
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.
1 |
./ConnectO365Services.ps1 –Disconnect |
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!