This is the first in a series of articles covering the topic of Progress Bars.
Sometimes a script can take time to complete a task and we need a way to display this in our GUI application. We can add a visual aid to help us see the progress of our script.
There are a number of ways to accomplish this:
- Write the results to a text file.
- Write the results to a database table.
- Display the results in a text or label box in our GUI form.
You can also use the available progress control objects and script commands in your solution:
- Progress Bar Control
- Progress Bar Overlay Control
- ToolStrip Control
- StatusStrip Control
- Write-Progress (non-graphical progress bar for console scripting)
All of these controls enable the GUI application to display the progress of the script during execution.
Once you learn to use one, it is easy to use the others since they have most of the same .NET properties and methods for handling the progress bar animation.
To learn more about these properties and methods, check out the following article on the ProgressBar Control.
Progress Bar Display Options
The following images show some options for using the progress bar controls. These can be the starting point for building either simple or complex forms. The complexity is up to you—be creative!
Using the “Progress Bar” control in it’s simplest form, and in a multi-task progress:
Using the “Progress Bar Overlay” control gives a better notion of completion by adding text to the progress bar:
All of the above GUI forms were created manually. The GUI templates provided with PowerShell Studio can save development time and can also provide ready-to-use code that can be easily modified.
The images below provide examples of the progress bar control added to a pre-existing template.
When you use either the “StatusStrip” or “ToolStrip” control, the progress bar will be added to a specific location on the form. When the “StatusStrip” control is used, the Progress Bar is placed at the bottom of the form:
When the “ToolStrip” control is used, the Progress Bar is placed at the top of the form:
Using either the “Progress Bar” or “Progress bar Overlay” control gives you the ability to add the control anywhere on the form:
If you want to provide a non-graphical progress bar for a console script solution, use the “Write-Progress” cmdlet. For more information and an example, run the following command:
Get-Help Write-Progress -detailed
Keep in mind that using the “Write-Progress” cmdlet is host dependent—when packaging the application, it won’t work when using either “Silent Engine” or “Windows Form” in a GUI application.
By using any of these controls (or cmdlet) and a few lines of code, we can add the visual aid to help us see what’s going on with our script progress.
Notice that when executing the progress bar in a GUI application, it will become unresponsive until it completes processing the code behind the control. If there is a requirement to give a user the ability to interrupt or cancel the progress operation, you need to build a responsive form.
Check out these articles for information on making a responsive GUI:
I will provide an example of a “Responsive Form” in Part 5 of this series—”Handling Steps Progress with a Background Job in a GUI Application”.
In the upcoming series we will explore some progress bar scenarios, including notion of completion, status notification, and steps progress. Up next is Part 2 where we will cover the progress bar use for notion of completion.
Progress Bar series topics:
- Progress Bar Series (Part 2) – Handling Progress with Notion of Completion in a GUI Application
- Progress Bar Series (Part 3) – Handling Progress Status Information in a GUI Application
- Progress Bar Series (Part 4) – Handling Steps Progress with a Background Job in a GUI Application
- Progress Bar Series (Part 5) – Display Progress in a Script (non-GUI) solution
- PowerShell Studio: New ProgressBar Control
- ProgressBarOverlay Control
- StatusBar Control
- StatusStrip Control
As always, if you have any ideas, comments, or feedback, please visit our feedback forum and reference this post.
Max Trinidad is a Technology Evangelist at SAPIEN Technologies Inc., and a Microsoft PowerShell MVP. You can reach him at firstname.lastname@example.org