Windows script download file

Обновлено:
Опубликовано:

Для загрузки файлов из Интернета в Powershell предусмотрен метод DownloadFile, которому нужно передать два аргумента — что скачиваем и куда. Рассмотрим способ его применения, а также дополнительные команды для загрузки и распаковки архива.

По HTTP или  HTTPS
    DownloadFile
    Invoke-WebRequest
С FTP-сервера
Распаковка архива
Возможные проблемы

Скачать по HTTP или  HTTPS

Рассмотрим методы, удобные для загрузки файлов по протоколу http (https).

DownloadFile

Данный метод удобно применять для написания скриптор. Пример:

# Задаем переменные что копируем и куда:
$download_url = «https://download_ahyware/file1.zip»
$local_path = «C:\Downloads\file1.zip» 

# Создаем объект WebClient и скачиваем файл с помощью DownloadFile:
$WebClient = New-Object System.Net.WebClient
$WebClient.DownloadFile($download_url, $local_path)

* в данном примере мы скачаем файл https://download_ahyware/file1.zip и разместим его на компьютере по пути C:\Downloads\file1.zip.

Invoke-WebRequest

Данный командлет предназначен для загрузки содержимого html документа. Также с его помощью можно скачивать файлы:

Invoke-WebRequest -URI https://www.dmosk.ru/files/winsetupfromusb.zip -outfile c:\temp\winsetupfromusb.zip

* где URI — путь-источник (что скачиваем); outfile — путь-назначение (куда скачиваем).

Для удобства можно использовать команды wget или iwr — по сути, это алиас на Invoke-WebRequest:

wget https://www.dmosk.ru/files/winsetupfromusb.zip -o winsetupfromusb.zip

iwr https://www.dmosk.ru/files/winsetupfromusb.zip -o winsetupfromusb.zip

* обязательно задаем опцию outfile (-o), в противном случае, загрузка выполнится, но не сохранится файл.

Скачать с FTP

Принцип скачивания файлов с FTP-сервера такой же, за исключением того, что сначала необходима авторизация.

Пример скрипта:

$download_url = «ftp://download_ahyware/file1.zip»
$local_path = «C:\Downloads\file1.zip»
$user = «myFtpUser»
$pass = «myPassword123»

$WebClient = New-Object System.Net.WebClient 
$WebClient.Credentials = New-Object System.Net.NetworkCredential($user, $pass)
$WebClient.DownloadFile($download_url, $local_path)

* в данном скрипте мы авторизовываемся на FTP-сервере с учетной записью myFtpUser и паролем myPassword123. После, как в примере выше, мы скачали файл в папку C:\Downloads.

Распаковываем архив

Чаще всего, файлы в сети Интернет хранятся в сжатом виде. Поэтому, после загрузки файла есть необходимость его распаковать. Для этого в Powershell можно использовать следующую команду:

Expand-Archive $archive_file.zip -DestinationPath C:\Unzip\

Однако, не все версии Powershell поддерживают командлет Expand-Archive. В таком случае можно вызвать стороннее приложение, например 7-zip. Пример скрипта:

set-alias sz «$env:ProgramFiles\7-Zip\7z.exe» 
sz x -r C:\Downloads\file1.zip -oC:\Unzip\

Возможные ошибки

Исключение при вызове downloadfile с 2 аргументами исключение во время запроса webclient

Причина: как правило, неправильно указан путь для загрузки или отсутствие прав на папку, в которую должно идти скачивание.

Решение: проверяем путь или пробуем указать другой.

  1. Using Curl Command

  2. Downloading Multiple Files

  3. Error Handling in Batch Scripts

  4. Conclusion

  5. FAQ

How to Download File From URL in Batch Script

In today’s digital age, downloading files from URLs using scripts has become a common task for developers and system administrators alike. Batch scripts, in particular, provide a simple yet powerful way to automate this process on Windows systems. Whether you’re looking to download a single file or multiple files from various URLs, mastering batch scripts can save you time and effort.

In this tutorial, we will explore how to download files from URLs using batch scripts, focusing on both the basic commands and some advanced techniques. By the end of this guide, you’ll be equipped with the knowledge to streamline your file downloading tasks effectively.

