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


- 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:
- Office 365 : Connecting to SharePoint online site using CSOM when Multi-Factor Authentication (MFA) is enabled for the use
- Add-Type
- AuthenticationManager Class
- PnP PowerShell overview
- Install-Module
Thanks for reading 🙂 Stay tuned, I am going to share all my PowerShell scripts here 🙂
Enjoy the LIFE journey 🙂
You must be logged in to post a comment.