PowerShell, References, Scripting
comments 4

Using Write-Progress cmdlet with BITS file transfers

As you may know already, I released my first PowerPack for PowerGUI sometime last week. I am currently looking at various other features I want to add to this powerpack. One such feature is displaying progress information for each file transfer. While trying to figure out how I can implement this within PowerGUI Admin console, I came across a nice cmdlet Write-Progress which is used to show progress bars. Though this is a nice cmdliet, I can not directly use this in the powerpack.

However, I want to show how to use Write-Progress cmdlet with BITS file transfer.

To load BITS file transfer PowerShell cmdlets

Import-Module BitsTransfer

To start File Transfer

$bitsJob = Start-BitsTransfer -source http://download.microsoft.com/download/7/8/A/78ACCE4D-D1DD-4E54-80DA-928DCFD3AD6A/OfficeServer.exe -destination “C:\SharePoint” -Asynchronous

To view the progress,

$bitsJob | ForEach-Object {
 while ($_.JobState -eq “Transferring”) {
  $pctComplete = [int](($_.BytesTransferred * 100)/$_.BytesTotal);
  clear-host;
  write-progress -activity “File Transfer in Progress” -status “% Complete: $pctComplete” -percentcomplete $pctComplete
  sleep 10;
 }
}

 This is it. You will see a progress bar at the top of PowerShell window as shown here.

Progress bar

Progress bar

If you close and open the PowerShell window, you will have to import Bits PowerShell module again and use the below command to get an instance of Bits file transfer Job

$bitJob = Get-BitsTransfer

You can now use the Write-Progress sample code as given above to monitor the progress.

Filed under: PowerShell, References, Scripting

by

Ravikanth is a principal engineer and the lead architect for Microsoft and VMware virtualized and hybrid cloud solutions within the Infrastructure Solutions Group at Dell EMC. He is a multi-year recipient of Microsoft Most Valuable Professional (MVP) award in Windows PowerShell (CDM) and Microsoft Azure. Ravikanth is the author of Windows PowerShell Desired State Configuration Revealed (Apress) and leads Bangalore PowerShell and Bangalore IT Pro user groups. He can be seen speaking regularly at local user group events and conferences in India and abroad.