Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Last active June 20, 2019 10:42
Show Gist options
  • Save michaelvdnest/e609bf97cd6fddc89a0026c8be6325f8 to your computer and use it in GitHub Desktop.
Save michaelvdnest/e609bf97cd6fddc89a0026c8be6325f8 to your computer and use it in GitHub Desktop.
A PowerShell function that displays a progress bar with ETA.
<#
.Synopsis
Displays a progress bar with ETA
.DESCRIPTION
Long description
.EXAMPLE
Example of how to use this cmdlet
Display-Progress -Id 1 -Count $i -Total $total -Activity "Exporting $($item.TestType)"
#>
function Display-Progress
{
[CmdletBinding()]
Param
(
[int]$Id,
[DateTime]$Start,
[int]$Count,
[int]$Total,
[string]$Activity,
[double]$PercentageSubItem
)
Begin
{
}
Process
{
$prct = $Count / $Total
$elapsed = (Get-Date) - $Start
if ($PercentageSubItem) {
$prct = (($Count - 1) / $Total) + ((1 / $Total ) * $PercentageSubItem)
}
$totalTime = ($elapsed.TotalSeconds) / $prct
$remain = $totalTime - $elapsed.TotalSeconds
$eta = (Get-Date).AddSeconds($remain)
$status = ("$($prct.ToString('P')) % ($Count/$total) {0:dd\.hh\:mm\:ss} eta $eta" -f (New-TimeSpan -seconds $remain))
Write-Progress -Id $Id -Activity $Activity -Status $status -PercentComplete ($prct * 100)
}
End
{
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment