Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server error in PowerShell SharePoint Online

Recently, I was trying to insert items to the SharePoint online list through the PowerShell command. The error came in the below line while calling the ExecuteQuery() method. The error comes as an Exception calling “ExecuteQuery” with “0” argument(s): “Unable to connect to the remote server”

$Context.ExecuteQuery().

Exception calling “ExecuteQuery” with “0” argument(s): “Unable to connect to the remote server”

You can write in Windows PowerShell ISE, or you can also use Visual Studio Code to write and debug PowerShell Script.

Exception calling ExecuteQuery with 0 argument(s) Unable to connect to the remote server
Exception calling ExecuteQuery with 0 argument(s) Unable to connect to the remote server

Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server

I was writing the code below to connect to the SharePoint Online site from PowerShell.

$SiteURL = "https://onlysharepoint2013.sharepoint.com/sites/Bhawana/"
$Context = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
$securePassword=ConvertTo-SecureString "MyPassword" -AsPlainText -Force
$Context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials("bijay@XXXXX.onmicrosoft.com", $securePassword)

The username and password everything was working fine. The same code was also working on another server.

Then we saw the internet was not working on the server. We tried to open the site in a browser, but it did not open either. Once the internet was up, the code worked as expected.

Check out Create and Delete SharePoint Document Library Using PowerShell

ExecuteQuery” With “0” Argument(S): “The Remote Server Returned An Error: (403) Forbidden

You might also get an error like ExecuteQuery” With “0” Argument(S): “The Remote Server Returned An Error: (403) Forbidden while working with PowerShell SharePoint Online Office 365.

You can solve by adding Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll explicitly in the PowerShell script.

Import-Module “E:\Bijay\Microsoft.SharePoint.Client.dll”
Import-Module “E:\Bijay\Microsoft.SharePoint.Client.Runtime.dll”

Sometimes, even if you install the SharePoint Online management shell, you might get the above issue.

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

If you are still Exception calling “executequery” with “0” argument(s): the remote server returned an error: (403) forbidden error, you can try the below things.

  • Open PowerShell ISE in administrator mode
  • The user account should be an admin account.

I hope this will help to resolve the error “Exception calling ExecuteQuery with 0 argument(s): Unable to connect to the remote server error in PowerShell SharePoint.”

You may also like the following PowerShell 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