I have an authentication script below which is deemed as legacy and will soon be blocked by Microsoft.

How can I convert this script to use modern auth methods?

$site = https://mysite.sharepoint.com
$listName = "Events"
$user = [email protected]
$pwd = ConvertTo-SecureString "XXXXXXXXXXX" -AsPlainText -Force
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($site)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($user,$pwd)
$ctx.Credentials = $credentials
$siteColl = $ctx.Web

3 Answers 3


You could use account with MFA to connect to SharePoint Online:

 #Add required references to OfficeDevPnP.Core and SharePoint client assembly
[System.Reflection.Assembly]::LoadFrom("C:\Program Files\WindowsPowerShell\Modules\SharePointPnPPowerShellOnline\3.29.2101.0\OfficeDevPnP.Core.dll") 
$siteURL = "https://contoso.sharepoint.com/sites/siten_name"
$AuthenticationManager = new-object OfficeDevPnP.Core.AuthenticationManager
$ctx = $AuthenticationManager.GetWebLoginClientContext($siteURL)
Write-Host "Title: " $ctx.Web.Title -ForegroundColor Green
Write-Host "Description: " $ctx.Web.Description -ForegroundColor Green

For more information, you could refer to; https://social.technet.microsoft.com/wiki/contents/articles/53244.sharepoint-connect-to-site-with-mfa-enabled-using-csom-and-pnp-powershell.aspx

  • This looks good but would require the user to sign in with MFA right? This script will run on task scheduler and needs to be fully automated.
    – BennKingy
    Jan 13, 2022 at 12:06

The only way to bypass MFA requirement is to use app only access. I have multiple scripts that run unattended using this approach on Azure. You can find more information on this Microsoft link:



https://morgantechspace.com/2021/09/connect-to-sharepoint-site-with-mfa-account-using-csom-and-powershell.html < Theres a guide here that should work without having to consent to MFA by the user :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.