The underlying connection was closed: An unexpected error occurred on a send

PowerShell error – The underlying connection was closed: An unexpected error occurred on a send, while trying to run CSOM PowerShell code on SharePoint online sites.

The underlying connection was closed. PowerShell connection error

Same error while trying to use PnP cmdltes

The underlying connection was closed. PnP connection error

From network trace we are able to figure out that server is closing the TCP connection forcefully.

TCP error

Reason being a mismatch in security protocol. Microsoft announced that they will be moving to TLS 1.2 for encryption on October 31, 2018 for Office 365. Here is a Microsoft support article Preparing for the mandatory use of TLS 1.2 in Office 365 explaining this.


Update your default security protocol to be TLS 1.2+. As an immediate fix you can add

[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;

this to the top of your script to force the PowerShell session to use TLS 1.2 protocol. Remember that this holds good only for the current session. If you close and reopen a new PowerShell it will default to old protocol. You need to add this line of code for every session to get over this problem.TLS 1.2

Your PowerShell CSOM should start working again!

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

SharePoint Navigation switcher

SharePoint Navigation has received lot of attention in the past couple of year when compared to how users had to deal with navigation in SharePoint 2010 & 2013 On-prem days. Here is the latest of the improvements, SharePoint navigation switcher. This feature was made Generally available in Dec 2021. By the time of this writing […]

Read More

Disable feedback surveys in Power Platform

If you have worked in Microsoft 365 PowerApps or Power Automate, you might be familiar with feedback survey which pops up quite often. Microsoft wants your feedback on how likely are your to recommend Power Automate to a friend or colleague? Well, I ran into a conversation in twitter about this survey. Apparently I learnt […]

Read More

Error handling in Azure PowerShell Runbooks

Error handling in Azure PowerShell Runbooks should be handled carefully if not the runbook with continue to Completed state even if there is an Error in the script. Unlike exception, Runbook execution will continue after  an Error  by default. Windows PowerShell has multiple streams of output. Runbooks in Azure work differently in each of these […]

Read More