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
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:
- 50+ PowerShell SharePoint Examples
- Delete List View using PowerShell in SharePoint 2013
- Retrieve List Items Created in Last N days using PowerShell SharePoint Online
- net System error 2240 has occurred The user is not allowed to log on from this workstation error in PowerShell SharePoint Online
- SharePoint 2016 PowerShell Script to list all Users in Site Collection
- Steps to Increase SharePoint Online Storage Quota using PowerShell
- SharePoint 2013 backup and restore using PowerShell
- Delete list items created before N days using PowerShell in SharePoint Online
- SharePoint Online Create Workflow History List using PowerShell or using SharePoint Designer 2013
- SharePoint Online check if File exists or not in a document library using PowerShell
- How to create and remove indexed column using PowerShell in SharePoint Online/2013/2016 list or document library?
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.
I am Bijay a Microsoft MVP (8 times – My MVP Profile) in SharePoint and have more than 15 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com