PowerShell function to calculate data transfer time

This PowerShell function will calculate how long it will take to transfer a given number of bytes at a given speed (in bits per second). Optionally, you can specify transfer overhead, which defaults to ten percent.

Example Usage

PS C:\> Get-TransferTime -FileSize 4.4gb -Speed 2mb


Days         : 0
Hours        : 5
Minutes      : 0
Seconds      : 22
TotalDays    : 0.208587962962963
TotalHours   : 5.00611111111111
TotalMinutes : 300.366666666667
TotalSeconds : 18022

The Function

<#
.Synopsis
Data transfer time calculation.
.DESCRIPTION
Calculate how long it would take to transfer a given number of bytes at a given speed.
.PARAMETER FileSize
File Size in bytes. Can specify with GB, MB, etc.
.PARAMETER Speed
Transfer speed in bits. Can specify with GB, MB, etc.
.PARAMETER OverheadPercent
Transfer overhead percent. Defaults to 10.
.EXAMPLE
Get-TransferTime -FileSize 4.4gb -Speed 2mb
.EXAMPLE
Get-TransferTime -FileSize 50mb -Speed 10mb -OverheadPercent 6
.NOTES
Ken Bradley, 2015-12-10
#>
function Get-TransferTime
{
    [CmdletBinding()]
    [Alias()]
    [OutputType([int])]
    Param
    (
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=0)]
        [double]$FileSize, # bytes, can specify GB, MB, etc

        #Speed help!
        [Parameter(Mandatory=$true,
                   ValueFromPipelineByPropertyName=$true,
                   Position=1)]
        [double]$Speed, # bits, can specify GB, MB, etc

        [Parameter(Mandatory=$false,
                   ValueFromPipelineByPropertyName=$true,
                   Position=2)]
        [double]$OverheadPercent=10
        )

    # Scale percent so 100% = 1.0
    $ScaledPercent = 1 + ($OverheadPercent / 100)
    # Calculate seconds
    $Result = ($FileSize * 8 * $ScaledPercent) / $Speed

    # Output the result nicely formated
    New-TimeSpan -Seconds $Result | Format-List -Property Days, Hours, Minutes, Seconds, TotalDays, TotalHours, TotalMinutes, TotalSeconds
    }

[EDIT: Previously, there was an error with OverheadPercent being excluded from the calculation. It has been added back. Thanks to JG for catching this.]

How to output objects from your own PowerShell functions

This example of PowerShell code shows how to output objects from your own functions. The basic tasks you do to accomplish this in your function are:

  1. Assemble the values into a hash table
  2. Create an object using that hash table
  3. Output the object

Once you do this, then you can treat your output as you would any other object in PowerShell.

  1. Assemble the values into a hash table
    • See my example below, it is the lines like $Properties =  @{…}
    • More info at about_Splatting
  2. Create an object using that hash table
    • See my example, the line starting with $Object = New-Object…
  3. Output the object
    • Simply Write-Output $Object
# Example Function
function Get-SystemInfo {

    # Prepare the info
    $OS = Get-CimInstance -ClassName Win32_OperatingSystem # for FreePhysicalMemory, Caption, PSComputerName, SystemDrive
    $LoadPercentage = (Get-CimInstance -ClassName Win32_Processor).LoadPercentage
    $ProcessCount = (Get-Process).Count
    $SystemDrive = Get-CimInstance -ClassName Win32_LogicalDisk -filter "Name = '$($OS.SystemDrive)'" 
    $ComputerSystem = Get-CimInstance -ClassName Win32_ComputerSystem

    # Prepare the properties in a hash table
    $Properties = @{
        ComputerName = $ComputerSystem.DNSHostName;
        Domain = $ComputerSystem.Domain;
        FreePhysicalMemory = "{0:n0}" -f ($OS.FreePhysicalMemory /1KB) + " MB";
        OS = $OS.Caption;
        LastBootUpTime = $OS.LastBootUpTime;
        LoadPercentage = $LoadPercentage;
        ProcessCount = $ProcessCount;
        SysVolFree = "{0:n1}" -f ($SystemDrive.FreeSpace /1GB) + " GB"
        }
    
    # Create an object using the properties
    $Object = New-Object -TypeName PSObject -Property $Properties

    # Output the object
    Write-Output $Object     
    }

# Example Usage

# Using the results, put them into an object "$Result"
$Result = Get-SystemInfo

# Access the values inside the object
$Result.LastBootUpTime