Error validating credentials due to invalid username or password using PowerShell

Saida SADIKI 0 Reputation points
2023-07-17T11:53:07.0666667+00:00

I want to authenticate to sharepoint using PowerShell v7

and i'm using this command with attributes URL site and Credentials for Sharepoint

==> Connect-PnPOnline -Url $SiteURL -Credentials $Cred to authenticate sharepoint

but i got this error even my account without MFA

[Error validating credentials due to invalid username or password using PowerShell 7][1]

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Authenticator
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 23,675 Reputation points Microsoft External Staff
    2023-07-18T02:46:18.45+00:00

    Hi @Saida SADIKI,

    Thank you for posting in this community.

    First, I would like to confirm with you about how you created your Credentials.

    Let me give you an example here:

    $SiteURL="https://domain.sharepoint.com/sites" 
    $UserName="******@domain.onmicrosoft.com" 
    $Password = "userpassword"   
    $SecurePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force 
    $Cred = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $SecurePassword  
    try{ 
    #connect to sharepoint online site using powershell 
    Connect-PnPOnline -Url $SiteURL -Credentials $Cred  
    write-host "Connect successfully" -foregroundcolor green 
    }catch{  
    write-host "Error: $($_.Exception.Message)" -foregroundcolor Red  
    }
    

    a image (3)

    Thank you for your time and efforts in advance.


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.