Skip to content

Instantly share code, notes, and snippets.

@michaelvdnest
Last active April 28, 2022 07:00
Show Gist options
  • Save michaelvdnest/e2eff62fd1f4a8e9ddeb1933576c0dc0 to your computer and use it in GitHub Desktop.
Save michaelvdnest/e2eff62fd1f4a8e9ddeb1933576c0dc0 to your computer and use it in GitHub Desktop.
An example of calculating ETA for a PowerShell progress bar.
# Data Source
$items = 1..85
# Loop
$items | ForEach-Object `
-Begin {
# Initialize Tracking
$total = $items.Count
$start = Get-Date
$i = 0
} `
-Process {
# Progress Tracking
$i++
$prct = $i / $total
$elapsed = (Get-Date) - $start
$totalTime = ($elapsed.TotalSeconds) / $prct
$remain = $totalTime - $elapsed.TotalSeconds
$eta = (Get-Date).AddSeconds($remain)
# Display
$activity = "Testing ETA"
$status = ("$($prct.ToString('P')) % ($i/$total) {0:dd\.hh\:mm\:ss} eta $eta" -f (New-TimeSpan -seconds $remain))
Write-Progress -Activity $activity -Status $status -PercentComplete ($prct * 100)
# Operation
Start-Sleep -Milliseconds (Get-Random -Minimum 100 -Maximum 500)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment