Search code examples
powershelldefaultfile-locationinvoke-webrequest

Invoke-WebRequest without OutFile?


I used Invoke-WebRequest in Powershell to download a file without using the -OutFile parameter, and, from the documentation here, the file should've ended up in the directory I was in. However, there is nothing. Response was OK, no error was shown.

What could've happened to that file? Am I mistaken about how Invoke-WebRequest should work without an Out parameter?

Thanks!

Note: I know I can easily download the file using the parameter, but it's pretty big and I'd like to make sure it doesn't end up clogging disk space somewhere I don't need


Solution

  • From the linked docs:

    By default, Invoke-WebRequest returns the results to the pipeline.

    That is, in the absence of -OutFile no file is created.
    (If you don't capture or redirect the output, it will print to the host (console).)

    As techguy1029 notes in a comment on the question, the current directory only comes into play if you do use
    -OutFile but specify a mere file name rather than a path.

    Additionally:

    • In v7.4+ of PowerShell (Core), you may now specify just a directory path, in which case the the last segment of the (possibly redirected, ultimate) target URL is implicitly used as the file name.
      As of this writing (v7.4.1), there are two problems with this feature.

      • Using the -Verbose switch currently doesn't reveal the file name used (whose name won't be obvious, if URL redirections were involved), only the download directory: GitHub issue #21081

      • Similarly it would be helpful to be able to determine the downloaded file's full path programmatically, via the response object (in combination with -PassThru), which is the subject of the feature request in GitHub issue #21082

    • A planned future enhancement (which will probably land in v7.5 at the earliest) is to use the server-suggested file name rather than the last URL segment, namely via a Content-Disposition field in the response header.
      However, work on the relevant PR, GitHub PR #19385, seems to have stalled as of this writing.


    As an aside:

    • To-pipeline output is a response object of WebResponseObject-derived class, whereas only the value of the response's body (the equivalent of property value .Content) is saved with -OutFile.

    • Using the -PassThru switch in combination with -OutFile causes the response object to be emitted as well (by default, a download produces no direct output).