Batch scripts are text files containing a sequence of commands that can be executed by the command-line interpreter on Windows. They are particularly useful for automating repetitive tasks, like downloading files from the internet. The beauty of batch scripts lies in their simplicity; you can write them using any text editor and execute them directly from the command prompt.

To get started, you will need to open a text editor like Notepad and begin writing your batch script. The most common command used for downloading files in a batch script is curl, which is included in Windows 10 and later versions. If you’re using an older version of Windows, you may need to install it separately.

Using Curl Command

The curl command is a powerful tool for transferring data from or to a server using various protocols, including HTTP, HTTPS, and FTP. Downloading a file using curl in a batch script is straightforward. Here’s how you can do it:

@echo off
set URL=https://example.com/file.zip
set OUTPUT=file.zip
curl -o %OUTPUT% %URL%

Output:

file.zip                     100%[====================================>]  1.23M  1.23MB/s    in 0.9s    

In this script, we start by turning off the command echoing with @echo off. Then, we set the URL variable to the file you want to download and the OUTPUT variable to the name you want to give the downloaded file. The curl command uses the -o option to specify the output file name, followed by the URL of the file to be downloaded. When you run this script, it will download the specified file from the URL and save it with the given name.

Using curl is not only efficient but also allows for various options, such as resuming interrupted downloads or specifying user authentication.

Downloading Multiple Files

If you need to download multiple files from different URLs, you can easily extend your batch script. Here’s an example of how to do that:

@echo off
setlocal enabledelayedexpansion

set URLs=https://example.com/file1.zip https://example.com/file2.zip https://example.com/file3.zip
set OUTPUT_DIR=downloads

mkdir %OUTPUT_DIR%

for %%u in (%URLs%) do (
    set FILENAME=%%~nxu
    curl -o "%OUTPUT_DIR%\!FILENAME!" %%u
)

Output:

file1.zip                   100%[====================================>]  1.23M  1.23MB/s    in 0.9s    
file2.zip                   100%[====================================>]  1.45M  1.45MB/s    in 1.0s    
file3.zip                   100%[====================================>]  1.67M  1.67MB/s    in 1.1s    

In this script, we define a list of URLs separated by spaces and specify an output directory where the files will be saved. The mkdir command creates the directory if it doesn’t already exist. The for loop iterates through each URL, extracting the filename and using curl to download the file into the specified directory. The use of enabledelayedexpansion allows us to correctly handle variable expansion within the loop.

This method is efficient for downloading multiple files in one go, saving you the hassle of executing separate commands for each file.

Error Handling in Batch Scripts

When downloading files, it is essential to handle potential errors gracefully. Here’s how you can implement basic error handling in your batch script:

@echo off
set URL=https://example.com/file.zip
set OUTPUT=file.zip

curl -o %OUTPUT% %URL%
if errorlevel 1 (
    echo Download failed. Please check the URL or your internet connection.
) else (
    echo Download completed successfully: %OUTPUT%
)

Output:

Download completed successfully: file.zip

In this script, we check the exit status of the curl command using if errorlevel 1. If the command fails (returns a non-zero exit code), we print an error message. Otherwise, we confirm that the download was successful. This simple error handling mechanism can help you troubleshoot issues when a download does not go as planned.

Conclusion

Downloading files from URLs using batch scripts is a skill that can significantly enhance your productivity. By leveraging commands like curl, you can automate the process for both single and multiple files, while also incorporating error handling to ensure a smooth experience. Whether you are managing downloads for work or personal projects, mastering batch scripts will empower you to handle file downloads efficiently.

With the techniques outlined in this tutorial, you’re now ready to put your batch scripting skills to the test. Start creating your scripts today and simplify your file downloading tasks!

