Skip to main content

How to Download a File from the Internet in a Batch Script

A common requirement for modern automation scripts is the ability to download files from the internet. You might need to fetch the latest version of a setup file, download a configuration file from a central repository, or pull a tool needed for the script to continue. While the native batch environment (cmd.exe) has no built-in command like wget or curl, modern Windows includes several powerful, built-in utilities that your batch script can easily control to perform downloads.

This guide will teach you the best methods for downloading a file. We will cover the most compatible and powerful method using PowerShell, which works on all modern Windows systems. We will also introduce the new, native curl.exe command available in Windows 10/11, which is an excellent and simple alternative.

The Challenge: No Native DOWNLOAD Command

The batch command processor is not a web client. It cannot natively handle HTTP/HTTPS protocols, redirects, or data streams. To download a file, a batch script must act as a "wrapper" that calls a more powerful, built-in Windows utility designed for network communication.

For any modern Windows system (Windows 7 and newer), calling PowerShell is the most robust, flexible, and universally available solution. PowerShell has full access to the .NET framework's networking capabilities.

The simplest method in PowerShell is to use the System.Net.WebClient object.

powershell -Command "(New-Object System.Net.WebClient).DownloadFile('URL', 'OutputFile')"
  • powershell -Command "...": Executes the PowerShell command from your batch script.
  • New-Object System.Net.WebClient: Creates a temporary web client object.
  • .DownloadFile('URL', 'OutputFile'): Calls the method to download the file from the specified URL and save it to the specified output file path.

Method 2 (Modern & Simple): Using the Native curl.exe

Starting with Windows 10 (build 1803 and newer), Windows now includes a native version of curl.exe, the immensely popular command-line tool for transferring data. If you know your script will only run on up-to-date Windows 10/11 machines, this is an excellent and very simple option.

Syntax: curl -L "URL" -o "OutputFile"

  • -L: Location. This is a crucial switch that tells curl to follow any HTTP redirects. Many download links will redirect to a different server.
  • -o "OutputFile": Specifies the output filename.
  • "URL": The URL of the file to download.

A Legacy Alternative: bitsadmin.exe

The Background Intelligent Transfer Service (bitsadmin) is another tool that can handle downloads. It's more complex and designed for background jobs, but it works on older systems.

Syntax: bitsadmin /transfer MyDownloadJob /download /priority normal "URL" "C:\Path\To\OutputFile"

For its complexity, this is generally not recommended unless you have a specific need for BITS features or are on an older system where PowerShell is not an option.

Common Pitfalls and How to Solve Them

  • Permissions: You cannot save a file to a protected directory (like C:\Program Files) unless your script is run with elevated privileges.

    • Solution: Run your script as an Administrator if you need to write to protected locations. Otherwise, save files to a user-writable directory like %TEMP% or %USERPROFILE%\Downloads.
  • Firewalls and Proxies: In a corporate environment, direct internet access may be blocked.

    • Solution: This is an environmental issue, not a script issue. The system's proxy settings must be configured correctly. For advanced scripts, PowerShell's WebClient object can be configured to use specific proxy credentials.
  • HTTPS/TLS Errors on Older Systems: An older OS (like Windows 7) may fail to download from a modern, secure server because it uses an outdated security protocol (TLS).

    • Solution: You can tell PowerShell to use a more modern protocol by adding a command. This is an advanced but sometimes necessary step.
      powershell -Command "[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12; (New-Object System.Net.WebClient).DownloadFile(...)"
  • Checking for Success: How do you know if the download worked?

    • Solution: Check the %ERRORLEVEL% immediately after the command. An exit code of 0 means success. A non-zero code means the command failed (e.g., "404 Not Found," network error).

Practical Example: A "Download If Missing" Tool Script

This script needs a specific command-line tool (7z.exe) to function. It first checks if the tool exists, and if it doesn't, it downloads the 7-Zip portable executable. It uses the recommended PowerShell method.

@ECHO OFF
SETLOCAL
SET "ToolPath=%~dp07z.exe"
SET "DownloadURL=https://www.7-zip.org/a/7z2301-extra.7z"
SET "ArchiveFile=%TEMP%\7z_archive.7z"
SET "7zDLL=%~dp07z.dll"

ECHO --- Tool Dependency Checker ---
ECHO.
IF EXIST "%ToolPath%" (
ECHO [SUCCESS] Found %ToolPath%. Ready to proceed.
GOTO :End
)

ECHO [INFO] 7z.exe not found. Attempting to download it...
ECHO.

REM --- The Download Command ---
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('%DownloadURL%', '%ArchiveFile%')"

IF %ERRORLEVEL% NEQ 0 (
ECHO [FAILURE] Download failed. Please check your internet connection.
GOTO :End
)

ECHO Download successful. You will need to extract the archive manually.
ECHO Archive is located at: %ArchiveFile%

:End
ENDLOCAL

Conclusion

While cmd.exe cannot download files on its own, it can easily control powerful, built-in Windows utilities to get the job done.

  • The PowerShell WebClient.DownloadFile method is the most compatible and recommended approach, working reliably on all modern Windows versions from 7 onwards.
  • The native curl.exe command is an excellent, simple alternative if you are scripting exclusively for up-to-date Windows 10/11 environments.
  • Always check %ERRORLEVEL% after your download command to handle potential failures gracefully.
  • Be mindful of permissions and save files to user-writable locations unless you are running as an Administrator.