Use PowerShell to measure the time to download a file

tree 7

It is actually surprisingly easy to measure how long it takes to download a file with PowerShell. Let’s look at an example: My internet speed is according to around 4.2 Mbps (megabits/second). If I would like to know how long it takes me to download a file of 1GB I can use a simple one liner:


How does that actually work?

  1. Divide the size of the files in bytes (1GB/)
  2. by the download speed/second in bytes (*1MB). First converting Mb into MB (4.2/8)
  3. The results is the time it takes to download the specified size in decimal seconds
  4. The result is used to create a new TimeSpan object (around 32 minutes to download 1GB on my PC)

It doesn’t take much more effort to turn this into a re-usable function:

function Get-DownloadTime($SizeInBytes,$SpeedInMbps,[switch]$AsTimeSpan){
    $ts = [TimeSpan]::FromSeconds($SizeInBytes/($SpeedInMbps/8*1MB))
    if ($AsTimeSpan){
Get-DownloadTime 1GB 4.2
Get-DownloadTime 1GB 4.2 -AsTimeSpan

The function takes the size in bytes and the speed in Mbps and returns by default the time in the format “hh:mm:ss” or the TimeSpan object when used with the -AsTimeSpan switch. More details on the TimeSpan format specifiers can be found here.


photo credit: Pine Forest Colorful Tree via photopin (license)