FAQ

  1. How do I create a batch script?
    You can create a batch script by opening a text editor, writing your commands, and saving the file with a .bat extension.

  2. Can I use batch scripts on older versions of Windows?
    Yes, batch scripts are supported on older versions of Windows, but you may need to install commands like curl separately.

  3. What if I encounter errors while downloading?
    You can implement error handling in your batch script to check the exit status of commands and provide feedback.

  4. Is it possible to download files using FTP in batch scripts?
    Yes, you can use curl or other FTP commands in batch scripts to download files from FTP servers.

  5. Can I schedule batch scripts to run automatically?
    Yes, you can use Windows Task Scheduler to run your batch scripts at specified times or intervals.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe

To download a file using PowerShell, you can use the `Invoke-WebRequest` cmdlet, which allows you to specify a URL and a destination path for the file being downloaded.

Invoke-WebRequest -Uri "http://example.com/file.txt" -OutFile "C:\path\to\your\file.txt"

Understanding PowerShell File Download Basics

What is PowerShell?

PowerShell is a task automation framework, consisting of a command-line shell and associated scripting language. It is built on the .NET framework, enabling IT professionals to control and automate the administration of Windows operating systems and applications. With its rich set of cmdlets, PowerShell provides users with a powerful tool to streamline repetitive tasks, making it an essential skill for system administrators and developers alike.

The Importance of File Downloads in Automation

In many automation scenarios, the ability to download files programmatically is crucial. For example, systems often need to pull updates, retrieve data files from cloud storage, or download application packages for installation. Mastering the art of downloading files with PowerShell not only enhances productivity but also allows users to incorporate these tasks into larger scripts or workflows, enabling seamless data handling and system maintenance.

PowerShell Upload File: A Quick How-To Guide

PowerShell Upload File: A Quick How-To Guide

How to Download a File with PowerShell

Using the `Invoke-WebRequest` cmdlet

What is `Invoke-WebRequest`?
`Invoke-WebRequest` is a versatile cmdlet in PowerShell designed to send HTTP and HTTPS requests to web servers. This command can be used not only to fetch web pages but also to download files, making it a go-to choice for many users.

Basic Syntax
The syntax to use `Invoke-WebRequest` for downloading files is quite straightforward:

Invoke-WebRequest -Uri '<URL>' -OutFile '<LocalPath>'

Example: Downloading a file from a URL
To download a file using this cmdlet, you can use the following command:

Invoke-WebRequest -Uri 'https://example.com/file.zip' -OutFile 'C:\Downloads\file.zip'

This command sends a request to the specified URL and saves the downloaded file to the local path given in the `-OutFile` parameter.

Explaining Parameters

  • `-Uri`: This parameter specifies the URL of the file you want to download. It is crucial to ensure that the URL is correct and accessible.
  • `-OutFile`: This parameter indicates where on your local file system the downloaded file should be saved. Make sure the directory exists; otherwise, PowerShell will return an error.

Using the `Start-BitsTransfer` cmdlet

What is `Start-BitsTransfer`?
`Start-BitsTransfer` is another powerful cmdlet that leverages the Background Intelligent Transfer Service (BITS) to manage file transfers. BITS is especially beneficial for downloading files as it can resume broken downloads automatically.

Basic Syntax
Similar to `Invoke-WebRequest`, the syntax for `Start-BitsTransfer` is as follows:

Start-BitsTransfer -Source '<URL>' -Destination '<LocalPath>'

Example: Downloading a file using BITS
Here’s how you can utilize `Start-BitsTransfer`:

Start-BitsTransfer -Source 'https://example.com/file.zip' -Destination 'C:\Downloads\file.zip'

This command begins transferring the file specified in the `-Source` parameter to the path specified in the `-Destination` parameter.

Benefits of Using BITS
Using `Start-BitsTransfer` has several advantages:

  • Resumable Transfers: If a download is interrupted (due to network issues, for instance), you can resume it without starting over.
  • Bandwidth Management: BITS transfers occur when system resources are available, making it more efficient for large downloads.

Mastering PowerShell Noprofile for Swift Command Execution

Mastering PowerShell Noprofile for Swift Command Execution

Advanced Techniques for Downloading Files

PowerShell Script to Download File from URL

An effective way to automate file downloads is to create a PowerShell script. Below is a simple script that downloads a file from a URL.

Creating a Script File
You can create a new text file with a .ps1 extension (for example, `DownloadFile.ps1`) and include the following code:

