Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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/
share|improve this question

6 Answers 6

up vote 22 down vote accepted

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 a newer version of the Windows Management Framework. Version 4 (which comes with Windows 8.1 / 2012 R2) is available here: http://www.microsoft.com/en-us/download/details.aspx?id=40855

share|improve this answer
2  
This 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. –  Matthew Scharley Jan 14 at 0:52
    
This is brilliant. Thank you! –  jsalonen May 25 at 17:32
    
But 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 Jun 6 at 17:51
3  
Fair 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. –  im_nullable Jul 13 at 6:35
1  
@dezza I've updated the answer with a different approach. Try it again. –  Warren Rumak Sep 20 at 20:06

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 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.

share|improve this answer
13  
So 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 for wget I have seen so far. Thanks! –  jsalonen Nov 28 '11 at 10:49
1  
As a side-note you can also do this asynchronously using something like (new-object System.Net.WebClient).DownloadFileAsync(url,filePath) –  James Apr 23 '13 at 8:49
    
Can we fetch a particular text via Webclient and outout to a notepad ? thanks –  Mowgli Jun 18 '13 at 16:11
1  
Yes, 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") –  Peter Mortensen Jun 6 at 17:57

There is Invoke-WebRequest in the upcoming Powershell V3

Invoke-WebRequest http://www.google.com/ -OutFile c:\google.html

http://technet.microsoft.com/en-us/library/hh849901.aspx

share|improve this answer
4  
all the elegance of dd... –  gWaldo Aug 31 '12 at 15:29
1  
@gWaldo you are kidding–this is a joy to use (speaking as someone just learning PS) –  Jack Douglas Oct 16 '12 at 20:41
6  
I just mean that the -Outfile parameter seems extraneous when you could just use > (to overwrite) or >> (to append) to a file. –  gWaldo Oct 17 '12 at 13:12
4  
@gWaldo or even deduce the filename from the URL just like wget does :) –  Peltier Jul 17 '13 at 10:29
2  
And as of PS 4.0, wget and curl are aliasted to Invoke-WebRequest (iwr) by default :D –  Bob Mar 25 at 16:12

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.

share|improve this answer
2  
BITS relies on support at the server end, if available this works in the background and you can get progress updates with other cmdlets. –  Richard Nov 28 '11 at 10:42
2  
I 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 Nov 28 '11 at 10:45
1  
@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). –  Matthew Steeples Nov 28 '11 at 11:09
    
I see and I think I get the point in using BITS, however, its not what I'm looking for in here. –  jsalonen Nov 28 '11 at 11:23

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 PS 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 PS 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).

Cheers

share|improve this answer

Here is a PowerShell-function that resolves short URLs before downloading the file

function get-FileFromUri {
param(
[parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[string]
[Alias('Uri')]
$Url,
[parameter(Mandatory=$false, Position=1)]
[string]
[Alias('Folder')]
$FolderPath
)
process {
try {
# resolve short URLs
$req = [System.Net.HttpWebRequest]::Create($Url)
$req.Method = "HEAD"
$response = $req.GetResponse()
$fUri = $response.ResponseUri
$filename = [System.IO.Path]::GetFileName($fUri.LocalPath);
$response.Close()
# download file
$destination = (Get-Item -Path ".\" -Verbose).FullName
if($FolderPath) { $destination = $FolderPath }
if ($destination.EndsWith('\')) {
$destination += $filename
} else {
$destination += '\' + $filename
}
$webclient = New-Object System.Net.webclient
$webclient.downloadfile($fUri.AbsoluteUri, $destination)
write-host -ForegroundColor DarkGreen "downloaded '$($fUri.AbsoluteUri)' to '$($destination)'"
} catch {
write-host -ForegroundColor DarkRed $_.Exception.Message
}
}
}

use it like this:

get-FileFromUri http://example.com/url/of/example/file

to download the file to the current folder
or

get-FileFromUri http://example.com/url/of/example/file C:\example-folder

to download the file to the specified folder

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.