Is it possible to download using the Windows command line?

124

54

Without using any non-standard (Windows included) utilities, is it possible to download using the Windows command line?

The preferred version is Windows XP, but it's also interesting to know for newer versions.

To further clarify my question:

  • It has to be using HTTP
  • The file needs to be saved
  • Standard clean Windows install, no extra tools

So basically, since everybody is screaming Wget, I want simple Wget functionality, without using Wget.

Robert Massa

Posted 2009-10-23T14:58:03.700

Reputation: 1 476

6

More ideas in "If the only browser in Windows is dead, how to connect to the Internet?" at http://superuser.com/questions/50427/if-the-only-browser-in-windows-is-dead-how-to-connect-to-the-internet/

– Arjan – 2009-10-23T15:16:10.740

And which out of the dozen Windows XP versions would that be? – Arjan – 2009-10-23T15:17:50.517

Let's say it can be any windows XP SP2 version and everything released later. – Robert Massa – 2009-10-23T15:19:11.157

@arjan Interesting question, but there's still no definitive answer. – Robert Massa – 2009-10-23T15:19:57.223

I should have asked for "edition". Like Starter, Home, Professional, Media Center, Tablet, maybe even Embedded (good change for tools there I guess!)... Or the European versions without Windows Media Player. :-) – Arjan – 2009-10-23T15:35:39.660

More answers to this question can be found here: http://stackoverflow.com/questions/4619088/windows-batch-file-file-download-from-a-url

– Anderson Green – 2013-03-17T02:09:52.100

Answers

51

You can write a VBScript and run it from the command line

Create a file downloadfile.vbs and insert the following lines of code:

' Set your settings
    strFileURL = "http://www.it1.net/images/it1_logo2.jpg"
    strHDLocation = "c:\logo.jpg"

' Fetch the file
    Set objXMLHTTP = CreateObject("MSXML2.XMLHTTP")

    objXMLHTTP.open "GET", strFileURL, false
    objXMLHTTP.send()

If objXMLHTTP.Status = 200 Then
Set objADOStream = CreateObject("ADODB.Stream")
objADOStream.Open
objADOStream.Type = 1 'adTypeBinary

objADOStream.Write objXMLHTTP.ResponseBody
objADOStream.Position = 0    'Set the stream position to the start

Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.Fileexists(strHDLocation) Then objFSO.DeleteFile strHDLocation
Set objFSO = Nothing

objADOStream.SaveToFile strHDLocation
objADOStream.Close
Set objADOStream = Nothing
End if

Set objXMLHTTP = Nothing

Run it from the command line as follows:

cscript.exe downloadfile.vbs 

Igor

Posted 2009-10-23T14:58:03.700

Reputation:

that's if permission to cscript.exe is permitted – hello_there_andy – 2015-04-02T11:23:22.207

I can confirm that I have tested this script in Windows PE 5.1 and it has worked like a charm. I intend to use it for offline deployment, in order to check the version of the platform in the USB drive against a text file stored on the server. – Wayfarer – 2016-05-05T16:55:00.490

1

I wonder if this relies on Internet Explorer, but I guess this would be a fine answer for "If the only browser in Windows is dead, how to connect to the Internet?" at http://superuser.com/questions/50427/if-the-only-browser-in-windows-is-dead-how-to-connect-to-the-internet/ :-)

– Arjan – 2009-10-23T15:37:28.707

74

Starting with Windows 7, I believe there's one single method that hasn't been mentioned yet that's easy:

Syntax:

bitsadmin  /transfer job_name       /download  /priority priority   URL  local\path\file

Example:

bitsadmin  /transfer mydownloadjob  /download  /priority normal  ^
                  http://example.com/filename.zip  C:\Users\username\Downloads\filename.zip

(Broken into two separate lines with ^ for readability (to avoid scrolling).)

Warning: As pointed out in the comments, the bitsadmin help message starts by saying:

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.

... but another comment reported that it works on Windows 8.

timeshift

Posted 2009-10-23T14:58:03.700

Reputation: 849

Doesn't run from a Batch file via task scheduler. I get this error: Unable to add file - 0x800704dd The operation being requested was not performed because the user has not logged on to the network. The specified service does not exist. – Vaibhav Garg – 2015-02-20T12:34:32.460

