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

Install-Module -Name SharePointPnPPowerShellOnline

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
#Please make sure the path of following files
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.28.1807.0\OfficeDevPnP.Core.dll”
  • 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.
#get the site of which we need to load the properties
$siteURL = "https://knowledgejunction1.sharepoint.com/"

#Create the client context object
$authManager = new-object OfficeDevPnP.Core.AuthenticationManager; 

$ctx = $authManager.GetWebLoginClientContext($SiteURL);
  • 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
$web = $ctx.Web;
$ctx.Load($web);
$ctx.ExecuteQuery();

Complete PowerShell:

#Please make sure the path of following files
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll”
Add-Type -Path “C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll”
Add-Type -Path “C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\2.28.1807.0\OfficeDevPnP.Core.dll”

#get the site of which we need to load the properties
$siteURL = "https://knowledgejunction1.sharepoint.com/"

#Create the client context object
$authManager = new-object OfficeDevPnP.Core.AuthenticationManager; 

$ctx = $authManager.GetWebLoginClientContext($SiteURL);# 

$web = $ctx.Web;
$ctx.Load($web);
$ctx.ExecuteQuery();

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 🙂

Prasham Sabadra

LIFE IS VERY BEAUTIFUL :) ENJOY THE WHOLE JOURNEY :) Founder of Knowledge Junction and live-beautiful-life.com, Author, Learner, Passionate Techie, avid reader. Certified Professional Workshop Facilitator / Public Speaker. Scrum Foundation Professional certificated. Motivational, Behavioral , Technical speaker. Speaks in various events including SharePoint Saturdays, Boot camps, Collages / Schools, local chapter. Can reach me for Microsoft 365, Azure, DevOps, SharePoint, Teams, Power Platform, JavaScript.

You may also like...

Leave a Reply

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

%d bloggers like this: