Mostly No one have interest to wait for some time to complete the work/task/execution.But in certain situation, that’s is not always possible to completed the task without waiting for few seconds/Minutes. In Powershell if you wrote some automation and if sometimes it will to be take more time to execution due to gathering lot of information from then, reality, the script will take a time to complete.
In this scenario the script writer need to show some progress bar to indicate the user about the progress instead of blinking cursor at the delay. This can be achievable by using Write-Progress cmdlet which Displays a progress bar within a Powershell command window.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 |
Write-Progress [-Activity] <String> [[-Status] <String>] [[-Id] <Int32>] [-PercentComplete <Int32>] [-SecondsRemaining <Int32>] [-CurrentOperation <String>] [-ParentId <Int32>] [-Completed] [-SourceId <Int32>] [<CommonParameters>] |
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
############################################################ #Project : How To Create Progress Bars in PowerShell #Developer : Thiyagu S (dotnet-helpers.com) #Tools : PowerShell 5.1.15063.1155 #E-Mail : mail2thiyaguji@gmail.com ############################################################ $progressTimes = 20 $ioop_index = 0 for ($ioop_index=0;$ioop_index -lt $progressTimes; $ioop_index++) { $completeStatus = ($ioop_index / $progressTimes) * 100 #Write-Progress: Displays a progress bar within a PowerShell command window. #-Activity: Specifies the first line of text in the heading above the status bar. This text describes the activity whose progress is being reported. #-Status: Specifies the second line of text in the heading above the status bar. This text describes current state of the activity. #-PercentComplete: This command displays the progress of a For loop that counts from 1 to 20. Write-Progress -Activity 'in Progress' -Status "Did thing $ioop_index times" -PercentComplete $completeStatus -SecondsRemaining ($progressTimes-$ioop_index) #Suspends the activity in a script or session for the specified period of time. Start-Sleep 1 } |
Output:
What do you think?
I hope you have an idea of How To Create Progress Bars in PowerShell. I would like to have feedback from my posts readers. Your valuable feedback, question, or comments about this article are always welcome.