Confirm working on this Win8 – hello_there_andy – 2015-04-02T11:53:51.807

@VaibhavGarg you need to make sure the name of the downloaded file is exactly as appears on the last part of the download URL (after the final '/' and preceding, say, the '.exe') – hello_there_andy – 2015-04-02T11:55:15.327

2It's present in Win10… though doesn't handle FTP protocol :( – kgadek – 2015-10-29T17:43:40.433

It's not present on my Win10 - at least not in safe mode. – amenthes – 2018-05-05T16:45:22.140

1This looks like the kind of tool the OP was looking for, but it is not a standard part of a Windows install, you have to install it separately. Useful tool though. – Reed Hedges – 2011-07-22T15:42:31.113

16it is part of windows7. – akira – 2011-08-20T04:20:08.030

3didn't work on my windows 7... – jyz – 2012-04-05T00:14:46.083

3This should have been the top-voted answer. bitsadmin is deprecated in favor of Windows powershell though. – lenkite – 2012-05-14T09:04:04.597

1It was part of my windows 7 install. Thanks! – Burkhard – 2012-10-24T12:54:14.457

3Confirm working on Win7 – kaiser – 2012-12-10T07:41:13.597

7Note: 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. – Ujjwal Singh – 2013-01-01T20:33:18.513

1@jyzuz It is part of Windows 7 Ultimate. I don't know what you have. – barlop – 2014-05-29T02:31:25.297

40

Windows 7 includes PowerShell and there's pretty much nothing you can't do with PowerShell.

Native alternative to wget in Windows PowerShell?

user15130

Posted 2009-10-23T14:58:03.700

Reputation: 517

1Yeah, there's nothing you can't do without powershell. It's a real turing tarpit :) – jcarpenter2 – 2014-10-17T17:36:22.390

@JanusTroelsen my version of PowerShell responded "The term 'iwr' is not recognized as the name of a cmdlet,..." but after some investigation this similar worked: powershell -command "$clnt = new-object System.Net.WebClient; $clnt.DownloadFile(\"https://host/name\", \"outpufilename\")" – rogerdpack – 2015-08-28T21:01:26.617

16(New-Object Net.WebClient).DownloadFile('someurl', 'somepath') – Jason Stangroome – 2011-11-17T02:52:07.123

3Powershell is for Powerrangers ! How kool is that and why the heck didn't I know about this? Byebye cmd. – kaiser – 2012-12-10T07:45:10.037

18from cmd.exe: powershell -command "& { iwr http://www.it1.net/it1_logo2.jpg -OutFile logo.jpg }". also works from the run prompt – Janus Troelsen – 2014-06-03T11:57:34.880

25

PowerShell (included with Windows 8 and included with .NET for earlier releases) has this capability. The powershell command allows running arbitrary PowerShell commands from the command line or a .bat file. Thus, the following line is what's wanted:

powershell -command "& { (New-Object Net.WebClient).DownloadFile('http://example.com/', 'c:\somefile') }"

Nik

Posted 2009-10-23T14:58:03.700

Reputation: 259

2this doesn't work in Windows Server 2012 FYI, it throws a MethodInvocationException – knocte – 2016-05-18T09:41:30.103

2

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post - you can always comment on your own posts, and once you have sufficient reputation you will be able to comment on any post.

– harrymc – 2014-05-29T11:45:55.183

3@harrymc sure it's an answer. it downloads a file. what more do you want? – Janus Troelsen – 2014-06-03T11:55:07.943

1(1) Some text to explain what does this one-liner, (2) Verify that your answer isn't an alternative formulation to a previous answer, since if it is, it should at most be expressed as a comment on that answer. – harrymc – 2014-06-03T12:34:07.193

What version of Windows and PowerShell is required for this to work? – Peter Mortensen – 2014-06-06T18:11:02.467

17

I found a way of doing it, but really, just install Wget.

You can use Internet Explorer from a command line (iexplore.exe) and then enter a URL as an argument. So, run:

iexplore.exe http://blah.com/filename.zip

Whatever the file is, you'll need to specify it doesn't need confirmation ahead of time. Lo and behold, it will automatically perform the download. So yes, it is technically possible, but good lord do it in a different way.

DHayes

Posted 2009-10-23T14:58:03.700

Reputation: 2 103

I know wget is a much better way, it's just a hypothetical question ;) Your answer comes pretty close, but still requires user intervention(clicking "Save", or configuring not to display this dialog) – Robert Massa – 2009-10-23T15:15:07.387

3Like I said, you have to deselect the option to prompt to save for that file type. For example, download a zip file, disable that prompt, and then in the future any zip files accessed from the command line will automatically save. – DHayes – 2009-10-23T15:17:40.457

1+1, though I've not validated it works (but the final statement is very true) – Arjan – 2009-10-23T15:18:49.660

1+1 for "dear lord don't do it this way"... :) – quack quixote – 2009-10-23T16:34:53.333

