Connecting PowerShell to Office 365 The Right Way
In my note about automating Office 365 with PowerShell and Flow, I criticized Microsoft for publishing too many PowerShell modules, which makes it messy if you want to manage many different parts of Office 365 in a single PowerShell instance (window).
The inevitable question then arises about how to configure PowerShell to work best with Office 365. Or put another way, how to build the best script to connect the various bits of Office 365 as efficiently as possible using a command window or the PowerShell ISE (or however else you choose to use PowerShell). In response, I think two approaches exist:
- Build your own script from scratch and include the commands to connect to the different parts of Office 365 that you want to work with. This Microsoft post includes most of the commands you’ll need to use. However, you still need to do some work to build and test the script to make sure it is fit for purpose, including using multi-factor authentication to connect to services that support MFA, like Exchange Online.
- Pick up one of the many example scripts that exist and modify it for your own purposes.
PowerShell Connection Scripts
Normally, I tell people looking for a good connection script for Office 365 to head over to MVP Michel de Rooij’s site and learn about his Connect-Office365Services.ps1 script. Michel has developed and enhanced the script over the years to track developments in the service and it’s an excellent start for for anyone who wants to work with Office 365.
Yesterday, MVP Chris Goosen posted an update to his Connect-365.ps1 script. Michel influenced this script too and the nice thing about it is that it includes a GUI to allow you to choose what Office 365 services to connect to and whether to use MFA. This is a nice touch.
If You Don’t Like a Script, Change it
In both cases, the scripts are available from the TechNet Gallery or GitHub and because the code in the scripts is relatively straightforward PowerShell, you can change how the scripts work to reflect your view on how best to connect.
The Office 365 for IT Pros eBook includes over 1,000 examples of how to use PowerShell to work with different parts of Office 365. Before you can use those examples, you need to connect – so that’s why you need a script.