19

I want a way to download a file via HTTP given its URL (similar to how wget works). I have seen the answers to this question, but I have two changes to the requirements:

  • I would like it to run on Windows 7 or later (though if it works on Windows XP, that's a bonus).
  • I need to be able to do this on a stock machine with nothing but the script, which should be text that could be easily entered on a keyboard or copy/pasted.
  • The shorter, the better.

So, essentially, I would like a .cmd (batch) script, VBScript, or PowerShell script that can accomplish the download. It could use COM or invoke Internet Explorer, but it needs to run without any input, and should behave well when invoked without a display (such as through a Telnet session).

Jason R. Coombs
  • 2,062
  • 2
  • 16
  • 19
  • dload v1.0 a win32 command line tool like wget http://superuser.com/a/833445/384998 – seizu Oct 30 '14 at 16:52
  • 1
    possible duplicate of [How to download files from command line in Windows, like Wget is doing?](http://superuser.com/questions/25538/how-to-download-files-from-command-line-in-windows-like-wget-is-doing) – bummi Jan 26 '15 at 12:24
  • Not a duplicate because this question is specifically looking for something that will run on a stock machine (without additional software, such as wget for Windows). – Jason R. Coombs Feb 01 '15 at 16:31
  • @JasonR.Coombs The accepted answer is the same as one of the answers in that question, so future reader gets nothing more. – Franklin Yu Nov 13 '17 at 19:17
  • I agree; at this point, the more general question supersedes this one. – Jason R. Coombs Nov 14 '17 at 13:54

8 Answers8

15

I would use Background Intelligent Transfer Service (BITS) (primer):

Background Intelligent Transfer Service (BITS) is a component of modern Microsoft Windows operating systems that facilitates prioritized, throttled, and asynchronous transfer of files between machines using idle network bandwidth.

Starting with Windows 7, Microsoft advises to use the PowerShell cmdlets for BITS.

% import-module bitstransfer
% Start-BitsTransfer http://path/to/file C:\Path\for\local\file

You could also use BITS via COM Objects, see here for an example VBScript. And there is bitsadmin, a Command line tool to control downloads:

BITSAdmin is a command-line tool that you can use to create download or upload jobs and monitor their progress.

In Windows 7 bitsadmin.exe states itself that it is a deprecated tool. Nevertheless:

% bitsadmin.exe /transfer "NAME" http://path/to/file C:\Path\for\local\file
Wasif
  • 7,984
  • 2
  • 19
  • 32
akira
  • 61,009
  • 17
  • 135
  • 165
  • 2
    It appears now that bitsadmin is deprecated and may not be included in future versions of Windows. – Jason R. Coombs Feb 20 '12 at 14:11
  • @JasonR.Coombs: link? reference? – akira Feb 21 '12 at 06:06
  • 2
    http://technet.microsoft.com/en-us/magazine/ff382721.aspx ... so, instead of "bitadmin.exe" one just uses bits-cmdlets. – akira Feb 21 '12 at 06:24
  • 1
    thanks for that. All I had to go on was bitsadmin was telling me it was deprecated when I ran it. – Jason R. Coombs Feb 22 '12 at 12:27
  • note that out-path must be fully qualified and not relative (hat tip http://superuser.com/questions/365755/why-doesnt-the-bitsadmin-exe-download-manager-work-for-me) – matt wilkie Nov 08 '16 at 21:33
  • What is the meaning of "NAME" in your `% bitsadmin.exe /transfer "NAME" http://path/to/file C:\Path\for\local\file ` example? – Kevin Meredith Dec 23 '16 at 15:37
  • @KevinMeredith: https://msdn.microsoft.com/en-us/library/aa362813(VS.85).aspx - "Use the name parameter to specify the name of the job." ... in the `/transfer` section – akira Dec 23 '16 at 19:48
  • `BITSADMIN version 3.0 BITS administration utility. (C) Copyright 2000-2006 Microsoft Corp. BITSAdmin is deprecated and is not guaranteed to be available in future versions of Windows. Administrative tools for the BITS service are now provided by BITS PowerShell cmdlets.` – BanksySan Jun 01 '18 at 16:16
15

If you have PowerShell >= 3.0, you can use Invoke-WebRequest:

Invoke-WebRequest -OutFile su.htm -Uri superuser.com

Or golfed:

iwr -outf su.htm superuser.com
Wasif
  • 7,984
  • 2
  • 19
  • 32
Zombo
  • 1
  • 24
  • 120
  • 163
7

Try the System.Net.WebClient class. There is a sample PowerShell script at the bottom of this page:

$c = new-object system.net.WebClient
$r = new-object system.io.StreamReader $c.OpenRead("http://superuser.com")
echo $r.ReadToEnd()
Wasif
  • 7,984
  • 2
  • 19
  • 32
Charles Gargent
  • 739
  • 6
  • 11
  • 2
    This is helpful. I found the WebClient also has a DownloadFile method, which will download the content directly to a file. Thanks. – Jason R. Coombs Apr 10 '10 at 11:56
3

Copy and paste the following six lines (or just the last four lines) into a text file. Then rename it to vget.vbs.

'cscript vget.vbs >FILE.TXT
'Run this vbscript at command line. Use above syntax to download/create FILE.TXT
Set oX = CreateObject("Microsoft.XmlHTTP")
oX.Open "GET", "http://www.exampleURL.com/FILE.TXT", False
oX.Send ""
WScript.Echo oX.responseText

Obviously you need to customize three things in this script to make it work for you.

  1. The part which says http://www.exampleURL.com/FILE.TXT. You will need to substitute the correct URL for the file you wish to download.
  2. The command you will run at the command line to execute this script; will need to specify the correct name for the script, vget.vbs, if that is what you called it.
  3. And the name FILE.TXT that you want the output to be directed to by a DOS batch command line.

I have only tried using this to download a raw ASCII text file (a more powerful CMD script) from my Dropbox account, so I don't know if it will work for EXE files, etc.; or from other Web servers.

If you dispense with the first two comment lines, it is only four lines long. If you know your way around VBScript you might even be able to carry this code around in your head, and type it into the command line as needed. It only contains five key command components: CreateObject, .Open, .Send, WScript.Echo and .responseText.

Wasif
  • 7,984
  • 2
  • 19
  • 32
ozidroid
  • 39
  • 1
2

Since no one has pointed it out I would like to add my answer.

If you have a version of python installed you could use some external libraries to download files.

python -m pip install wget # install the library
python -m wget http://someserver.org/somefile.rar

Further on you could put this command in wget.bat file like this:

python -m wget %1

Then you could just run wget.bat from command line, but I prefer running it directly because it shows a nice progress bar when run directly.

Wasif
  • 7,984
  • 2
  • 19
  • 32
Advik
  • 21
  • 3
2

There's a utility (resides with CMD) on Windows which can be run from CMD (if you have write access):

set url=https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg
set file=file.jpg
certutil -urlcache -split -f %url% %file%

Cmdlets in Powershell:

$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
$file = "file.jpg"
$ProgressPreference = "SilentlyContinue";
Invoke-WebRequest -Uri $url -outfile $file

.Net under PowerShell:

$url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg"
$file = "file.jpg"
# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http
# Create the HttpClient object
$client = New-Object -TypeName System.Net.Http.Httpclient
$task = $client.GetAsync($url)
$task.wait();
[io.file]::WriteAllBytes($file, $task.Result.Content.ReadAsByteArrayAsync().Result)

C# Command-line build with csc.exe:

https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/compiler-options/command-line-building-with-csc-exe

using System;
using System.IO;
using System.Net.Http;
using System.Threading.Tasks;

namespace DownloadImage
{
    class Program
    {
        static async Task Main(string[] args)
        {
            using var httpClient = new HttpClient();
            var url = "https://www.nsa.org/content/hl-images/2017/02/09/NSA.jpg";
            byte[] imageBytes = await httpClient.GetByteArrayAsync(url);
            using var fs = new FileStream("file.jpg", FileMode.Create);
            fs.Write(imageBytes, 0, imageBytes.Length);
        }
    }
}

Built in Windows applications. No need for external downloads.

Tested on Win 10

Zimba
  • 1,051
  • 11
  • 15
1

Here's my attempt to resume the ways of how file can be downloaded on Windows without usage of external tools.

It includes BITSADMIN, Microsoft.XmlHTTP and WinHTTP with a hybrid batch/JScript script that does not need temporary files, and System.Net.WebClinet with JScript.NET self-compiled hybrid.

Wasif
  • 7,984
  • 2
  • 19
  • 32
npocmaka
  • 1,259
  • 12
  • 12
0

This technique is very often used in hacking, download a file in memory using Powershell:

powershell -nop -c "& {(New-Object System.Net.WebClient).DownloadString('URL')}"
Wasif
  • 7,984
  • 2
  • 19
  • 32