Skip to content

Instantly share code, notes, and snippets.

@WimObiwan
Last active December 16, 2019 09:00
Show Gist options
  • Save WimObiwan/3371953841219dd4a0c7084ec1929a59 to your computer and use it in GitHub Desktop.
Save WimObiwan/3371953841219dd4a0c7084ec1929a59 to your computer and use it in GitHub Desktop.
Get-WithProgress.ps1
function Get-WithProgress {
param(
[string] $Activity = 'Processing',
[int] $Loop = 0
)
# v1.0 Initial version
# v1.1 Only increase progress AFTER processing an item
# Collect the pipeline input ($Input) up front in an array,
# using @(...) to force enumeration.
$a = @($Input)
# Count the number of input objects.
$count = $a.Count
if ($Loop -le 0) {
if ($count -gt 0) {
$Loop = [Math]::Pow(10, [Math]::Max(0, [Math]::Floor([Math]::Log10($count) - 1.5)))
} else {
$Loop = 1
}
}
$i = 0
# Process the input objects one by one, with progress display.
$a | ForEach-Object {
if ($i % $Loop -eq 0) {
$prc = $i * 100 / $count
Write-Progress -PercentComplete $prc -Activity $Activity -Status "$i/$count processed"
}
$_
++$i
}
Write-Progress -Completed -Activity $Activity
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment