M365 – SharePoint online – Connecting to SharePoint online site using PowerShell when Multi-Factor Authentication (MFA) is enabled for the user

Hi All,

LIFE IS BEAUTIFUL 🙂 Today with PowerShell 🙂

Today I am sharing one small PowerShell script but very useful.

On 24 Dec 2017 I had written the article “Office 365 : Connecting to SharePoint online site using CSOM when Multi-Factor Authentication (MFA) is enabled for the user“. And till today this article is in my blogs Top Posts 🙂

In the above article we discussed console application written using CSOM (Client Side Object Model) to connect to my SharePoint online site when Multi Factor Authentication (MFA) is enabled.

In this article we will discuss the same but rather using CSOM we will use the PowerShell script.

Prerequisites: Since we are using GetWebLoginClientContext() method from AuthenticationManager class. This class is available in OfficeDevPnP.Core.dll assembly. If this assembly is not installed on local computer, we need to download and install the same.

We can install the latest version of this assembly using “Install-Module” cmdlet as

All other existing versions are available on – https://www.powershellgallery.com/packages/SharePointPnPPowerShellOnline

Lets discuss the steps:

Steps:

  • Load the respective assemblies. Here I am using Add-Type. There is very good detailed documentation on Add-Type from Microsoft included in reference section 🙂 as
  • We need to make sure that path specified for above assemblies are correct.
  • Create new object of AuthenticationManager class and using GetWebLoginClientContext() method to get the credentials.
  • On execution of this method prompt appears for User Name and Password as
M365 - Prompt for user name for connecting to SharePoint site / Office 365
Fig1: M365 – Prompt for user name for connecting to SharePoint site / Office 365
M365 - Prompt for password for connecting to SharePoint site / Office 365
Fig2: M365 – Prompt for password for connecting to SharePoint site / Office 365
  • Once UserName and Password is validated, next prompt will be for the code based on the method we choose for MFA like code on phone or code from the Authenticatior App
  • Once we entered the code, we will be connected to Office 365 or our SharePoint online site

Complete PowerShell:

Reference:

  1. Office 365 : Connecting to SharePoint online site using CSOM when Multi-Factor Authentication (MFA) is enabled for the use
  2. Add-Type
  3. AuthenticationManager Class
  4. PnP PowerShell overview
  5. Install-Module

Thanks for reading 🙂 Stay tuned, I am going to share all my PowerShell scripts here 🙂

Enjoy the LIFE journey 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.