12

Windows Explorer (not to be confused with Internet Explorer) can download files via HTTP. Just enter the URL into the Address bar. Or from the command line, for example, C:\windows\explorer.exe http://somewhere.com/filename.ext.

You get the classic File Download prompt. Unless the file is a type that Windows Explorer knows how to display inline, (.html, .jpg, .gif), in which case you would then need to right-click to save it.

I just tested this on my VMware image of a virgin install of Windows XP 2002 SP1, and it works fine.

Chris Noe

Posted 2009-10-23T14:58:03.700

Reputation: 377

Nowadays, it depends on your default-browser. Mine is chrome, and it auto-downloads to a known location, so I consider this solution to be sufficient for my needs. Thx! – kayleeFrye_onDeck – 2016-11-02T02:57:39.380

4this requires user interaction. probably not what most people who want to download a file from the command prompt want – Kip – 2012-11-14T20:14:13.687

7

You can use (in a standard Windows bat):

powershell -command "& { iwr http://www.it1.net/it1_logo2.jpg -OutFile logo.jpg }"

It seems to require PowerShell v4...

(Thanks to that comment and this one)

Anthony O.

Posted 2009-10-23T14:58:03.700

Reputation: 220

6

Use FTP.

From the command line:

ftp ftp.somesite.com
user
password

etc. FTP is included in every Windows version I can remember; probably not in 3.1, maybe not in Windows 95, but certainly everything after that.

@RM: It is going to be rough if you don't want to download any other tools. There exists a command line Wget for Windows and Wget is designed to do exactly what you're asking for.

Satanicpuppy

Posted 2009-10-23T14:58:03.700

Reputation: 6 169

1This will not work with most FTP servers as passive mode is not supported by this Windows ftp client (with NATs inbetween passive mode is required). – Peter Mortensen – 2016-04-05T21:04:33.083

@PeterMortensen It also doesn't remotely answer the question since he edited it to specify he was looking for wget functionality. But don't let that stop you from resurrecting a 7 year old wrong answer just to add some pedantry. – Satanicpuppy – 2016-04-06T19:32:21.207

+1 good answer. ftp is pretty universal, as long as the server you're trying to download from supports it. – DaveParillo – 2009-10-23T15:05:15.343

Thank you, sorry I wasn't more specific, I meant using HTTP. – Robert Massa – 2009-10-23T15:10:47.957

4

Use PowerShell like this:

  1. Create a download.ps1 file:

    param($url, $filename)
    $client = new-object System.Net.WebClient 
    $client.DownloadFile( $url, $filename)
    
  2. Now you can download a file like this:

    powershell Set-ExecutionPolicy Unrestricted
    powershell -ExecutionPolicy RemoteSigned -File "download.ps1" "http://somewhere.com/filename.ext" "d:\filename.ext"
    

Thomas Jespersen

Posted 2009-10-23T14:58:03.700

Reputation: 201

This works, even on Windows XP 64-bit, PowerShell 2.0. – Peter Mortensen – 2016-04-05T21:13:00.263

1

File can be download via below method

bitsadmin /transfer wcb /priority high https://sustainabledevelopment.un.org/content/documents/Agenda21.pdf C:\Program Files (x86)\Dell Update\Agenda21.pdf

Mansur Ali

Posted 2009-10-23T14:58:03.700

Reputation: 171

1

