Powershell v3 Invoke-WebRequest HTTPS error
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
Introduction
PowerShell's Invoke-WebRequest often fails with HTTPS errors when connecting to endpoints that use self-signed certificates, outdated TLS protocols, or certificate chains that the system does not trust. The most common error is The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel. The fix depends on whether the issue is the TLS protocol version, the certificate trust chain, or certificate validation itself.
Fix 1: Set TLS 1.2 (Most Common Fix)
PowerShell 3-5 defaults to TLS 1.0/1.1, which most modern servers reject. Force TLS 1.2 before making the request.
For maximum compatibility, enable multiple TLS versions:
In PowerShell 7+, TLS 1.2 is the default, so this fix is only needed for Windows PowerShell 5.1 and earlier.
Fix 2: Bypass Certificate Validation (Development Only)
For development environments with self-signed certificates, you can bypass certificate validation entirely. Never use this in production.
Fix 3: Install the Certificate
The proper fix for self-signed or internal CA certificates is to install them in the trusted root certificate store.
Or use certutil:
After importing, Invoke-WebRequest trusts the server without any code-level workarounds.
Fix 4: Use Invoke-RestMethod for API Calls
Invoke-RestMethod automatically parses JSON responses and has the same TLS behavior. If you are calling REST APIs, it is often more convenient.
Diagnosing the Exact Error
Check which TLS protocols the server supports and what your PowerShell session is using.
The inner exception message usually reveals whether the issue is TLS protocol mismatch, certificate trust failure, or something else.
Common Pitfalls
- Bypassing certificate validation in production scripts — this disables all TLS security and exposes the connection to man-in-the-middle attacks.
- Setting the TLS protocol inside a function but not at script scope —
ServicePointManagersettings apply process-wide but must be set before the first HTTPS connection. - Forgetting that PowerShell 7 uses a different HTTP stack (HttpClient) than Windows PowerShell 5.1 (WebRequest) — the
-SkipCertificateCheckparameter only exists in PowerShell 7+. - Not checking if a proxy server is intercepting HTTPS traffic — corporate proxies often inject their own certificates, requiring the proxy CA to be trusted.
- Assuming TLS 1.3 is available on older Windows versions — TLS 1.3 requires Windows 10 version 1903 or later.
Summary
- Set
[Net.ServicePointManager]::SecurityProtocolto TLS 1.2 for PowerShell 5.1 and earlier. - Use
-SkipCertificateCheckin PowerShell 7+ for development with self-signed certificates. - Install internal CA certificates in the trusted root store for the proper production fix.
- Check the inner exception message to diagnose whether the issue is TLS version, certificate trust, or proxy interference.
- Never bypass certificate validation in production environments.
Related reading
- Preferred Java way to ping an HTTP URL for availability
- Pretty print JSON output of Spring Boot Actuator endpoints
- Problems using Maven and SSL behind proxy
- Produce balanced mini batch with Dataset API
- pq could not resize shared memory segment. No space left on device
- Predefined type 'System.Object' is not defined or imported .net 4.6
- Producer throughput with varying acks=0,1,-1
- Programmatically make Amazon purchase?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.