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 Authenticator
Microsoft Authenticator
A Microsoft app for iOS and Android devices that enables authentication with two-factor verification, phone sign-in, and code generation.
5,787 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
9,984 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,426 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Ling Zhou_MSFT 14,450 Reputation points Microsoft Vendor
    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="user@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.