Native alternative to wget in Windows PowerShell?
Solution 1:
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.
Solution 2:
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.
Solution 3:
There is Invoke-WebRequest
in the upcoming PowerShell version 3:
Invoke-WebRequest http://www.google.com/ -OutFile c:\google.html
Solution 4:
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.
Solution 5:
PowerShell V4 One-liner:
(iwr http://blog.stackexchange.com/).Content >index.html`
or
(iwr http://demo.mediacore.tv/files/31266.mp4).Content >video.mp4
This is basically Warren's (awesome) V3 one-liner (thanks for this!) - with just a tiny change in order to make it work in a V4 PowerShell.
Warren's one-liner - which simply uses wget
rather than iwr
- should still work for V3 (At least, I guess; didn't tested it, though). Anyway. But when trying to execute it in a V4 PowerShell (as I tried), you'll see PowerShell failing to resolve wget
as a valid cmdlet/program.
For those interested, that is - as I picked up from Bob's comment in reply to the accepted answer (thanks, man!) - because as of PowerShell V4, wget
and curl
are aliased to Invoke-WebRequest
, set to iwr
by default. Thus, wget
can not be resolved (as well as curl
can not work here).