I use this code to connect to the SharePoint online environment:

$username = "xxxxxxxx" 
$password = "xxxxxxxx"
$credential = New-Object System.Management.Automation.PSCredentials ($username, $password)
connect-pnponline -url xxxxxxxxxxxxxxx -Credential $credential

But I get the error: The remote server returned an error (403) Forbidden

How can I solve this?

Gr, P


PSCredential needs a SecureString rather than a plain string.

$encpassword = convertto-securestring -String $password -AsPlainText -Force
$credential = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $encpassword

connect-pnponline -url xxxxxxxxxxxxxxx -Credential $credential
  • Still receive the message: "the remote server returned an error (403) forbidden – Peter Kiers Jan 6 at 16:53
  • Got it :) the url wasn't correct. Thanks for the help. – Peter Kiers Jan 6 at 17:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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