I am able to log in to my SharePoint online site using my credential but when I am connecting to the same SharePoint online site using the same credentials using PowerShell CSOM - getting the invalid credential error - tried to connect to the same site using sharegate tool it works but only with powershell it fails.

Connection sample code :

$UserName="[email protected]"
$Password ="MyPassword"

#Setup Credentials to connect
   $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName,(ConvertTo-SecureString $Password -AsPlainText -Force))
#Set up the context

$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteUrl)
 $Context.Credentials = $credentials

Thanks in advance - any help is greatly appreciated.

  • Is multi-factor authentication is enabled for your account? Can please add the complete error message your are getting? Commented Dec 21, 2019 at 4:10
  • Yes - in my account MFA was enabled that was the issue. Commented Dec 23, 2019 at 13:39
  • In that case, you can try creating app password for your account and try using that app password instead of your usual password. Try it and let me know if this works for you. Commented Dec 23, 2019 at 13:41

3 Answers 3


If the account has MFA (Multi Factor Enabled), I mean apart from username and password we need to pass the one time OTP which we will get in our mobile - this is called MFA enabled password. Example: if we run the hybrid configuration onboarding powershell script which is provided by Microsoft that also gets failed incase of MFA enabled password.

To overcome this issue - for time being disable the MFA for your account and after executing the script enable it back. However, this is fine if this is one time execution but if we want to write and execute the PowerShell script on regular basis, then this solution is not recommended. In that scenario we need to create a dedicated account like service account in SharePoint online with administrative access and without MFA enabled, and this account we need to use for any powershell script execution.


If you have multifactor authentication enforced for your user account, you need to create and use an app password instead of your normal password. To create an app password in office 365, visit aka.ms/createapppassword


Make sure you are on VPN if your tenant has MFA enabled

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.