Skip to main content

PowerShell script to async copy a directory and contents recursively, using BITS.

##
# PowerShell script to Copy a directory and contents recusively... with BITS.
#
# Source: http://woshub.com/copying-large-files-using-bits-and-powershell/
##

Import-Module BitsTransfer

$Source="\\lond-rep01\share\"
$Destination="c:\tmp\"

$folders = Get-ChildItem -Name -Path $source -Directory -Recurse

$bitsjob = Start-BitsTransfer -Source $Source\*.* -Destination $Destination -asynchronous -Priority low
while (($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting'))
{
    Sleep 4
}
Complete-BitsTransfer -BitsJob $bitsjob

foreach ($i in $folders)
{
    $exists = Test-Path $Destination\$i
    if ($exists -eq $false)
    {
        New-Item $Destination\$i -ItemType Directory
    }

    $bitsjob = Start-BitsTransfer -Source $Source\$i\*.* -Destination $Destination\$i -asynchronous -Priority low
    while (($bitsjob.JobState.ToString() -eq 'Transferring') -or ($bitsjob.JobState.ToString() -eq 'Connecting'))
    {
        Sleep 4
    }

    Complete-BitsTransfer -BitsJob $bitsjob
}