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 speedtest.net 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:

[TimeSpan]::FromSeconds(1GB/(4.2/8*1MB))

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){
        $ts
    }
    else{
        $ts.ToString('hh\:mm\:ss')
    }
}
#usage
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.

shareThoughts


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

Advertisements

I'd love to hear what you think

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s