PowerShell: How to efficiently download file with BitsTransfer

PowerShell: How to efficiently download file with BitsTransfer

PowerShell offers a powerful command-line tool for downloading files: the Background Intelligent Transfer Service (BITS). BITS is designed for efficient file transfers that don't interrupt network usage. This tutorial will guide you through using the Start-BitsTransfer cmdlet in PowerShell for downloading files, including handling multiple files and asynchronous downloads.

Getting Started with PowerShell

Open PowerShell: Access the PowerShell command-line interface from the Start menu or by entering powershell in the Run dialog (Win + R).

Steps to Download Files Using BitsTransfer

Downloading a Single File

Basic File Download: To download a single file, use the Start-BitsTransfer cmdlet. Replace the URL and destination path with your desired file and location.
  1. Start-BitsTransfer -Source "https://example.com/file.iso" -Destination "C:\downloads\file.iso"
Or, for a more streamlined syntax:
  1. Start-BitsTransfer "https://example.com/file.iso" "C:\downloads\file.iso"

Downloading Multiple Files

Multiple File Downloads: BITS can handle multiple file downloads simultaneously. Here's how to download two different files to specified paths:
  1. Start-BitsTransfer -Source "https://example.com/file1.iso", "https://example.com/file2.iso" -Destination "C:\downloads\file1.iso", "C:\downloads\file2.iso"

Asynchronous Download

Asynchronous Download: For non-blocking, asynchronous downloads, add the -Asynchronous parameter. This is particularly useful for large files or when performing other tasks.
  1. Start-BitsTransfer -Source "https://example.com/file.iso" -Destination "C:\downloads\file.iso" -Asynchronous

Monitoring Asynchronous Downloads

Check Download Progress: To monitor the progress of an asynchronous download, use Get-BitsTransfer.
  1. Get-BitsTransfer
Output:
  1. JobId DisplayName             TransferType           JobState           OwnerAccount
  2. ----------------------              ------------                --------              ------------
  3. 12345678-90ab-cdef-ghij-klmnopqrstuv           BITS Transfer Download           Transferring           UserName\YourAccount

Completing Asynchronous Downloads: Once the download is finished, finalize it with Complete-BitsTransfer.
  1. Get-BitsTransfer | Complete-BitsTransfer

Conclusion

This method is particularly effective for handling large file transfers, multiple files, and ensuring downloads continue without disrupting other network activities. BITS is a versatile tool for any system administrator's toolkit, enhancing file management capabilities within the PowerShell environment.