I am using below code to connect to SPO from powershell, but we have multi factor authentication in SharePoint.

So I want to give credentials from browser instead of script. Please suggest as i dont want to use PnP and Connect-SPOService due to environment restrictions.

    $ctx=New-Object Microsoft.Sharepoint.Client.ClientContext($webUrl)
    $ctx.Credentials=New-Object Microsoft.Sharepoint.Client.SharePointOnlineCredentials($userName,$password)
        return $ctx

2 Answers 2


You should create an app password for your user using these steps. Then you will be able to connect using your username and app password instead of a real password.

  • Isn't there any other way to do that, as probably our client will not permit me to create app password and i have to run this powershell script from a remote machine? After entering the email address in during logging in to SPO site, it asks our domain\userid and password again in a pop up, so i assume this is not a multi-factor authentication. Dec 14, 2018 at 17:11
  • Ah I got it. No it's not MFA Dec 14, 2018 at 17:32
  • 1
    I'm not aware of any other good way to do that when MFA is enabled. The other way is PnP-PS of course, but you can't use it... Dec 14, 2018 at 17:33

I didn't try the app password yet, but this worked for me

$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
$ctx = Get-PnPContext

Your Answer

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

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