Connect-SPOService : The Remote Server Returned An Error: (403) Forbidden.

Are you getting the error: connect-sposervice : the remote server returned an error: (403) forbidden in PowerShell SharePoint? I will show you here how to fix this issue.

I recently worked on a SharePoint project where I encountered an issue while working with the SharePoint Online site using PowerShell. The error says:

Exception calling “executequery” with “0” argument(s): “the remote server returned an error: (403) forbidden.”

connect-sposervice : the remote server returned an error: (403) forbidden

The remote server returned an error (403) forbidden, PowerShell

Here, we tried retrieving the web title using PowerShell with CSOM (using client dlls to connect to SharePoint Online sites).

Below is the code we were writing to retrieve the web title using PowerShell SharePoint Online.

Add-Type -Path "c:\Bijay\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "c:\Bijay\Microsoft.SharePoint.Client.Runtime.dll"   

$siteUrl = "https://<tenantname>.sharepoint.com/sites/TSInfoIntranet" 
$username = "bijay@<tenantname>.onmicrosoft.com" 
$password = Read-Host -Prompt "Enter password" -AsSecureString  

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)  
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password)  
$ctx.Credentials = $credentials 

$rootWeb = $ctx.Web  
$ctx.Load($rootWeb) 
$ctx.ExecuteQuery()

Write-Host $rootWeb.Title  

But when I ran the PowerShell script using Windows PowerShell ISE, it gave an error as “Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.”

From the error, it looks like, there was an error in the login details. I verified the Site URL, login name, and password, and everything was perfect.

Also, copy the SharePoint client dlls from another system that was working fine, but still the same error.

The solution to the error is to install the SharePoint Online client component SDK.

Download the SharePoint Online client component SDK and install it. Then restart PowerShell, and you can refer to it directly from the URL below.

Add-Type -Path "E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"  
Add-Type -Path "E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

Here, I have installed it on the E drive in my local system.

After you install the SharePoint Online SDK, it will create a structure like the one below and contain the required dlls.

E:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\

After this, the error “Exception calling “ExecuteQuery” with “0” argument(s): The remote server returned an error: (403) Forbidden.” will not come.

The other way is to install it from NuGet packages.

Conclusion

In this tutorial, I have explained the solution for fixing an error like the one below.

  • connect-sposervice : the remote server returned an error: (403) forbidden.
  • exception calling “executequery” with “0” argument(s): “the remote server returned an error: (403) forbidden.”
  • connect-pnponline : the remote server returned an error: (403) forbidden.
  • the remote server returned an error (403) forbidden powershell

You may also like the following tutorials:

Leave a Comment

Power Apps functions free pdf

30 Power Apps Functions

This free guide walks you through the 30 most-used Power Apps functions with real business examples, exact syntax, and results you can see.

Download User registration canvas app

DOWNLOAD USER REGISTRATION POWER APPS CANVAS APP

Download a fully functional Power Apps Canvas App (with Power Automate): User Registration App