Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 317
DMX David Cardinal Avatar asked Dec 22 '22 21:12

DMX David Cardinal


2 Answers

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.

like image 154
mklement0 Avatar answered Jan 18 '23 14:01

mklement0


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"
like image 33
ArcSet Avatar answered Jan 18 '23 15:01

ArcSet