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).
Related videos on Youtube
jsalonen
Updated on September 18, 2022Comments
-
jsalonen over 1 year
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/
-
Admin almost 9 yearsSee also superuser.com/questions/25538/…
-
-
Richard over 12 yearsBITS relies on support at the server end, if available this works in the background and you can get progress updates with other cmdlets.
-
jsalonen over 12 yearsI tried to fetch google.com, but all I get is
Start-BitsTransfer : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))
. I'm puzzled :| -
jsalonen over 12 yearsSo far this has been the best solution proposed. Also given that it seems I can rewrite it in one line format as
(new-object System.Net.WebClient).DownloadFile( '$url, $path)
it is the best correspondence forwget
I have seen so far. Thanks! -
Matthew Steeples over 12 years@jsalonen I think that BITS will only download files rather than pages. As Richard says it relies on some server side support (although I don't think it's Microsoft specific).
-
jsalonen over 12 yearsI see and I think I get the point in using BITS, however, its not what I'm looking for in here.
-
gWaldo over 11 yearsall the elegance of
dd
... -
Admin over 11 years@gWaldo you are kidding–this is a joy to use (speaking as someone just learning PS)
-
gWaldo over 11 yearsI just mean that the
-Outfile
parameter seems extraneous when you could just use>
(to overwrite) or>>
(to append) to a file. -
James about 11 yearsAs a side-note you can also do this asynchronously using something like (new-object System.Net.WebClient).DownloadFileAsync(url,filePath)
-
Mowgli almost 11 yearsCan we fetch a particular text via Webclient and outout to a notepad ? thanks
-
Peltier almost 11 years@gWaldo or even deduce the filename from the URL just like
wget
does :) -
Peltier almost 11 years
Invoke-WebRequest $url ($url -split "/")[-1]
. Unfortunately fails if the URL ends with a slash. Should be pretty straightforward to improve. -
Matthew Scharley over 10 yearsThis is now the correct answer, and I ran into wget accidentally testing if I had the actual wget installed. Annoying that it can't get the filename easily (you have to specify it in the output redirection), but this option has a better UI than the real wget (in my opinion) so there's that.
-
tvdo about 10 yearsAnd as of PS 4.0,
wget
andcurl
are aliasted toInvoke-WebRequest
(iwr
) by default :D -
user4514 about 10 years@Bob Thx, Feel free to edit the answer and include these aliases!
-
Peter Mortensen almost 10 yearsBut Windows 7 only comes with PowerShell 2.0, and the result will be "The term 'Invoke-WebRequest' is not recognized as the name of a cmdlet, ...".
-
Peter Mortensen almost 10 yearsYes, this works out of the box on Windows 7 (that comes with PowerShell 2.0). Sample:
$client.DownloadFile( "http://blog.stackexchange.com/", "c:/temp2/_Download.html")
-
Warren Rumak almost 10 yearsPowershell 4 is available for Windows 7 -- it's part of the Windows Management Framework. microsoft.com/en-us/download/details.aspx?id=40855
-
im_nullable almost 10 yearsFair warning: This method will put the entire content of the file into memory before writing it out to the file. This is not a good solution for downloading large files.
-
Warren Rumak over 9 years@im_nullable, good call -- I've added that to the post.
-
Warren Rumak over 9 years@dezza, what do you mean by "encoding"? The output is a capture of the content sent in the body of the HTTP GET response, be it binary files, HTML, or whatever else you ask for. It's really hard to see how a .py file can "break" by copying its raw contents from one place to another, unless the web server is the one messing with it first.....
-
Warren Rumak over 9 years@dezza I've updated the answer with a different approach. Try it again.
-
dza over 9 years@Warren +1 :) better and shorter.
-
Warren Rumak over 9 yearsp.s. To save some typing, you can type -o then hit tab to get tab completion for -OutFile
-
dza over 9 yearsThanks. Do you by any chance know how to clean copy/clipboard from PS console without block-selecting ? It's irritating copying from PS because almost every long command wraps on the next line.
-
Warren Rumak over 9 yearsAlso, the console in the Powershell ISE doesn't share the console's selection logic. I suggest using that instead of the standard PS console.
-
desalle over 9 yearsThis doesn't work if you're behind an authenticating firewall. You get an error "Proxy authentication required". You can fix this by running
$wc = New-Object Net.WebClient; $wc.UseDefaultCredentials = $true; $wc.Proxy.Credentials = $wc.Credentials
. You only need to do this once per session, it seems. (I'm not 100% sure why this works, it looks like the proxy is a session-level shared object...) -
Hut8 over 8 yearsWhy does this use 100% of one of my CPUs?
-
Chris S about 8 yearsThis uses IE, like everything in Powershell it's been done in a quick and dirty way, instead of just integrating wget or curl. But obviously if Microsoft did that it would ruin their licencing.
-
Warren Rumak almost 8 years@ChrisS It doesn't use IE if you provide the -UseBasicParsing parameter. (That's why this parameter is required for Server Core)
-
Nick over 7 years@jsalonen and since that's .NET, it works on PS 2.0, which I am restricted to at them moment.
-
Tyler Szabo about 7 years@gWaldo PowerShell can be quite slick. You can use shortcuts
(iwr http://www.google.com/).Content > google.html
or use argumentsInvoke-WebRequest -Uri "http://www.google.com" -OutFile google.html
. It's important to realize that in PowerShell the pipe is an object pipe, not just a character pipe so the output ofInvoke-WebRequest
isn't a stream of the file but rather an object where you'll need to use.Content
. Try this:$foo = Invoke-WebRequest http://www.google.com
then$foo | Get-Member
then$foo.StatusCode
or$foo.Content
. -
BurnsBA almost 7 yearsFor just getting a url and ignoring the results (e.g., part of an IIS warmup script) use DownloadData:
(new-object System.Net.WebClient).DownloadData($url) | Out-Null
-
Adi Roiban over 6 yearsOn Windows 2016 Core / Standard I had to pass
-usebasicparsing
as otherwise it was complaining about missing internet explorer engine -
Vomit IT - Chunky Mess Style over 6 yearsConsider adding some quoted reference to this answer supporting what you state in case the link ever dies so the answer content is still available that is currently only available via that link per your suggestion.
-
BaseZen about 6 yearsError messages are very unhelpful; if
$path
is a directory or existing file, it throws a generic Exception. Ah, Microsoft. -
bertieb over 5 yearsThis solution is mentioned in other answers (
wget
is an alias ofInvoke-WebRequest
, and one similar to the above) -
Zimba over 5 yearsThe point of the answer was to emphasise the note. None of the answers deal with no file being created due to the syntax error.
-
bertieb over 5 yearsThat should really be a comment on the other answer[s]
-
Ian Ellis about 5 yearsNice! Invoke-WebRequest blows up in my packer job, complaining about "Out of Memory". The WebClient.DownloadFile works a treat.
-
Zimba about 5 yearsThis answer is not provided in other answers nor similar to the one above.
-
Scott - Слава Україні almost 5 years(1) What is “the no browser initialized stuff”? (2) Note that the accepted answer already mentions
-UseBasicParsing
. -
Hashbrown over 4 years
converts the input to Unicode
: instead of> out.html
you can use| Set-Content out.html -Encoding Byte
, while not helpful foriwr
since it has-OutFile
now, it's good to know when writing binary data to files in other cmdlets -
Pro Q about 2 yearsRunning
client = New-Object System.Net.WebClient
gave me the errorclient : The term 'client' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
However, the one-liner worked.