#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

How To: Add Progress Bar To Your PowerShell Script

Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.

Ever run a script and wonder how far along it is? Why not add a Progress Bar to your script to give you a visual queue as to where in the process you currently are.

The following is a simple example of how to add a progress bar to a foreach loop.

In the following example, I will demonstrate how to get a list of ProjectWise folder objects, and simply return the folder object to the console. While it is processing the folders, the progress bar will update.

Write-Host 'Getting ProjectWise Rich Project / Work Area objects.'
$pwProjects = Get-PWFolders -Verbose

# The following is a counter for the progress bar.
$Counter = 1

# Loop through each project and display the project name.
foreach($pwProject in $pwProjects) {
    $percentComplete = $(($Counter / $pwProjects.Count) * 100 )
    $Progress = @{
        Activity = "Getting folder information for '$($pwProject.Name)'."
        Status = "Processing $Counter of $($pwProjects.Count)"
        PercentComplete = $([math]::Round($percentComplete, 2))
    }
    Write-Progress @Progress -Id 1
    # Increment the counter. 
    $Counter++
    
    # Display the Project Name
    $pwProject.Name
}

In the next example, I will demonstrate how to add a nested progress bar. I will get a list of ProjectWise Rich Project / Work Area folder objects. I will then loop through each of the folders to get any sub-folders. Again, you will see a progress bar for the initial folder objects. You will also see an additional or nested progress bar as each of the folders is processed for sub-folders.

Write-Host 'Getting ProjectWise Rich Project / Work Area objects.'
$pwProjects = Get-PWRichProjects -Verbose

# Loop thru each datasource and get desired information.
# The following is a counter for the progress bar.
$Counter = 1

foreach($pwProject in $pwProjects) {
    $percentComplete = $(($Counter / $pwProjects.Count) * 100 )
    $Progress = @{
        Activity = "Getting folder information for '$($pwProject.Name)'."
        Status = "Processing $Counter of $($pwProjects.Count)"
        PercentComplete = $([math]::Round($percentComplete, 2))
    }
    Write-Progress @Progress -CurrentOperation Projects -Id 1
    # Increment the counter for the outer loop 'Projects'. 
    $Counter++ 
    Write-Host "Getting subfolders for '$($pwProject.Name)'."
    
    $subFolders = Get-PWFolders -FolderID $pwProject.ProjectID -Slow
    
    # The following is a counter for the second progress bar. 
    $Counter_Sub = 1
    foreach ($subFolder in $subFolders) {
        $percentCompleteSub = $(($Counter_Sub / $subFolders.Count) * 100 )
        $ProgressSub = @{
            Id = 1
            Activity = "Getting sub-folder information for folder '$($subFolder.Name)'."
            Status = "Processing $Counter_Sub of $($subFolders.Count)"
            PercentComplete = $([math]::Round($percentCompleteSub, 2))
        }
        Write-Progress @ProgressSub -CurrentOperation SubFolders -Id 2

        Write-Host "Sub-folder: '$($subFolder.Name)'."

        # Increment the counter for the inner loop 'SubFolders'.
        $Counter_Sub++
    } # end foreach ($subFolder...
}

2 thoughts on “How To: Add Progress Bar To Your PowerShell Script”

  1. Hi Brian Flaherty,
    I just start to learn programming. I not sure how to add progress bar in my code.

    Click button, will run the function below but no progress bar.
    function Update.net3.5 {
    Enable-WindowsOptionalFeature -Online -FeatureName NetFx3 -Source ‘.\files\sxs’
    }
    $Update3x.Add_Click({ Update.net3.5 })

    Thanks

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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