There are a few ways that you can download using the command line in Windows:

  1. You can use Cygwin.

    Note: the included apps are not native Linux apps. You must rebuild your application from source if you want to run on Windows.

  2. Using telnet it's possible to make a request but you won't see any processing.

  3. You can write bat or VBS scripts.

  4. Write your own program that you can run from cmd.exe.

Jesus

Posted 2009-10-23T14:58:03.700

Reputation: 11

1

If you install Telnet, I imagine you could make a HTTP request to a server to download a file.

You can also install Cygwin, and use wget to download a file as well. This is a very easy way to download files from the command line.

EvilChookie

Posted 2009-10-23T14:58:03.700

Reputation: 4 519

2I'm pretty sure 'cygwin' counts as a non-standard utility ;-) – DaveParillo – 2009-10-23T15:04:07.810

Telnet is an interesting option, is there a way to pipe the output to a file without corrupting it? And can we pipe HTTP GET command into telnet to make the request? – Robert Massa – 2009-10-23T15:05:52.083

Install telnet? Telnet is like ftp; it comes with windows. Don't know about redirecting the output though. – Satanicpuppy – 2009-10-23T15:10:46.410

For XP (as in the question) telnet is installed by default, but I heard that on Vista that's no longer the case? But no, it does not allow for file downloads, unless all is returned in a single HTTP response, and one can strip the headers and decode the stuff on the command line as well. Quite unlikely one can control that. – Arjan – 2009-10-23T15:21:12.213

1Why install cygwin just to use wget? There is a native win32 binary available. – innaM – 2009-10-23T15:39:00.903

@DaveParillo: My answer was posted before the requirements of clean install. @Robert: I'm not sure. I've never used telnet in this fashion. I said 'I imagine you could'. @SatanicPuppy: As Arjan pointed out, it's not in Vista by default. @Manni: I did not know there was a win32 binary available. – EvilChookie – 2009-10-24T07:58:24.887

@EvilChookie Telnet comes with Windows Vista+, it simply isn't enabled by default in these versions of Windows. – user2428118 – 2013-09-18T09:36:30.037

@user2428118 the telnet service exists sure, but the telnet CLIENT does not exist in Win7, telnet.exe it can be copied from XP though. – barlop – 2014-05-29T02:40:26.580

I have a strange telnet.exe on win7 32bit ultimate here C:\Windows\winsxs\x86_microsoft-windows-telnet-client_31bf3856ad364e35_6.1.7600.16385_none_b807e788865dfff7> and similar on a win7 64bit ultimate C:\Windows\winsxs\amd64_microsoft-windows-telnet-client_31bf385 0.16385_none_1426830c3ebb712d> but it/they don't do anything. A working telnet.exe has to be copied from XP – barlop – 2014-05-29T02:42:34.710

@barlop The client does exist. You can just go to here and enable the Telnet client.

– user2428118 – 2014-05-30T08:04:49.797

1

You can install the Linux application Wget on Windows. It can be downloaded from http://gnuwin32.sourceforge.net/packages/wget.htm. You can then issue the command 'wget (inserturlhere)' or any other URL in your command prompt, and it will allow you to download that URL/file/image.

Jamie

Posted 2009-10-23T14:58:03.700

Reputation:

1

Strangely this version is not always compatible. On my Windows 7 computer it wasn't because libraries were missing. You'd want to use wget without any extra libraries most of the time. http://users.ugent.be/~bpuype/wget/

– sinni800 – 2011-05-16T12:17:20.727

0

In default Windows, you can't download via HTTP. Windows is a GUI-centric OS, so it lacks many of the commandline tools you'd find in other OS's, like wget, which would be the prime candidate.

System.Net.WebClient.DownloadFile(), a function in the WiniNet API, can download files, but I'm not sure how far you're getting into actual development vs. a batch file.

Andrew Scagnelli

Posted 2009-10-23T14:58:03.700

Reputation: 1 777

I understand it's possible using wget, but my question states without the use non-standard windows utils. – Robert Massa – 2009-10-23T15:09:52.090

0

If you have python installed here's an example which fetches the get-pip.py from the web

python -c "import urllib; urllib.urlretrieve ('https://bootstrap.pypa.io/get-pip.py', r'C:\python27\Tools\get-pip.py')"

Sipherlab

Posted 2009-10-23T14:58:03.700

Reputation: 11