If you need to download (transfer) a large file to a remote computer without overloading the network channel, we recommend using the BITS protocol. BITS (Background Intelligent Transfer service) is a built-in Windows service for transferring files between computers that supports: asynchronous transfers, can run in the background, can resume interrupted downloads, and can dynamically change download/upload speeds to avoid overloading the network connection. Thus, BITS allows you to efficiently transfer large files over an unstable or slow network without affecting the performance of other network apps. In this article, we’ll look at how to use PowerShell to download and transfer big files between computers using the BITS protocol.
How to Download Files with PowerShell and BitsTransfer Module
To manage BITS jobs on Windows, you can use the built-in PowerShell BitsTransfer module. Download an ISO image file from the IIS web server using the BITS protocol:
Start-BitsTransfer –source http://testsite.woshub.loc/netinst.iso -destination c:\temp
The BITS client synchronously downloads the file and saves it to the specified local directory. A bar will appear on the screen to indicate the download progress. The file transfer will not resume if a connection is lost (you will need to download the whole file again). Transferring a file in this mode is similar to downloading a file from a website using Invoke-WebRequest.
Asynchronous Resumable File Transfers via BITS
To download files via BITS in asynchronous mode (supporting resumed downloads), you need to add the -asynchronous parameter
Start-BitsTransfer –source http://testsite.woshub.loc/netinst.iso -destination c:\temp -asynchronous
If the connection is lost or the source or destination reboots, the asynchronous BITS job will automatically resume.
By default, the BITS job will use all of the available bandwidth on the network channel (foreground mode). To make the BITS job use only unused network link bandwidth, add the -Priority low parameter. In this case, the BITS download task will not compete for the transmission channel bandwidth with any other network service.
Managing BITS File Transfer Jobs with PowerShell
To check the status of background BITS download jobs, run:
Get-BitsTransfer | fl
The command returns the file transfer status, information on the number of bytes transferred, the total file size, the creation time, and the completion of the BITS job.
View the status of all BITS background jobs on your computer:
Get-BitsTransfer | select DisplayName, BytesTotal, BytesTransferred, JobState | Format-Table -AutoSize
You need to run the following command after the BITS file download job has finished:
Get-BitsTransfer| where JobState -eq 'Transferred'| Complete-BitsTransfer
If you do not do this, the BITS client will not automatically rename the temporary file with a .TMP
extension in the destination directory to its original filename.
BITS allows you to transfer files not only over HTTP, but also over the standard Windows SMB file-sharing protocol. If you want to copy a file to the C$ admin share on a remote computer or to a shared network folder, you will need to specify the UNC path:
Start-BitsTransfer -Source C:\iso\win2022.iso -Destination \\man-srv01\c$\iso -Asynchronous -DisplayName CopyISOtoMan
The BITS job can be paused:
Get-BitsTransfer -Name CopyISOtoMan | Suspend-BitsTransfer
Resume file download:
Get-BitsTransfer -Name CopyISOtoMan | Resume-BitsTransfer -Asynchronous
If you need to authenticate to a remote server to access a file:
Start-BitsTransfer -source http://10.1.1.18/erd65_32.iso -destination c:\temp -asynchronous -Priority low -Authentication NTLM -Credential Get-Credential
To track the status of a large file download job, use the following PowerShell script. The percentage of completion of the download will be displayed on the screen, and when the task is complete, the TMP file will be renamed back to its original name.
Import-Module BitsTransfer
$bitsjob = Start-BitsTransfer -Source http://10.1.1.18/erd65_32.iso -Destination c:\temp -Asynchronous
while( ($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting') )
{
Write-host $bitsjob.JobState.ToString()
$Proc = ($bitsjob.BytesTransferred / $bitsjob.BytesTotal) * 100
Write-Host $Proc “%”
Sleep 3
}
Complete-BitsTransfer -BitsJob $bitsjob
The BITS protocol is widely used for background file transfers on Windows networks. This protocol is used to download Windows Update (including updates from a WSUS server). The BITS protocol can be the optimal solution for transferring large ISO image and virtual machine files (VMDK, VHDX) over WAN networks.
2 comments
do you know how to have bits ignored self signed certs ?
One item that you don’t mention, and that my Google-Fu is not strong enough to find, is does BitsTransfer verify the remote file when you’re copying ‘to’ a network/remote location?.. alongside that question, would I be correct to presume that if it did, it would effectively ‘double’ the data transfer between the source & destination?