Invoke-WebRequest’s TimeoutSec parameter defaults to 100 seconds

According to TechNet, when the TimeoutSec parameter of Invoke-WebRequest is ommitted or specified as 0, the timeout is indefinite. There appears to be a bug that causes this value to default to 100 seconds, at least in PowerShell 4. I presume this is because the default value of the underlying System.Net.HttpWebRequest.Timeout property is 100,000 ms.

The workaround is to specify a large value for TimeoutSec, e.g.

Invoke-WebRequest -Uri $Uri -TimeoutSec ([int]::MaxValue)

That is, assuming that you want the request to return within 68 years.

Edit (07/03/2024)

In PowerShell 7, the maximum time-out is now 24.20:31:23.6470000, so the command with the maximum time-out is now:

Invoke-WebRequest -Uri $Uri -TimeoutSec ([timespan]::Parse("24.20:31:23").TotalSeconds)

2 thoughts on “Invoke-WebRequest’s TimeoutSec parameter defaults to 100 seconds”

  1. You need to specify $([int]::MaxValue) instead of [int]::MaxValue. Otherwise Powershell will complain about the parameter not being of type Int32

    Like

Leave a reply to Gary Pendlebury Cancel reply