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 SharePoint online list through PowerShell command. The error was coming in the below line while calling the ExecuteQuery() method. The error comes as 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 also you can 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

The below code I was writing to connect to 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 user name and password everything was working fine. The same code also was working in another server.

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

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 below things.

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

You may like following PowerShell SharePoint tutorials:

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

>