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"

Office 365 Connect

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.

This blog post describes the following:

  • Prerequisites
  • Installation
  • Use Guide
  • Contributing
  • Credits

Prerequisites

PowerShell gallery
Since the PowerShell module is in the PowerShell gallery, you only need a supported operating system. For more information on the OS requirements, click here: https://docs.microsoft.com/en-us/powershell/gallery/overview

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:

Credential manager

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.

Contributing

The project is hosted on Github: https://github.com/martensnico/Office365.Connect, and anyone is welcome to contribute. Please note that this is my first Github / PowerShell Gallery project, so if I don’t respond quickly, make sure to contact me on Twitter for a faster response.

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.

I’m using an external module for working with the Windows Credential Manager, which can be found here: https://www.powershellgallery.com/packages/CredentialManager/2.0

It has been a great learning project, and I hope I can contribute more to the Open Source community!

2 thoughts on “PowerShell – Easy & secure connection to Office 365

Leave a Reply

Your email address will not be published. Required fields are marked *