8

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

DMX David Cardinal
  • 599
  • 2
  • 7
  • 19
  • 3
    If you dont specify a **PATH**, the response will be saved in the current location. If you omit the `-OutFile` parameter completely, it won't be saved on a file at all – techguy1029 Sep 12 '19 at 17:52

2 Answers2

10

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, the current directory only comes into play if you do use
-OutFile but specify a mere file name rather than a path.


As an aside: To-pipeline output is a response object of (a) type (derived from) WebResponseObject, whereas only the value of the response's body (the equivalent of property value .Content) is saved with -OutFile.

mklement0
  • 382,024
  • 64
  • 607
  • 775
5

Lets talk about what the Microsoft documentation says for Invoke-WebRequest

" -OutFile : Specifies the output file for which this cmdlet saves the response body. Enter a path and file name. If you omit the path, the default is the current location. "

The Key word here is if a Path is omitted it will use the current path.

the -OutFile is a parameter of type String

The usage to save to current path would be

Invoke-webrequest "http://Test.com/test.pdf" -OutFile "Test.pdf"

else to have a custom path

Invoke-webrequest "http://Test.com/test.pdf" -OutFile "C:\Test\Test.Pdf"
ArcSet
  • 6,518
  • 1
  • 20
  • 34