PowerShell – Easy & secure connection to Office 365

Introduction

Are you tired of remembering all those cmdlets to connect to Exchange Online, SharePoint Online. So many different cmdlets for the different services. With this PowerShell module you don’t need to remember anything (not even your password!)

I published my first PowerShell module to the PowerShell Gallery: https://www.powershellgallery.com/packages/Office365.Connect/. The module contains 2 cmdlets that you can use to connect and disconnect from Office 365 PowerShell. It leverages the Windows Credential Manager to securely connect to any Office 365 service.
It also supports using Multi-factor authentication (MFA). Read more about this below.
In the image below you can see I can connect to these services, and it already specified an account to use. The script I ran to use this

Connect-Office365 -Tenant "sharepointrelated"

The script checks the Windows Credential Manager to see if there’s an entry named “sharepointrelated”. If there is, it uses the credentials entered there to connect to the tenant.

Windows Credential Manager
To ensure secure use of credentials, we leverage the Windows Credential Manager (You can find this in Control Panel). All you need to do is add any tenant you want to manage as a “Windows generic credential“.

Here’s an example that I have setup to demonstrate it:

Other modules
If you don’t have the required modules to connect to any of the Office 365 services using PowerShell, the Connect-Office365 cmdlet will provide an option to automatically install the required modules for you.

Installation

As any other PowerShell module published in the PowerShell Gallery, you can install the module using the following cmdlet. Make sure you run PowerShell as administrator:

Install-Module Office365.Connect

Use Guide – PowerShell

After you installed the module from the PowerShell gallery, you can start using cmdlets Connect-Office365 and Disconnect-Office365.

Here are some basic examples that you can use.

Connect Office 365 without MFA

Connect-Office365 -Tenant "Contoso"

This allows you to connect to Office 365 services using PowerShell, without the use of MFA.

Connect Office 365Using with MFA
If the tenant you want to manage uses MFA for your account (you always should for administrator accounts), you will need to specify this while connecting. For instance:

Connect-Office365 -Tenant "Fabrikam" -MFA

Disconnect all Office 365 services
If you are done managing Office 365, you can disconnect all sessions by using:

Disconnect-Office365

This will disconnect all services.

Disconnect specific Office 365 services
If you want to disconnect specific services, you can do this by doing:

Disconnect-Office365 -Teams -SharePoint

The above cmdlet will disconnect from Microsoft Teams and SharePoint Online, but any other services that you might be connected to will remain connected.

Credits

A big thanks to Ryan Yates for assisting me on my first Github project and helping me publish this to the PowerShell Gallery.
Also a thank you to my colleague Staffan Nelemans for helping me troubleshoot some issues I had while converting my initial PowerShell script to a module.