261

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/
216

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.

  • 3
    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 '14 at 0:52
  • 13
    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 '14 at 17:51
  • 15
    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 '14 at 6:35
  • 1
    @im_nullable, good call -- I've added that to the post. – Warren Rumak Sep 18 '14 at 15:47
  • 1
    @dezza I've updated the answer with a different approach. Try it again. – Warren Rumak Sep 20 '14 at 20:06
176

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.

  • 28
    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
  • 2
    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
  • 5
    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 '14 at 17:57
  • 3
    For 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 – BurnsBA May 2 '17 at 18:35
83

There is Invoke-WebRequest in the upcoming PowerShell version 3:

Invoke-WebRequest http://www.google.com/ -OutFile c:\google.html
  • 8
    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
  • 8
    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
  • 5
    @gWaldo or even deduce the filename from the URL just like wget does :) – Peltier Jul 17 '13 at 10:29
  • 5
    And as of PS 4.0, wget and curl are aliasted to Invoke-WebRequest (iwr) by default :D – Bob Mar 25 '14 at 16:12
16

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.

  • 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
  • I need to remember that one. Thanks! – flickerfly Jan 8 '15 at 22:40
6

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

4

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 to download the file to the current folder:

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

Or to download the file to a specified folder:

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

The following function will get a URL.

function Get-URLContent ($url, $path) {
  if (!$path) {
      $path = Join-Path $pwd.Path ([URI]$url).Segments[-1]
  }
  $wc = New-Object Net.WebClient
  $wc.UseDefaultCredentials = $true
  $wc.Proxy.Credentials = $wc.Credentials
  $wc.DownloadFile($url, $path)
}

Some comments:

  1. The last 4 lines are only needed if you are behind an authenticating proxy. For simple use, (New-Object Net.WebClient).DownloadFile($url, $path) works fine.
  2. The path must be absolute, as the download is not done in your current directory, so relative paths will result in the download getting lost somewhere.
  3. The if (!$path) {...} section handles the simple case where you just want to download the file to the current directory using the name given in the URL.
1

Use Windows 10 bash shell which includes wget once the windows feature is setup.

How to install Ubuntu bash shell on Windows:

YouTube: Running Bash on Ubuntu on Windows!

Windows Subsystem for Linux Documentation

  • 1
    Consider 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. – Pimp Juice IT Sep 27 '17 at 3:36
-1

Invoke-WebRequest -Uri "http://www.google.com/" -outfile "2.pdf"

Note: -outfile parameter expects a string, so if your filename starts with a number, and not enclosed in quotes, no output file is created.

This does not affect filenames starting with a letter.

  • This solution is mentioned in other answers (wget is an alias of Invoke-WebRequest, and one similar to the above) – bertieb Nov 27 '18 at 18:27
  • The point of the answer was to emphasise the note. None of the answers deal with no file being created due to the syntax error. – zimba Nov 28 '18 at 10:28
  • That should really be a comment on the other answer[s] – bertieb Nov 28 '18 at 13:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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