$Url = 'https://example.com/file.zip'
$Output = 'C:\Downloads\file.zip'
Invoke-WebRequest -Uri $Url -OutFile $Output

Explanation

  • `$Url`: This variable stores the URL of the file to download.
  • `$Output`: This variable defines where the file will be saved locally.
  • The `Invoke-WebRequest` command uses these variables to perform the download.

Downloading Multiple Files at Once

If you need to download several files efficiently, you can use a list of URLs in combination with a loop. Here’s how to do it:

Using a List of URLs
You can define an array containing multiple URLs and iterate through them to download each file:

$Urls = @(
    'https://example.com/file1.zip',
    'https://example.com/file2.zip'
)

foreach ($Url in $Urls) {
    Invoke-WebRequest -Uri $Url -OutFile "C:\Downloads\$($Url.Split('/')[-1])"
}

This script downloads each file and saves it in the `C:\Downloads` folder, using the name derived from the URL.

Efficiency in Bulk Downloads
By incorporating loops, you can save time and easily manage bulk downloads. This is especially useful in scenarios where files need to be aggregated for processing or reporting.

PowerShell Upload File to Dropbox: A Simple Guide

PowerShell Upload File to Dropbox: A Simple Guide

Error Handling and Troubleshooting

Handling Download Errors

Errors can occur during file downloads due to network issues, invalid URLs, or lack of permissions. Using a `try-catch` block allows you to manage these exceptions gracefully:

try {
    Invoke-WebRequest -Uri '<URL>' -OutFile '<LocalPath>'
} catch {
    Write-Host "Error downloading file: $_"
}

This method catches any errors that occur during the download and displays a user-friendly message.

Common Issues Encountered

  • Network Issues: Ensure your internet connection is stable and the URL is reachable.
  • Permission Errors: Check that you have write permissions to the specified local directory.
  • File Path Problems: Confirm that the path where you are attempting to save the file exists.

Mastering PowerShell Log Files: Tips and Tricks

Mastering PowerShell Log Files: Tips and Tricks

Conclusion

Understanding how to PowerShell download files opens a world of possibilities for automation and system management. With techniques like using `Invoke-WebRequest` and `Start-BitsTransfer`, as well as the ability to create scripts for bulk downloads, you can efficiently handle file transfers in your workflows. Practice these skills regularly, and they will soon become second nature, enhancing your PowerShell prowess.

Understanding PowerShell Downgrade Attack Techniques

Understanding PowerShell Downgrade Attack Techniques

Additional Resources

