#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

How To: Add Progress Bar To Your PowerShell Script

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...
}

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 )

Google photo

You are commenting using your Google 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