QUESTION :
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 forInvoke-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 forOut-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.
If you just need to retrieve a file, you can use the DownloadFile method of the WebClient object:
$client = New-Object System.Net.WebClient
$client.DownloadFile($url, $path)
Where $url
is a string representing the file’s URL, and $path
is representing the local path the file will be saved to.
Note that $path
must include the file name; it can’t just be a directory.
There is Invoke-WebRequest
in the upcoming PowerShell version 3:
Invoke-WebRequest http://www.google.com/ -OutFile c:google.html
It’s a bit messy but there is this blog post which gives you instructions for downloading files.
Alternatively (and this is one I’d recommend) you can use BITS:
Import-Module BitsTransfer
Start-BitsTransfer -source "http://urlToDownload"
It will show progress and will download the file to the current directory.