Using Start-BitsTransfer


New Year, New Posts. So first, Happy New Year! and Second, welcome to my 4th year of producing PowerShell Tips.

START-BITSTRANSFER

For this tip we will explore the Start-BitsTransfer cmdlet to see how useful it is. First, what is Bits? Well, technically, its BITS and it stands for Background Intelligent Transfer Service, which is a Microsoft technology. You can read about it more here:

Background Intelligent Transfer Service

What it allows us to do is to transfer files from locations that we have access to and ‘download’ those files to a location of our choice, that we also need access to. If we review the Get-Help for the Start-BitsTransfer cmdlet we see there are no examples and the Get-Help -Online for the cmdlet also fails to pull up the proper Online Help page for Start-BitsTransfer cmdlet. However, we can find the help using our favorite Search engine which brings up this page:

https://docs.microsoft.com/en-us/powershell/module/bitstransfer/start-bitstransfer?view=win10-ps

We have a proper example on the page that looks like this:
[sourcecode language=”powershell”]
Start-BitsTransfer -Source "http://server01/servertestdir/testfile1.txt" -Destination "c:\clienttestdir\testfile1.txt"
[/sourcecode]
We can see a list of parameters available to the cmdlet as well:

Use Case Scenarios for Start-BitsTransfer:

EXAMPLES

Example #1
In most cases, the easiest way to use the cmdlet is to use the Source and Destination parameters for the cmdlet. Examples:
[sourcecode language=”powershell”]
Start-BitsTransfer -Source "<SourceFile>" -Destination "<DownloadFile>"
[/sourcecode]
Now, let’s say our source was a Microsoft update and our destination was a local server share, then we would construct the one-liner like this:
.NET download location – https://download.visualstudio.microsoft.com/download/pr/7afca223-55d2-470a-8edc-6a1739ae3252/abd170b4b0ec15ad0222a809b761a036/ndp48-x86-x64-allos-enu.exe”
[sourcecode language=”powershell”]
$SourceFile = "https://download.visualstudio.microsoft.com/download/pr/7afca223-55d2-470a-8edc-6a1739ae3252/abd170b4b0ec15ad0222a809b761a036/ndp48-x86-x64-allos-enu.exe"
$DownloadPath = "\\fs01\Downloads\Updates"
Start-BitsTransfer -Source "$SourceFile" -Destination "$DownloadPath"
[/sourcecode]
Start-BitsTransfer does not provide much feedback, unless we add that code to a script.
Example #2
For this example, we will download a file from GitHub, only to find that is fails. With a straight download, we see something like this:


Now, the solution is to use a switch called ‘Dynamic’ which was discovered in this thread here:

https://powershell.org/forums/topic/bits-transfer-with-github/

By using that switch, no more errors occur. Below is a real code section that was created for downloaded an Exchange Health Check script:
[sourcecode language=”powershell”]
# HealthChecker Script – Latest Version
$Repo = "dpaulson45/HealthChecker"
$File = "HealthChecker.ps1"
$CurrentPath = (Get-Item -Path ".\" -Verbose).FullName
$Releases = "https://api.github.com/repos/$repo/releases"
$tag = (Invoke-WebRequest $releases | ConvertFrom-Json)[0].tag_name
$download = "https://github.com/$repo/releases/download/$tag/$file"
Try {
Start-BitsTransfer -Source "$Download" -Destination "$CurrentPath" -dynamic -ErrorAction STOP
} Catch {
Write-Host " * Download of $Tag of HealthChecker failed" -ForegroundColor Red
Write-Host " – Using previous version of HealthChecker script." -ForegroundColor Yellow
}
[/sourcecode]
Now we have a script to download the file to a local path.

NEXT STEPS

Look out for the next post where we can dive into other BitsTransfer cmdlets. There are quite a few cmdlets that involve the BITS technology. See you next time!

FURTHER READING

I cover this and many more topics in this book:

Notes from the Field: A PowerShell Primer

Related Post

Compare-ObjectCompare-Object

Depending on what tasks you perform with PowerShell, you may never need Compare-Object. However, when you need it, you will find it immensely helpful and problematic at the same time.