To further deepen your knowledge, refer to the official [PowerShell documentation](https://docs.microsoft.com/en-us/powershell/) for cmdlets and scripting techniques, and consider exploring community forums and tutorials for more advanced usage.

Mastering the PowerShell Profiler for Efficient Scripting

Mastering the PowerShell Profiler for Efficient Scripting

FAQs

Can I resume a failed download in PowerShell?

Yes, using BITS with the `Start-BitsTransfer` cmdlet allows you to resume downloads that were interrupted.

How do I check if a file downloaded successfully?

After download, you can verify the file’s presence using:

Test-Path 'C:\Downloads\file.zip'

This will return `True` if the file exists.

What if the file URL requires authentication?

If a URL requires authentication, you can use the `-Credential` parameter in `Invoke-WebRequest` to provide a username and password.

By mastering these techniques, you are well on your way to streamlining your workflows with PowerShell!

HTTPDownload «http://www.robvanderwoude.com/files/wmigen.zip», «C:\»

Sub HTTPDownload( myURL, myPath )
‘ This Sub downloads the FILE specified in myURL to the path specified in myPath.

‘ myURL must always end with a file name
‘ myPath may be a directory or a file name; in either case the directory must exist

‘ Written by Rob van der Woude
‘ http://www.robvanderwoude.com

‘ Based on a script found on the Thai Visa forum
‘ http://www.thaivisa.com/forum/index.php?showtopic=21832

    ‘ Standard housekeeping
    Dim i, objFile, objFSO, objHTTP, strFile, strMsg
    Const ForReading = 1, ForWriting = 2, ForAppending = 8

    ‘ Create a File System Object
    Set objFSO = CreateObject( «Scripting.FileSystemObject» )

    ‘ Check if the specified target file or folder exists,
    ‘ and build the fully qualified path of the target file
    If objFSO.FolderExists( myPath ) Then
        strFile = objFSO.BuildPath( myPath, Mid( myURL, InStrRev( myURL, «/» ) + 1 ) )
    ElseIf objFSO.FolderExists( Left( myPath, InStrRev( myPath, «\» ) — 1 ) ) Then
        strFile = myPath
    Else
        WScript.Echo «ERROR: Target folder not found.»
        Exit Sub
    End If

    ‘ Create or open the target file
    Set objFile = objFSO.OpenTextFile( strFile, ForWriting, True )

    ‘ Create an HTTP object
    Set objHTTP = CreateObject( «WinHttp.WinHttpRequest.5.1» )

    ‘ Download the specified URL
    objHTTP.Open «GET», myURL, False
    objHTTP.Send

    ‘ Write the downloaded byte stream to the target file
    For i = 1 To LenB( objHTTP.ResponseBody )
        objFile.Write Chr( AscB( MidB( objHTTP.ResponseBody, i, 1 ) ) )
    Next

    ‘ Close the target file
    objFile.Close( )
End Sub

Download «http://www.robvanderwoude.com/files/hardwinv.zip», «C:\hardwinv.zip»

Sub Download( myFileURL, myDestFile )
‘ This function uses X-standards.com’s X-HTTP component to download a file

‘ Arguments:
‘ myFileURL  [string] the URL of the file to be downloaded
‘ myDestFile [string] the fully qualified path of the downloaded «target» file

‘ Written by Rob van der Woude
‘ http://www.robvanderwoude.com

‘ The X-HTTP component is available at:
‘ http://www.xstandard.com/page.asp?p=C8AACBA3-702F-4BF0-894A-B6679AA949E6
‘ For more information on available functionality read:
‘ http://www.xstandard.com/printer-friendly.asp?id=32ADACB9-6093-452A-9464-9269867AB16E
    Dim objHTTP
    Set objHTTP = CreateObject(«XStandard.HTTP»)
    objHTTP.Get myFileURL
    objHTTP.SaveResponseToFile myDestFile
    Set objHTTP = Nothing
End Sub

One of my team members recently searched for a PowerShell script to download files from a URL. I suggested different methods to achieve this. In this tutorial, I will explain how to download a file from a URL using PowerShell.

There are multiple ways to download files using PowerShell. I will cover three primary methods:

  1. Using the Invoke-WebRequest cmdlet (built-in to PowerShell)
  2. Using the System.Net.WebClient class (.NET class)
  3. Using the Start-BitsTransfer cmdlet (requires BITS service)

Let me show you each method with examples.

1. Using Invoke-WebRequest

The Invoke-WebRequest cmdlet is the best way to download files from a URL using PowerShell. It allows you to send HTTP/HTTPS requests and save the response content to a file.

Here’s an example of downloading a ZIP file of US historical weather data:

$url = "https://www.spguides.com/wp-content/uploads/2024/12/WeatherInfo.zip" 
$output = "C:\WeatherData\WeatherInfo.zip"
Invoke-WebRequest -Uri $url -OutFile $output

This downloads the file from the specified URL and saves it to the output path on your local machine.

In this example:

  • $sourceUrl contains the URL of the file to be downloaded.
  • $destinationPath specifies the local path where the file will be saved.
  • Invoke-WebRequest fetches the file and saves it to the specified path.

I executed the above PowerShell script, and you can see it displays the exact output in the screenshot below; it downloaded the zip file and saved it on the local drive.

Download a File from a URL Using PowerShell

Sometimes, you might receive an error like The remote server returned an error: (403) Forbidden.

If the server requires authentication, you can include credentials:

$sourceUrl = "https://www.spguides.com/wp-content/uploads/2024/12/WeatherInfo.zip"
$destinationPath = "C:\WeatherData\WeatherInfo.zip"
$username = "your-username"
$password = "your-password"

$securePassword = ConvertTo-SecureString $password -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential ($username, $securePassword)

try {
    Invoke-WebRequest -Uri $sourceUrl -OutFile $destinationPath -Credential $credentials -Headers @{
        "User-Agent" = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36"
    }
    Write-Output "Download successful!"
} catch {
    Write-Output "An error occurred: $_"
}

Check out Check If File Modified In Last 24 Hours Using PowerShell

2. using System.Net.WebClient

Let me show you another method to download a file from a URL using PowerShell.

The System.Net.WebClient class provides another way to download files in PowerShell. It’s a .NET class so it requires creating a WebClient object first.

Example of downloading US national park data:

$url = "https://www.nps.gov/subjects/developer/api-documentation.htm#/parks/getPark"
$output = "C:\WeatherData\us-parks.json"

$wc = New-Object System.Net.WebClient
$wc.DownloadFile($url, $output)

This creates a new WebClient object, then uses its DownloadFile() method to save the file from the URL to the specified local path.

Here is the exact output in the screenshot below:

How to Download a File from URL Using PowerShell

If you want to handle timeout as well as any errors, then you can use the try catch block. Let me show you another example.

$sourceUrl = "https://example.com/financial-report.pdf"
$destinationPath = "C:\Downloads\financial-report.pdf"

$webClient = New-Object System.Net.WebClient
$webClient.Timeout = 60000 # Timeout in milliseconds

try {
    $webClient.DownloadFile($sourceUrl, $destinationPath)
    Write-Output "Download successful!"
} catch {
    Write-Output "An error occurred: $_"
}

Read How to Create a Folder If Not Exists in PowerShell?

3. using Start-BitsTransfer

Start-BitsTransfer is a PowerShell cmdlet for the Background Intelligent Transfer Service (BITS). It supports resuming interrupted downloads, download throttling, and asynchronous transfers.

Example downloading US Census data:

$url = "https://www2.census.gov/programs-surveys/popest/datasets/2020-2021/cities/totals/SUB-IP-EST2021-ANNRNK.csv"
$output = "C:\MyFolder\us-city-population-2021.csv"

Start-BitsTransfer -Source $url -Destination $output

This uses BITS to download the CSV file of 2021 US city population estimates from the Census Bureau website to the specified local destination.

Some key advantages of Start-BitsTransfer are:

  • Ability to pause and resume downloads
  • Configurable transfer rates to throttle bandwidth usage
  • Asynchronous transfer mode for parallel downloads

However, the BITS service must be running on your system.

Read How to Create a File in PowerShell if it Doesn’t Exist?

Invoke-WebRequest Vs. System.Net.WebClient Vs. Start-BitsTransfer

Here’s a comparison table to help you decide which method to use:

Method Ease of Use Supports Authentication Handles Large Files Automatic Resume
Invoke-WebRequest Easy Yes No No
Start-BitsTransfer Moderate No Yes Yes
System.Net.WebClient Moderate Yes No No

Conclusion

In this tutorial, we covered three different ways to download a file from a URL using PowerShell:

  1. Invoke-WebRequest
  2. System.Net.WebClient
  3. Start-BitsTransfer

I have also provided examples for each method. I hope you found this tutorial helpful for understanding how to use PowerShell to download files from the web. Let me know if you have any other questions!

You may also like:

  • PowerShell Get-date Add Days Examples
  • How to Create a Log File using PowerShell?
  • How to Compare Dates in PowerShell?
  • Append Text to Files in PowerShell

I am Bijay a Microsoft MVP (10 times – My MVP Profile) in SharePoint and have more than 17 years of expertise in SharePoint Online Office 365, SharePoint subscription edition, and SharePoint 2019/2016/2013. Currently working in my own venture TSInfo Technologies a SharePoint development, consulting, and training company. I also run the popular SharePoint website EnjoySharePoint.com

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Утилита для очистки оперативной памяти windows 10
  • Winscp windows server 2003
  • Zombie defense на windows phone
  • Asus p8h61 m lx2 драйвера windows 10 64
  • Легкий видеоредактор для windows 10