Chapters/scripting-at-scale/progress-samples.ps1


#Write-Progress demonstrations
return "This is a demo script file."

1..50 | foreach {
 Write-progress -Activity "My main activity" -Status "Status here" -CurrentOperation "Working on $_"
 start-sleep -Milliseconds 100
}

get-process | where starttime | foreach {
 Write-Progress -Activity "Get-Process" -status "Calculating process run time" `
 -CurrentOperation "process: $($_.name)"
 $_ | select ID, Name,StartTime, @{Name="Runtime";Expression = {(get-date) - $_.starttime}}
 start-sleep -Milliseconds 50
}


#calculating time
$i = 20
1..$i | foreach -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 -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
 
}