PowerShell: force TLS 1.2 ("unexpected error occurred on a send")
Check and force TLS 1.2 in PowerShell and .NET, verify SCHANNEL/WinHTTP registry settings, and skip certificate checks for tests.
On older Windows systems, Invoke-WebRequest and similar cmdlets can fail with "The underlying connection was closed: An unexpected error occurred on a send" because PowerShell does not negotiate TLS 1.2 by default. This page shows how to check and force the protocol, make TLS 1.2 the default, and, as a last resort, skip certificate validation.
List the supported protocols
# Get the BaseType of Net.SecurityProtocolType
PS> [Net.SecurityProtocolType]
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True SecurityProtocolType System.Enum
# Get the PowerShell supported TLS versions
PS> [enum]::GetNames([Net.SecurityProtocolType])
SystemDefault
Ssl3
Tls
Tls11
Tls12
Show the protocol currently in use:
[Net.ServicePointManager]::SecurityProtocol
Quick fix for the current session
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri https://example.com/
Other protocols can be forced the same way:
# Force PowerShell to use TLS 1.1
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls11
# Force PowerShell to use TLS 1.0
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls
TLS 1.3 cannot be set this way on these versions: the Tls13 value does not exist in the enumeration and the assignment fails with an error listing the valid values (SystemDefault, Ssl3, Tls, Tls11, Tls12).
Make TLS 1.2 the default for .NET applications
Setting SchUseStrongCrypto makes .NET Framework 4.x applications use strong protocols by default. Import this .reg content (the second key covers 32-bit applications on a 64-bit system):
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\.NETFramework\v4.0.30319]
"SchUseStrongCrypto"=dword:00000001
The IIS Crypto tool can also enable and disable protocols and cipher suites on Windows servers. Windows Management Framework 5.1 can be installed from the Microsoft download page on systems that need a newer PowerShell.
Check that TLS 1.2 is enabled
In the SCHANNEL registry:
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client\DisabledByDefaultmust be present with the value0.HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\SecurityProviders\SCHANNEL\Protocols\TLS 1.2\Client\Enabledmust be present with the value1.
On Windows Server 2008 R2, Windows Server 2012 and Windows 7, also check that TLS 1.2 is the default secure protocol in WinHTTP:
- The Microsoft update KB3140245 is installed.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp\DefaultSecureProtocolscontains0x00000A00or0x00000800.- On a 64-bit machine, check the same value under
HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Internet Settings\WinHttp\DefaultSecureProtocols.
Skip certificate validation
When a remote endpoint uses a certificate that cannot be validated, run this fragment before the remote calls:
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy {
public bool CheckValidationResult(
ServicePoint srvPoint, X509Certificate certificate,
WebRequest request, int certificateProblem) {
return true;
}
}
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
Notes
- Trusting all certificates disables a security control: use it only for tests, never in production scripts.
- Further reading: Force PowerShell to use TLS 1.2 and How to enable TLS 1.2 as the default security protocol on Windows servers.