Last active
June 20, 2019 10:42
-
-
Save michaelvdnest/e609bf97cd6fddc89a0026c8be6325f8 to your computer and use it in GitHub Desktop.
A PowerShell function that displays a progress bar with ETA.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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