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

In this PowerShell SharePoint tutorial, we will discuss how to solve the error “connect-sposervice : the remote server returned an error: (403) forbidden.”

Recently, I was taking a SharePoint corporate training; then we got the 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.”

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 one more system that was working fine, but still the same error.

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

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

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 in 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, we can also install it from NuGet packages.

Conclusion

In this tutorial, I have explained the solution for fixing an error that comes 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:

>