Jun 052012

Copying Files in the Background with PowerShell

In a previous tip we showed how you can use the BITS service to copy files. The main advantage of BITS is that it can copy things silently in the background, and it is resilient to interruptions such as reboots. After a reboot, BITS will continue the copy process, so this can be a good starting point to create a robust backup mechanism.


# adjust source and
destination to your needs:

if ( (Test-Path$Destination) -eq$false)

$params= @{

Start-BitsTransfer @params

This script would copy all downloaded EXE files to a backup location. Since the backup is running in the background, you get back immediately a response like this:


JobId                                       DisplayName                      TransferType                     JobState                         OwnerAccount
—–                                       ———–                      ————                     ——–                         ————
0488f61c-ab39-4661-81ce-b8f8a3… Backup                             Download                          Transferring                    TobiasAir1\Tobias 

To check progress, you can check progress using Get-BITSTransfer. You can call this cmdlet anytime, even next week after a couple of restarts. It may look like this:


PS> Get-BitsTransfer

JobId                                           DisplayName                   TransferType                     JobState                         OwnerAccount
—–                                           ———–                   ————                     ——–                         ————
287ed32a-ec3a-4e13-b88d-9f288f…    Upload TUDI
Data             Upload                             Transferred                     TobiasAir1\Tobias
546c7a07-315b-4900-b1de-134aaa…    Upload TUDI
Data             Upload                             Transferred                     TobiasAir1\Tobias
0488f61c-ab39-4661-81ce-b8f8a3…    Backup                          Download                           Transferring                   TobiasAir1\Tobias

You may see other BITS operations in this list as well since Windows is using the BITS service, too, to receive updates and upload data.

To actually complete the backup once it is done, you need to call Complete-BITSTransfer:



Where-Object { $_.DisplayName-eq‘Backup’ } |
ForEach-Object {

if ($_.JobState-eq‘Transferring’) {
of {1} files transferred. Try again later.’
, $total
else {
process done at $done”



Leave a Reply