SharePoint Online: Fix for "The remote server returned an error: (403) Forbidden" in PowerShell

Error: Exception calling "ExecuteQuery" with "0" argument(s): "The remote server returned an error: (403) Forbidden."
sharepoint online powershell the remote server returned an error (403) forbidden

Troubleshooting Checklist:
  1. You may provided the credentials of another tenant which would end up with this error message (Happens when working with multiple tenants!) So make sure the URL and credentials are correct.
  2. You may be missing the SharePoint Online Client Component SDK on your client computer. Make sure you have the latest version installed.
  3. You may be trying to access a site which you don't have access! 
  4. If you have two factor authentication enabled, You need to be in your organization network as per your tenant settings!
  5. The credential you pass, must be of type "SharePointOnlineCredentials". E.g.
    #Load SharePoint CSOM Assemblies
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    $SiteURL = ""
    $UserName = "[email protected]"
    $Password = "Password goes here"
    $SecurePassword= $Password | ConvertTo-SecureString -AsPlainText -Force
    #Setup the Context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
    $Ctx.Credentials = $Cred
    #Get the Site Collection storage used
    $Site = $Ctx.Site
    $StorageUsed = [Math]::Round(($Site.Usage.Storage/1MB),2)
    #Get Site Collection Size
    Write-host "Storage Used: $StorageUsed MB"

1 comment:

  1. Thanks, Installing CSOM SDK resolved my issue.


Please Login and comment to get your questions answered!

Powered by Blogger.