PowerShell: Copy files with BITS [Quick Guide]

1:59 pm February 7, 20177794

I. introduction

BITS ( Background Intelligent Transfer Service ) is a Windows component that allows you to smart data transfer in the background , it offers interesting possibilities compared to a mere copy, including:

  • the ability to pause and resume a download (even in the case of mains)-as can afford excellent Robocopy
  • the ability to manage that bandwidth priority that will allocate over network activity of the machine on which it is running (parameter “ Priority ‘ of the commandlet ‘ Start-BitsTransfer ” ‘ “)
  • a real job management, which may take place in synchronous or asynchronous mode

is not a new component included by Microsoft recently as there are for Windows XP but it for evolved over versions of Windows (via bitsadmin.exe), PowerShell module as for him is present since PowerShell 2.0.

BITS for its own named PowerShell module ‘ BitsTransfer “and allows to manage jobs via the command line, which is interesting for script, especially to transfer big files where the probability that a break occurs is higher.

Beyond of PowerShell, BITS is used by WSUS, Windows Update, or even Microsoft Virtual Machine Converter.

In this tutorial, we will see together how to download files with BITS via PowerShell , then end of the article I’ll refer you to a script that I published on GitHub and relies on BITS. The goal of the script that I share is the ability to download a tree view of data from a folder root (source), because BITS does not natively include this notion of recursion. It is able to download all of the files in a folder, but it won’t see in subfolders.

II. import the module BitsTransfer

If you’re familiar with PowerShell, a module import must be child’s play for you, this is still the command that allows you to import the BitsTransfer module:

 Import - Module BitsTransfer 

then we can list the orders of the BitsTransfer module:

 Get-Command - Module BitsTransfer CommandType Name Source Version - - --Cmdlet Add-BitsFile BitsTransfer Cmdlet Complete-BitsTransfer BitsTransfer Cmdlet Get-BitsTransfer BitsTransfer Cmdlet Remove-BitsTransfer BitsTransfer Cmdlet summary-BitsTransfe r BitsTransfer Cmdlet Set-BitsTransfer BitsTransfer Cmdlet Start-BitsTransfer BitsTransfer Cmdlet suspends-BitsTransfer BitsTransfer 

at the time where I write these lines, the BitsTransfer module is in version 2.0 and it includes 8 commandlets which will allow to create jobs with BITS and managing them. In this tutorial, we will use all commandlets but only: Get-BitsTransfer, Complete-BitsTransfer, Start-BitsTransfer.

Note: The commandlets “ Suspend-BitsTransfer ” and “ Summary-BitsTransfer ‘ allow respectively to suspend a BITS job or resume a suspended BITS job.

To get help on the various commandlets of the module: Help BitsTransfer

III. Download a file with bit, via HTTP (S)

turn to something more interesting: downloading a file through a BITS job. It will rely on the “Start-BitsTransfer” commandlet, and two parameters: the source and destination. This is a bare minimum to use this commandlet which offers many other possibilities.

To download a file from the URL “ https://owncloud.mon-domaine.fr/index.php/s/xiiI7SmWQzsGV5G/download ‘ and store it in” C:temp ‘ giving him like name “ my – fichier.iso.gz ” here is the command to run:

 Start-BitsTransfer - Source "https://owncloud.mon-domaine.fr/index.php/s/xiiI7SmWQzsGV5G/download" - Destination "C:tempmon-fichier.iso.gz"

Une progress bar appears:

during a download ” , there are different phases: Connecting, Transferring and Transferred (when the download is finished), you can also meet ‘ TransientError “If there is a job in error. By default, the mode of operation is synchronous implying that before continuing, the script will wait for the download to finish. To start multiple jobs in a loop on the fly, he will spend in asynchronous mode, that’s what we’ll see now.

III. asynchronous download with BITS, via HTTP (S)

the passage in asynchronous mode means that indicate the parameter “-Asynchronous” to run that one previously, which will give:

 Start-BitsTransfer - Source "https://owncloud.mon-domaine.fr/index.php/s/xiiI7SmWQzsGV5G/download"-"C:tempmon-fichier.iso.gz" - Asynchronous Destination 

this command returns:

 JobId DisplayName TransferType JobState OwnerAccount -----BITS Transfer Download Connecting FLOSTATIONFlorian 3a381ff9-2bff-4081-bbff-63eb17abf976 

Note : You’ll notice in the output of command above, the value “TransferType’ to ‘Download ‘. This is logical, because BITS also allows jobs to upload, for this you need to use the parameter “TransferType” with Start-BitsTransfer.

We can see the evolution of our BITS job by invoking regularly “Get-BitsTransfer”:

when one is in asynchronous mode, it must validate the end of the BITS job to ensure that the file is saved on your machine at the place indicated in the parameter “Destination.” To validate all jobs, use the following command:

 Get-BitsTransfer | Complete-BitsTransfer 

we could also specify only the job that we have just launched, either by the JobId or its DisplayName, which would give:

 Get-BitsTransfer - JobId "3a381ff9-2bff-4081-bbff-63eb17abf976" | Complete-BitsTransfer 

from this moment there (except if there was an error), the file is saved on your machine.

Now for the latest example of this tutorial…

IV. download a set of files with BITS, via SMB

download files with BITS does not stop at the web link, I would say that it will be more used with transfers of files via SMB links to access shares.

In General, the command Start-BitsTransfer is used like this. For example, to download all files in a folder from a UNC path:

 Start-BitsTransfer - Source "\*.*" - Destination "C:temp"

Dans the destination, the files will retain their original names. But then, you tell me how do you authenticate on a share where it is needed? Microsoft for planned the coup by incorporating a “Credential” parameter that can receive an object of type PSCredential with a login/password pair to authenticate to the Start-BitsTransfer commandlet.

What gives (for example):

 $MyCred = Get-Credential 
 Start-BitsTransfer - Source "\*.*" - Destination "C:temp" - Credential $MyCred 

I’m done demonstrations related to BITS, feel free to explore its various settings and commandlets, notably the gestion of the priority for a job, the management of the credentials, the ability to bypass a proxy, etc…

For those interested, here is the script that I published on GitHub: Start-BitsDownloadRecursive

-> If you source a tree with a root folder, files, and subfolders with of other files, these subfolders may contain other folders with them also to files, etc… Everything will be recreated in the destination with the same tree and same file names, relying on BITS. A BITS job by subfolder will be generated:

Besides this may be improved (or optimized) and script of the added features, so feel free to give your opinion 🙂