Update-Progress

The Write-Progress command is a very useful operator interface for PowerShell. It displays the progress of a longer running script without polluting the standard output.

Most examples for the progress bar are used with a ForEach loop. The total array size is counted and each loop increments the bar.
For linear start to finish scripts I wanted a function that I could call that would handle the percentage calculations and be easy to make changes to the main script. With the Update-Progress function simply call the function each time a significant part of your script has completed and it will handle all of the progress bar control.

So how does it work?
On first run the function reads the script it is embedded in using Get-Content. This is saved into an array and a ForEach counts how many times it has been called. This is so the percentage to increment the progress bar can be calculated.

$ContentArray = Get-Content $script:MyInvocation.MyCommand.Path
Foreach ($line in $ContentArray)  
{
    If ($line -like "*Update-Progress*")
    {
        $Global:ProgressTotal ++
    }
}

Then each time the script is called the Write-Progress is updated by one count:

$Global:ProgressComplete = [decimal]::round($ProgressCount/$ProgressTotal*100)
If (($ProgressComplete -gt 100) -or ($ProgressComplete -lt 0))  
{
    $Global:ProgressComplete = 0
}
Write-Progress -Id 1 -Activity $Global:Activity -Status "$ProgressComplete percent complete" -PercentComplete $ProgressComplete  
$Global:ProgressCount ++

Then finally on the last call the progress bar is set to complete.

Write-Progress -Id 1 -Activity $Global:Activity -Status "100 percent complete" -PercentComplete 100  

The whole script can be found here:
https://github.com/gbuktenica/Update-Progress