You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Invoke-webrequest unable to download large files - stream too long #15438
Probably just a typo for gigabyte, but what PowerShell version are you on, IIRC older ones suffered from bad performance due to inefficiencies with the progress stream but that should be less of a problem today.
This sounds like more of an issue with the underlying implementation, it's hitting the built-in limits of whatever type of Stream it's using.
It might be necessary to download files to a temp location so we can chunk the download data as it's received and avoid storing the entirety of a very large file into a single Stream object.
jdescelliers (@jdescelliers) What version of PowerShell do you observe this issue? I believe the large file download issues was fixed by Ilya (@iSazonov)'s PR #11095, which replaced MemoryStream with async streams. The fix was available in 7.1. Can you try on 7.1 and see if you observe the same?
You need to specify -OutFile to store the data into a file and bypass this problem. The default behaviour is to store the response in memory and that has limits as you've encountered.
Yes. It works when using -OutFile, there are still multiple issues that may need to be treated separately:
High memory usage: on a 2nd attempt, download peaked around 30MB/s and memory usage was steady at 96MB so more than 3 times the needed RAM: is it related to how .NET Core allocates memory in large chunks?
that memory is not released back to the OS when the download is complete,
I didn't see any mention in the documentation of Invoke-Webrequest that the response body would be stored in memory unless ``-OutFile` is provided,
shouldn't -Outfile be able to be "smarter" by using the filename that may be included in the response Content-Disposition: attachment header?
The original question was answered. For discussion on further improvements, please search for existing related issues or create new issue if no related issue is found.
This is still an issue in 7.4.5.
If Invoke-WebRequest is part of a function, the downloaded content is still going into the memory before being outputed into a file.
Example is with an alias of how Linux wget should act on Windows:
If you try to pull with wget <somefile>, it will pull it first into the memory, before saving it as a file. Thus, you will eventually run with bigger files into Stream was too long.
This is still an issue in 7.4.5. If Invoke-WebRequest is part of a function, the downloaded content is still going into the memory before being outputed into a file. Example is with an alias of how Linux wget should act on Windows:
If you try to pull with wget <somefile>, it will pull it first into the memory, before saving it as a file. Thus, you will eventually run with bigger files into Stream was too long.
I just ran into this when fetching a 2GB file. My machine has plenty of free memory, much larger than 2GB, yet wget failed due to "stream was too long". This is very strange.
Activity
iSazonov commentedon May 22, 2021
What is GN?
jborean93 commentedon May 23, 2021
Probably just a typo for gigabyte, but what PowerShell version are you on, IIRC older ones suffered from bad performance due to inefficiencies with the progress stream but that should be less of a problem today.
vexx32 commentedon May 23, 2021
This sounds like more of an issue with the underlying implementation, it's hitting the built-in limits of whatever type of Stream it's using.
It might be necessary to download files to a temp location so we can chunk the download data as it's received and avoid storing the entirety of a very large file into a single Stream object.
daxian-dbw commentedon May 25, 2021
jdescelliers (@jdescelliers) What version of PowerShell do you observe this issue? I believe the large file download issues was fixed by Ilya (@iSazonov)'s PR #11095, which replaced
MemoryStreamwith async streams. The fix was available in 7.1. Can you try on 7.1 and see if you observe the same?rjmholt commentedon May 25, 2021
jdescelliers (@jdescelliers) it would be useful to know:
$PSVersionTablewill tell you this)Get-Errorwill tell you that in PS 7)I'm currently testing this out with PS 7.2-preview.5 on my Windows 10 machine by downloading a 10 GB file and will report back on what happens.
rjmholt commentedon May 26, 2021
So after running this yesterday,
iwrran for a few hours. I didn't compare it to, say, a browser, so I'm not sure if it was the site or the cmdlet.But it didn't throw for me.
iSazonov commentedon May 26, 2021
jdescelliers (@jdescelliers) Please share $PSVersionTable.
rjmholt commentedon May 26, 2021
And the output of
Get-Errorif there is an errorjdescelliers commentedon Jun 2, 2021
So a bit of context:
%PSVersionTableoutput:Invoke-WebRequest: Stream was too long.Exception : Type : System.IO.IOException TargetSite : Name : Write DeclaringType : System.IO.MemoryStream MemberType : Method Module : System.Private.CoreLib.dll StackTrace : at System.IO.MemoryStream.Write(Byte[] buffer, Int32 offset, Int32 count) at Microsoft.PowerShell.Commands.WebResponseContentMemoryStream.Initialize() at Microsoft.PowerShell.Commands.WebResponseContentMemoryStream.ToArray() at Microsoft.PowerShell.Commands.WebResponseObject.InitializeContent() at Microsoft.PowerShell.Commands.WebResponseObjectFactory.GetResponseObject(HttpResponseMessage response, Stream responseStream, ExecutionContext executionContext) at Microsoft.PowerShell.Commands.InvokeWebRequestCommand.ProcessResponse(HttpResponseMessage response) at Microsoft.PowerShell.Commands.WebRequestPSCmdlet.ProcessRecord() at System.Management.Automation.CommandProcessor.ProcessRecord() Message : Stream was too long. Source : System.Private.CoreLib HResult : -2146232800 CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], IOException FullyQualifiedErrorId : System.IO.IOException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand InvocationInfo : MyCommand : Invoke-WebRequest ScriptLineNumber : 1 OffsetInLine : 1 HistoryId : 2 Line : Invoke-WebRequest -Uri https://releases.ubuntu.com/20.04.2.0/ubuntu-20.04.2.0-desktop-amd64.iso -Method Get PositionMessage : At line:1 char:1 + Invoke-WebRequest -Uri https://releases.ubuntu.com/20.04.2.0/ubuntu-2 … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ InvocationName : Invoke-WebRequest CommandOrigin : Internal ScriptStackTrace : at <ScriptBlock>, <No file>: line 1pwsh.exewhen the exeption was raised: 2.1GB which isn't released as of writing this comment.On the same URL,
wget1.20.3 uses roughtly 30MB of RAM for 30MB/s download speed.jborean93 commentedon Jun 2, 2021
You need to specify
-OutFileto store the data into a file and bypass this problem. The default behaviour is to store the response in memory and that has limits as you've encountered.jdescelliers commentedon Jun 5, 2021
Yes. It works when using
-OutFile, there are still multiple issues that may need to be treated separately:Invoke-Webrequestthat the response body would be stored in memory unless ``-OutFile` is provided,-Outfilebe able to be "smarter" by using thefilenamethat may be included in the responseContent-Disposition: attachmentheader?iSazonov commentedon Jun 6, 2021
Perhaps we already have such issue.
No need explicitly document this because it is how pipeline works - we should accumulate data before sending it to pipeline.
I guess .Net Core GC is too lazy.
Maybe we should see if we can do better.
daxian-dbw commentedon Jan 10, 2022
The original question was answered. For discussion on further improvements, please search for existing related issues or create new issue if no related issue is found.
orcinus commentedon May 2, 2023
This is still a problem in 2023.
PowerShell is a ridiculous joke at this point.
xantari commentedon Dec 26, 2023
-OutFile still doesn't respect the content-disposition file name :(
ku4eto commentedon Sep 9, 2024
This is still an issue in 7.4.5.
If
Invoke-WebRequestis part of a function, the downloaded content is still going into the memory before being outputed into a file.Example is with an alias of how Linux
wgetshould act on Windows:If you try to pull with
wget <somefile>, it will pull it first into the memory, before saving it as a file. Thus, you will eventually run with bigger files intoStream was too long.rwalle commentedon Jan 13, 2026
I just ran into this when fetching a 2GB file. My machine has plenty of free memory, much larger than 2GB, yet wget failed due to "stream was too long". This is very strange.