350

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

12 Answers 12

320

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.

17
  • 4
    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. Jan 14, 2014 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, ...". Jun 6, 2014 at 17:51
  • 27
    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. Jul 13, 2014 at 6:35
  • 2
    @im_nullable, good call -- I've added that to the post. Sep 18, 2014 at 15:47
  • 1
    @dezza I've updated the answer with a different approach. Try it again. Sep 20, 2014 at 20:06
197

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.

11
  • 40
    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, 2011 at 10:49
  • 3
    As a side-note you can also do this asynchronously using something like (new-object System.Net.WebClient).DownloadFileAsync(url,filePath)
    – James
    Apr 23, 2013 at 8:49
  • 6
    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") Jun 6, 2014 at 17:57
  • 4
    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, 2017 at 18:35
  • 1
    Error messages are very unhelpful; if $path is a directory or existing file, it throws a generic Exception. Ah, Microsoft.
    – BaseZen
    Mar 30, 2018 at 14:24
104

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

Invoke-WebRequest http://www.google.com/ -OutFile c:\google.html
9
  • 10
    all the elegance of dd...
    – gWaldo
    Aug 31, 2012 at 15:29
  • 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, 2012 at 13:12
  • 5
    @gWaldo or even deduce the filename from the URL just like wget does :)
    – Peltier
    Jul 17, 2013 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, 2014 at 16:12
  • 2
    @gWaldo PowerShell can be quite slick. You can use shortcuts (iwr http://www.google.com/).Content > google.html or use arguments Invoke-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 of Invoke-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. Mar 8, 2017 at 0:20
20

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.

4
  • 3
    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, 2011 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, 2011 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). Nov 28, 2011 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, 2011 at 11:23
8

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

8

If your Windows is new enough (like version 1809 or newer), there's a "real" curl available. curl has the command-Line option "-O" (capital letter O; small letter won't do the same!) The option "-O", alternatively "--remote-name" tells curl, that the saved file gets the same name as the file-name part of the URL.

One needs to start this as "curl.exe", to discern it from the Alias "curl" for "Invoke-WebRequest". Incidentally it works in cmd.exe without changes.

Using the same example as in another answer here

curl.exe -O http://demo.mediacore.tv/files/31266.mp4

(The site won't allow me to add this as a comment, since I apparently need more "reputation" for that - so it gets a new answer)

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
  • 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. Sep 27, 2017 at 3:36
0

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

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

This does not affect filenames starting with a letter.

4
  • 1
    This solution is mentioned in other answers (wget is an alias of Invoke-WebRequest, and one similar to the above)
    – bertieb
    Nov 27, 2018 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, 2018 at 10:28
  • That should really be a comment on the other answer[s]
    – bertieb
    Nov 28, 2018 at 13:02
  • This answer is not provided in other answers nor similar to the one above.
    – Zimba
    Mar 23, 2019 at 15:19
0

PowerShell Invoke-RestMethod may have fewer dependencies than other methods ... in case you have a minimal (or older) Windows Server installed.

See error reported at Running Invoke-WebRequest as System account:

Invoke-WebRequest : The response content cannot be parsed because the Internet Explorer engine is not available, or Internet Explorer's first-launch configuration is not complete. Specify the UseBasicParsing parameter and try again.


This can be an alternative to applying the -UseBasicParsing option that is, in some cases, required with wget or Invoke-WebRequest.

However, the displayed response may be in a different format, based on data parsing:

PowerShell formats the response based to the data type. For an RSS or ATOM feed, PowerShell returns the Item or Entry XML nodes. For JavaScript Object Notation (JSON) or XML, PowerShell converts, or deserializes, the content into [PSCustomObject] objects.

-1

This should work for you to get around the no browser initialized stuff. Note the "-UseBasicParsing" param.

Invoke-WebRequest http://localhost -UseBasicParsing
1
  • 2
    (1) What is “the no browser initialized stuff”? (2) Note that the accepted answer already mentions -UseBasicParsing. Apr 28, 2019 at 6:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .