Saturday, 26 January 2019

Native alternative to wget in Windows PowerShell?


I know I can download and install the aformentioned library (wget for Windows), but my question is this:


In Windows PowerShell, is there a native alternative to wget?


I need wget simply to retrieve a file from a given URL with HTTP GET. For instance:


wget http://www.google.com/

Answer



Here's a simple PS 3.0 and later one-liner that works and doesn't involve much PS barf:


wget http://blog.stackexchange.com/ -OutFile out.html

Note that:



  • wget is an alias for Invoke-WebRequest

  • Invoke-WebRequest returns a HtmlWebResponseObject, which contains a lot of useful HTML parsing properties such as Links, Images, Forms, InputFields, etc., but in this case we're just using the raw Content

  • The file contents are stored in memory before writing to disk, making this approach unsuitable for downloading large files


  • On Windows Server Core installations, you'll need to write this as


    wget http://blog.stackexchange.com/ -UseBasicParsing -OutFile out.html


  • Prior to Sep 20 2014, I suggested


    (wget http://blog.stackexchange.com/).Content >out.html

    as an answer.  However, this doesn't work in all cases, as the > operator (which is an alias for Out-File) converts the input to Unicode.




If you are using Windows 7, you will need to install version 4 or newer of the Windows Management Framework.


You may find that doing a $ProgressPreference = "silentlyContinue" before Invoke-WebRequest will significantly improve download speed with large files; this variable controls whether the progress UI is rendered.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...