powershellpowershell-core

write-progress not working with multiple progress activities in PS 7.x


This script works correctly in Powershell 5.1, but not in 7.4.7. In 7, it only shows the progress indicator for the first activity, but not the second. Can anyone see how I can fix that, or is it a pwsh bug?

$numOps = 40
$progressPerOp = 100 / $numOps # 100/total number of operations
$progressOpsComplete = 0
for ($i = 1; $i -le $numOps; $i++) {
    $percent = $progressPerOp * $progressOpsComplete
    Write-Progress -Activity "testing progress indicator" -Status "Processing...$percent%" -PercentComplete ($progressPerOp * $progressOpsComplete)
    Start-Sleep -Milliseconds 50
    $progressOpsComplete++
}
$percent = $progressPerOp * $progressOpsComplete
Write-Progress -Activity "testing progress indicator" -Status "Processing...$percent%" -PercentComplete ($progressPerOp * $progressOpsComplete)
Write-Progress -Activity "testing progress indicator" -Completed
write-host "`$progressPerOp:$progressPerOp  `$progressOpsComplete:$progressOpsComplete"
Write-Progress -Activity "waiting 10 seconds" -Status "waiting"
sleep 6
Write-Progress -Activity "waiting 10 seconds" -Completed

Solution

  • The fix to the issue / workaround, can be adding a small timer in between activities, i.e.:

    # ... same code as before
    Write-Progress -Activity "testing progress indicator" -Completed
    Start-Sleep -Milliseconds 200 # <- Solves the issue
    write-host "`$progressPerOp:$progressPerOp  `$progressOpsComplete:$progressOpsComplete"
    Write-Progress -Activity "waiting 10 seconds" -Status "waiting"
    

    As for why it happens, not sure if this is a known limitation with Write-Progress in PowerShell 7+ or a documented bug, they have changed the way progress looks in this version. There are a few issues that seem closely related to yours: