Using BITS with PowerShell is a great way to copy files because you get the best of both worlds. BITS doesn't care if the network connection drops or if the machine restarts, it will just wait patiently until it can connect again and start from where it left off.

You can leave the job and know that the file will get there in the end. This is especially useful if you have to regularly upload logs from a client/laptop because the job will just keep going until the file is transferred.

By leveraging BITS with PowerShell, you're able to seamlessly integrate BITS transfers within your PowerShell scripts which makes for a pretty powerful combination!

Creating a BITS job with PowerShell

To create a BITS job with PowerShell you'll need to use the Start-BITSTransfer cmdlet and give it the source and destination. This will create the job and start copying the file right away.

PS51> Start-BitsTransfer -Source C:\file.log -Destination \\server\share\logfiles -Asynchronous

The Asynchronous parameter allows it run in the background so you don't need to watch the progress bar. You can leave this off if you want to watch it run, but it's perfect for when you are scheduling a copy job, for example.

If you are copying lots of files it's worth thinking about the DisplayName parameter so that you can spot the job in the list.

PS51> Start-BitsTransfer -Source C:\file.log -Destination \\server\share\logfiles -Asynchronous -DisplayName SnipBITS2

Finding BITS jobs with PowerShell

To find BITS jobs, use the Get-BitsTransfer PowerShell cmdlet. Notice the DisplayName property of the job is there because we specified it with the switch.

And if we pipe this into Select-Object -Property * it gives you all the attributes for the job.

Notice this shows the status of the BITS job and the BytesTotal/BytesTransferred. In this case, the BITS job has finished, but the job still exists in the list. This means that you can still see all the details of the jobs and you can even add more files to it. It will start transferring those as well. You can do this by piping the job into the Add-BitsFile PowerShell cmdlet, and specifying the files we want to add.

PS51> Get-BitsTransfer -Name SnipBITS1� | Add-BitsFile -Source C:\logs\* -Destination \\server\share\logfiles -Asynchronous

Pausing and restarting a BITS job with PowerShell

If you need to pause the job halfway through and start it up later you can use the Suspend-BitsTransfer and Resume-BitsTransfer cmdlets.

PS51> Get-BitsTransfer -Name SnipBITS2 | Suspend-BitsTransfer
PS51> Get-BitsTransfer -Name SnipBITS2 | Resume-BitsTransfer -Asynchronous

Removing BITS jobs with PowerShell

If you want to close down the jobs once they are complete, you can use the Complete-BitsTransfer cmdlet. This cmdlet will close the session which will make the transfers disappear from the list once they are complete.

Either pipe the job to it or the list of jobs.

PS51> Get-BitsTransfer | Complete-BitsTransfer
PS51> Start-BitsTransfer -Source C:\logs\* -Destination \\server\share\logfiles | Complete-BitsTransfer

Summary

If you are often uploading large log files or client logs, using PowerShell and BITS can be a great way to do this!

Join the Jar Tippers on Patreon

It takes a lot of time to write detailed blog posts like this one. In a single-income family, this blog is one way I depend on to keep the lights on. I'd be eternally grateful if you could become a Patreon patron today!

Become a Patron!