How to create Progress Bar in PowerShell


As the script takes a lot of time, the script writer is aware of this and adds important instructions to make the user aware that something is going on using the progress bar built into PowerShell, which supports Help show notifications and show graphs by graph of percentage of work done. In this article, Taimienphi.vn will guide you how to create Progress Bar in PowerShell.


Of course no one likes to wait ever, we all want work done right away, but it’s not always possible. Everything takes time and you have to accept to wait a while for the process to be completed, this is the reason for creating a progress bar in PowerShell so users can realize that the process is happening. .

In fact, PowerShell has to interact with many different systems and has its own schedule. When the script takes a lot of time, and the script writer is aware of this, adding important instructions to make the user aware that something is happening instead of staring every few minutes at the blinking cursor. .

Script writers have several options for building visual cues so that users are aware of what is happening in the background. Can choose to use references Write-Verbose or Write-Information to inform the status. Or you can use the built-in progress bar in PowerShell, support display notifications and display charts with a percentage graph of the work done.

Suppose a task in your script could take several minutes. Instead of forcing users to stare at the blinking cursor, you can create a Progress Bar in PowerShell.

Firstly, confirm the progress bar you created will be displayed. To control the visibility of the progress bar, PowerShell has an automatic variable called $ ProgressPreference. This variable is set to Continue by default, but if the variable is set to SilentlyContinue for any reason, the progress bar will not be displayed.

After confirming, you can see the progress bar. The progress bar consists of 3 areas represented by command parameters Write-Progress cmdlet, task title (Activity – activity), sub tasks (Status – status) and task completion percentage (PercentComplete).

To open and update the progress bar while it’s running, use the Write-Progress cmdlet command. If Write-Progress runs separately, only long-running processes will be displayed. To test the progress bar, create an infinite loop to run continuously.

while ($ true) {write-progress -Activity ‘Doing thing’; sleep 1}

Create progress bar in powershell 2

In the image above, you can see there is only one activity. But you can add some side quests in it.

$ i = 0

while ($ true) {

Write-Progress -Activity ‘Doing thing’ -Status “Did thing $ i times”

sleep 1

$ i ++

}

When the above code runs, you will see there is a “subactivity” at the bottom showing what is happening. The next step is to add the percentage of completed missions.

$ totalTimes = 10

$ i = 0

for ($ i = 0; $ i -lt $ totalTimes; $ i ++) {

$ percentComplete = ($ i / $ totalTimes) * 100

Write-Progress -Activity ‘Doing thing’ -Status “Did thing $ i times” -PercentComplete $ percentComplete

sleep 1

}

Create progress bar in powershell 3

You have now completed creating the Progress Bar in PowerShell. Using three parameters along with Write-Progress is all you need to start building an intuitive progress bar.

https://thuthuat.taimienphi.vn/cach-tao-progress-bar-in-powershell-27589n.aspx
In this article, Taimienphi.vn has shown you how to create Progress Bar in PowerShell. This is a very useful utility for Windows users, not only allows users to create progress bars, users can also Trim SSD with Powershell without any difficulties. If you have any questions or questions, you can leave your comments in the comment section below the article, Taimienphi.vn will answer your questions as soon as possible.

.

Add a Comment

Your email address will not be published. Required fields are marked *