Is it possible to download using the Windows command line?

474,887

Solution 1

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 

Solution 2

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.

Solution 3

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

Native alternative to wget in Windows PowerShell?

Solution 4

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') }"

Solution 5

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.

Share:
474,887
Robert Massa
Author by

Robert Massa

Developer at InfoProjects.

Updated on September 17, 2022

Comments

  • Robert Massa
    Robert Massa over 1 year

    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.

    • Arjan
      Arjan over 14 years
      More ideas in "If the only browser in Windows is dead, how to connect to the Internet?" at superuser.com/questions/50427/…
    • Arjan
      Arjan over 14 years
      And which out of the dozen Windows XP versions would that be?
    • Robert Massa
      Robert Massa over 14 years
      Let's say it can be any windows XP SP2 version and everything released later.
    • Robert Massa
      Robert Massa over 14 years
      @arjan Interesting question, but there's still no definitive answer.
    • Arjan
      Arjan over 14 years
      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. :-)
    • Anderson Green
      Anderson Green about 11 years
      More answers to this question can be found here: stackoverflow.com/questions/4619088/…
  • DaveParillo
    DaveParillo over 14 years
    I'm pretty sure 'cygwin' counts as a non-standard utility ;-)
  • DaveParillo
    DaveParillo over 14 years
    +1 good answer. ftp is pretty universal, as long as the server you're trying to download from supports it.
  • Robert Massa
    Robert Massa over 14 years
    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
    Robert Massa over 14 years
    I understand it's possible using wget, but my question states without the use non-standard windows utils.
  • Satanicpuppy
    Satanicpuppy over 14 years
    Install telnet? Telnet is like ftp; it comes with windows. Don't know about redirecting the output though.
  • Robert Massa
    Robert Massa over 14 years
    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)
  • DHayes
    DHayes over 14 years
    Like 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.
  • Arjan
    Arjan over 14 years
    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
    Arjan over 14 years
    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 superuser.com/questions/50427/… :-)
  • innaM
    innaM over 14 years
    Why install cygwin just to use wget? There is a native win32 binary available.
  • EvilChookie
    EvilChookie over 14 years
    @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.
  • sinni800
    sinni800 about 13 years
    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. users.ugent.be/~bpuype/wget
  • Reed Hedges
    Reed Hedges almost 13 years
    This 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.
  • akira
    akira almost 13 years
    it is part of windows7.
  • user1533603
    user1533603 over 12 years
    (New-Object Net.WebClient).DownloadFile('someurl', 'somepath')
  • jyz
    jyz about 12 years
    didn't work on my windows 7...
  • lenkite
    lenkite about 12 years
    This should have been the top-voted answer. bitsadmin is deprecated in favor of Windows powershell though.
  • Furqat Ubaydullayev
    Furqat Ubaydullayev over 11 years
    It was part of my windows 7 install. Thanks!
  • Kip
    Kip over 11 years
    this requires user interaction. probably not what most people who want to download a file from the command prompt want
  • Luigi Ranghetti
    Luigi Ranghetti over 11 years
    Confirm working on Win7
  • TrickyDBA
    TrickyDBA over 11 years
    Note: 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.
  • user2428118
    user2428118 over 10 years
    @EvilChookie Telnet comes with Windows Vista+, it simply isn't enabled by default in these versions of Windows.
  • barlop
    barlop almost 10 years
    @jyzuz It is part of Windows 7 Ultimate. I don't know what you have.
  • barlop
    barlop almost 10 years
    @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
    barlop almost 10 years
    I have a strange telnet.exe on win7 32bit ultimate here C:\Windows\winsxs\x86_microsoft-windows-telnet-client_31bf38‌​56ad364e35_6.1.7600.‌​16385_none_b807e7888‌​65dfff7> and similar on a win7 64bit ultimate C:\Windows\winsxs\amd64_microsoft-windows-telnet-client_31bf‌​385 0.16385_none_1426830c3ebb712d> but it/they don't do anything. A working telnet.exe has to be copied from XP
  • user2428118
    user2428118 almost 10 years
    @barlop The client does exist. You can just go to here and enable the Telnet client.
  • Janus Troelsen
    Janus Troelsen almost 10 years
    @harrymc sure it's an answer. it downloads a file. what more do you want?
  • Janus Troelsen
    Janus Troelsen almost 10 years
    from cmd.exe: powershell -command "& { iwr http://www.it1.net/it1_logo2.jpg -OutFile logo.jpg }". also works from the run prompt
  • harrymc
    harrymc almost 10 years
    (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.
  • Peter Mortensen
    Peter Mortensen almost 10 years
    What version of Windows and PowerShell is required for this to work?
  • jcarpenter2
    jcarpenter2 over 9 years
    Yeah, there's nothing you can't do without powershell. It's a real turing tarpit :)
  • Vaibhav Garg
    Vaibhav Garg over 9 years
    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.
  • hello_there_andy
    hello_there_andy about 9 years
    that's if permission to cscript.exe is permitted
  • hello_there_andy
    hello_there_andy about 9 years
    Confirm working on this Win8
  • hello_there_andy
    hello_there_andy about 9 years
    @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')
  • rogerdpack
    rogerdpack over 8 years
    @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\")"
  • kgadek
    kgadek over 8 years
    It's present in Win10… though doesn't handle FTP protocol :(
  • Peter Mortensen
    Peter Mortensen about 8 years
    This 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
    Peter Mortensen about 8 years
    This works, even on Windows XP 64-bit, PowerShell 2.0.
  • Satanicpuppy
    Satanicpuppy about 8 years
    @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.
  • Wayfarer
    Wayfarer about 8 years
    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.
  • knocte
    knocte about 8 years
    this doesn't work in Windows Server 2012 FYI, it throws a MethodInvocationException
  • kayleeFrye_onDeck
    kayleeFrye_onDeck over 7 years
    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!
  • amenthes
    amenthes about 6 years
    It's not present on my Win10 - at least not in safe mode.
  • DimiDak
    DimiDak about 3 years
    Must be the only answer in the whole internet about using cmd to download a file without powershell or the need to install tools like curl and wget.