Chapters/scripting-at-scale/progress-samples.ps1
#Write-Progress demonstrations return "This is a demo script file." 1..50 | ForEach-Object { Write-Progress -Activity "My main activity" -Status "Status here" -CurrentOperation "Working on $_" Start-Sleep -Milliseconds 100 } Get-Process | Where-Object starttime | ForEach-Object { Write-Progress -Activity "Get-Process" -status "Calculating process run time" ` -CurrentOperation "process: $($_.name)" $_ | Select-Object ID, Name, StartTime, @{Name = "Runtime"; Expression = {(Get-Date) - $_.starttime}} Start-Sleep -Milliseconds 50 } #calculating time $i = 20 1..$i | ForEach-Object -Begin { [int]$seconds = 21 } -process { Write-Progress -Activity "My main activity" -Status "Calculating square roots" ` -CurrentOperation "processing: $_" -SecondsRemaining $seconds [math]::Sqrt($_) Start-Sleep -Seconds 1 $seconds -= 1 } #calculating a percent #you need to know in advance $i = 20 1..$i | ForEach-Object -Begin { [int]$count = 0 } -process { #calculate percent complete $count++ $pct = ($count/$i) * 100 Write-Progress -Activity "My main activity" -Status "Calculating square roots" ` -CurrentOperation "processing: $_" -PercentComplete $pct [math]::Sqrt($_) Start-Sleep -Milliseconds 200 } |