Certainly, no one likes to wait forever, we all want tasks to be completed immediately, but that's not always possible. Everything takes time and you have to accept waiting for a period for the process to be completed, which is why creating a progress bar in PowerShell is essential for users to realize that the process is ongoing.
In reality, PowerShell has to interact with various systems and has its own schedule. When scripts take up a lot of time, and script writers acknowledge this, they add important directives for users to realize that something is happening instead of staring at the blinking cursor for minutes.
Script writers have several options to create visual cues for users to understand what's happening in the background. They can opt to use reference documents like Write-Verbose or Write-Information to communicate status. Alternatively, they can utilize the built-in progress bar in PowerShell, aiding in displaying messages and showing charts with percentage completion.
Imagine a task in your script that could take up to a few minutes. Instead of making users stare at a blinking cursor, you can create a Progress Bar in PowerShell.
Firstly, you need to confirm that the progress bar you create will be displayed. To control the visibility of the progress bar, PowerShell has an automatic variable called $ProgressPreference. By default, this variable is set to Continue. However, if for some reason the variable is set to SilentlyContinue, the progress bar will not be displayed.
Once confirmed, you can see the progress bar. The progress bar consists of three areas represented by parameters of the Write-Progress cmdlet: the task title (Activity), subtasks (Status), and the percentage of task completion (PercentComplete).
To open and update the progress bar while it's running, you use the Write-Progress cmdlet. If Write-Progress runs alone, it only displays long-running processes. To test the progress bar, create an infinite loop to run continuously.
while ($true) { write-progress -Activity 'Doing task'; sleep 1 }
In the image above, you can see there's only one activity. But you can add some subtasks to it.
$counter = 0
while ($true) {
Report-Progress -Task 'Performing action' -Status 'Action completed $i times'
Start-Sleep -Seconds 1
$i += 1
}
When the above code segment runs, you'll observe a 'subtask' displayed below showing what's happening. The next step is to add the percentage of task completion.
$totalIterations = 10
$counter = 0
for ($i=0;$i -lt $totalIterations; $i++) {
$completionPercentage = ($i / $totalIterations) * 100
Invoke-Action -Activity 'Performing task' -Status 'Task executed $i times' -PercentageCompleted $completionPercentage
Start-Sleep -Seconds 1
}
Now you've completed creating a Progress Bar in PowerShell. Using 3 parameters along with Write-Progress is all you need to start building a visually appealing progress bar.
In this article, Mytour just showed you how to create a Progress Bar in PowerShell. This is a very useful utility for Windows users, not only allowing users to create a progress bar, but users can also Trim SSD with Powershell without any difficulty. If you have any questions or need any assistance, you can leave your comments in the comment section below the article, Mytour will answer your questions as soon as possible.
