Copying Large Files Using BITS and PowerShell

As a rule, in local (and global) networks files are transferred between the systems using SMB, FTP or HTTP protocols. The problem of all these protocols is some troubles with resuming the download of the large files, which can become even worse if the channel is slow or unstable. Moreover, when copying files using these protocols the whole bandwidth between a server and a recipient is used, which may negatively affect the network performance and the work of other apps. (It is not always possible to configure QoS policies as necessary.) In this article, we’ll consider how to use BITS protocol and Powershell to copy large files if the channel is slow or unstable.

BITS Protocol

BITS (Background Intelligent Transfer service) is a Windows service used to transfer files between the systems. Windows Update (including WSUS) and SCCM distribution points transfer files to the target computers using this protocol.

Using BITS protocol, you can transfer files between computers (either download or upload files).

The Advantages of Using BITS Protocol:

BITS is the intelligent protocol able to control the used bandwidth not to affect the work of other network apps. BITS can use only the free band and dynamically change the data rate during the transfer (if other applications increase the network use)

A BITS task will be automatically resumed in case of disruptions or computer restarts

A file can be downloaded in the background, unnoticed by a user

The recipient side and the server side do not require a deployed IIS server

Thus, BITS is a preferable protocol to transfer large files in slow networks.

Note. Robocopy.exe utility allows to resume file copying if the connection has been broken.

OS and Powershell Version Requirements

BITS protocol appeared in Windows XP, in which bitsadmin.exe utility could be used to manage BITS tasks. The utility is still supported, but is deprecated. To manage BITS jobs, it is preferable to use special Powershell cmdlets.

To work under this scenario, we need Windows Vista or Windows Server 2008 or higher and Powershell 2.0 or higher.

Tip. It is possible to use Windows Server 2003 as well. In this case you will have to install KB 923845 update and PowerShell 2.0

BITS support is required on the side of both client and server.

How to Download a File Using BITS Protocol using Powershell

Suppose that we have to download a file stored in HTTP on the IIS server (http://10.1.1.18/erd65_32.iso). It is supposed that anonymous access to the server is possible. (Later we’ll consider access with authentication).

First of all, import BITS module in your Powershell session:

Import-Module BitsTransfer

After the module is imported, the list of all available commands can be displayed as follows:

Synchronous BITS File Transfer

The message “This is a file transfer that uses the Background Intelligent Transfer service (BITS)” means that the specified file is being downloaded using BITS.

In this case the cmdlet has started the download in a synchronous mode. The download is similar to a common process of file copying, with a progress bar displayed on the screen showing the download status. If a computer is restarted, the download won’t be resumed.

Asynchronous BITS File Transfer

BITS download can be started in the asynchronous mode as well. To do it, add –asynchronous parameter to the command shown above. In this mode, if something happens (server or client restart, connection disruption, etc.), the task will automatically resume and the download will be continue.

Important. By default, Start-BitsTransfer runs with Foreground priority (the highest possible one). It is supposed that the download started in this mode will compete with other processes for the channel bandwidth. To avoid it, you have to explicitly set any other priority as an argument of the command, e. g., -Priority low

In this case, the file transfer process is not displayed on the screen. The task status can be obtained using Get-BitsTransfer cmdlet:

Get-BitsTransfer | fl

The command returns the transfer status (in this case the transfer is over: Transferred), the info about the number of bytes transferred, the time the file has been created and the time the task is completed.

When using the asynchronous transfer mode, a temporary file with TMP extension is created (by default, it is hidden in Windows Explorer). To convert it into the source file, run the command Complete-BitsTransfer:

Get-BitsTransfer | Complete-BitsTransfer

After that, the BITS download task is considered to be completed and is removed from the list of jobs.

If the server, where the file is stored, requires user authentication, the following command will allow to show a window to enter user data and access the resource:

To make it easier to track the results of a BITS task, you can use a simple script, which allows to track the task progress and displays the download percentage on the screen. After the download is over, the script converts the file into the source format:

Thus, using BITS features is a great alternative to the common file copying using SMB protocol. BITS file transfer task is performed despite of connection disruptions or computer restarts, and doesn’t much occupy the bandwidth without interfering with other apps. BITS will be the best solution to transfer ISO files and copying virtual machine files.