Estimating PowerShell Script Completion Time: A Step-by-Step Guide

As a regular PowerShell user, you’ve probably found yourself in a familiar scenario: running a lengthy script and either watching the screen intently or sprinkling Write-Host commands to track its progress. While this helps keep tabs on your script’s iterations, it’s far from ideal and often still requires your continuous attention.

Imagine if, instead of merely monitoring your script’s progress, you could accurately predict its completion time. This small shift in how you work could free you up to focus on other important tasks, maximizing your productivity and making your workflow more efficient.

The Traditional Approach: Write-Host for Progress Tracking

Adding Write-Host commands within your loops is a common technique to track which iteration your script is currently on. This method, while functional, keeps you tethered to your screen, waiting for the script to finish. It’s a solution that works but doesn’t optimize your time.

A Better Solution: Predicting Script Completion Time

By calculating how long a script will take to complete, you can better manage your time and workload. This method involves estimating the total runtime based on the script’s progress. Here’s a step-by-step guide to help you implement this in your PowerShell scripts.

The Script

The script below serves as a straightforward template for estimating the remaining time and calculating the finish time for any PowerShell script. This is achieved by capturing the current date and time, calculating the elapsed time since the script started, and then comparing it to the number of records yet to be processed.

As the script runs, it provides an increasingly accurate prediction of the completion time:

$exampleLoopCount = 120
$timePerLoop = 1000 # 1 second

$startTime = Get-Date
$totalRecordCount = $exampleLoopCount # This should be set to the total count of any records are actions that are being taken
for($currentIndex=0; $currentIndex -lt $totalRecordCount; $currentIndex++) {
    # Estimate time to completion
    $estimation = ''
    $now = Get-Date
    if ($currentIndex -gt 0) {
        $elapsed = $now - $startTime # how much time has been spent
        $average = $elapsed.TotalSeconds / $currentIndex # how many seconds per site
        $totalSecondsToGo = ($totalRecordCount - $currentIndex) * $average # seconds left
        $span = New-TimeSpan -Seconds $totalSecondsToGo # time left
        $estimatedCompletion = $now + $span # when it will be complete
        $estimation = $estimatedCompletion.ToString() # readable estimation
    }

    # Do whatever you need to do
    Start-Sleep -Milliseconds $timePerLoop

    # Show a progress bar and estimated time to completion
    if ($currentIndex -gt 0) {
        Write-Progress -Id 0 `
            -Activity "Retrieving data - Est. Completion - $($estimation)" `
            -Status "$currentIndex of $exampleLoopCount" `
            -PercentComplete (($currentIndex / $totalRecordCount) * 100)
    }
}

# Clear the progress bar
Write-Progress -Id 0 -Activity " " -Status " " -Completed

Write-Information "Script Completed"

Running the script will show something like this in Visual Studio –

Whereas in the PowerShell IDE, you’ll see the following –

Transitioning from traditional progress tracking methods to estimating script completion time can significantly enhance your PowerShell scripting experience. By implementing these techniques, you can reclaim valuable time, reduce the need for constant monitoring, and improve your overall productivity. Try incorporating these methods into your next PowerShell project and see the difference it makes in your workflow.

Feel free to share your experiences and tips below, and let’s continue to optimize our scripting practices together!

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.